-
-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generators): Add service file for shared information (#3008)
- Loading branch information
Showing
11 changed files
with
141 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
outline: deep | ||
--- | ||
|
||
# Service Shared | ||
|
||
The `<service>.shared` file contains variables and type declarations that are shared between the [client](./client.md) and the [server application](./app.md). It can also be used for shared utility functions or schemas (e.g. for client side validation). | ||
|
||
## Variables | ||
|
||
By default two shared variables are exported: | ||
|
||
- `<name>Path` - The path of the service. Changing this will change the path for the service in all places like the application, the client and types | ||
- `<name>Methods` - The list of service methods available to the client. This can be updated with service and custom methods a client should be able to use. | ||
|
||
## Client setup | ||
|
||
This file also includes the client side service registration which will be included in the [client](./client.md). It will register a client side service based on the shared path and methods. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,31 @@ | ||
import { generator, toFile, when, after, before } from '@feathershq/pinion' | ||
import { generator, toFile, after, before } from '@feathershq/pinion' | ||
import { injectSource } from '../../commons' | ||
import { ServiceGeneratorContext } from '../index' | ||
|
||
const importTemplate = ({ | ||
upperName, | ||
folder, | ||
fileName, | ||
className, | ||
camelName, | ||
type | ||
}: ServiceGeneratorContext) => /* ts */ `import type { | ||
${upperName}, | ||
${upperName}Data, | ||
${upperName}Query, | ||
${className} | ||
} from './services/${folder.join('/')}/${fileName}' | ||
const importTemplate = ({ upperName, folder, fileName, camelName }: ServiceGeneratorContext) => /* ts */ ` | ||
import { ${camelName}Client } from './services/${folder.join('/')}/${fileName}.shared' | ||
export type { | ||
${upperName}, | ||
${upperName}Data, | ||
${upperName}Query | ||
} | ||
export const ${camelName}ServiceMethods = ['find', 'get', 'create', 'patch', 'remove'] as const | ||
export type ${upperName}ClientService = Pick<${className}${ | ||
type !== 'custom' ? `<Params<${upperName}Query>>` : '' | ||
}, typeof ${camelName}ServiceMethods[number]> | ||
${upperName}Query, | ||
${upperName}Patch | ||
} from './services/${folder.join('/')}/${fileName}.shared' | ||
` | ||
|
||
const declarationTemplate = ({ path, upperName }: ServiceGeneratorContext) => | ||
` '${path}': ${upperName}ClientService` | ||
|
||
const registrationTemplate = ({ | ||
camelName, | ||
path | ||
}: ServiceGeneratorContext) => ` client.use('${path}', connection.service('${path}'), { | ||
methods: ${camelName}ServiceMethods | ||
})` | ||
const registrationTemplate = ({ camelName }: ServiceGeneratorContext) => | ||
` client.configure(${camelName}Client)` | ||
|
||
const toClientFile = toFile<ServiceGeneratorContext>(({ lib }) => [lib, 'client']) | ||
|
||
export const generate = async (ctx: ServiceGeneratorContext) => | ||
generator(ctx) | ||
.then(injectSource(registrationTemplate, before('return client'), toClientFile)) | ||
.then( | ||
when( | ||
(ctx) => ctx.language === 'js', | ||
injectSource(importTemplate, after('import authenticationClient'), toClientFile) | ||
) | ||
) | ||
.then( | ||
when( | ||
(ctx) => ctx.language === 'ts', | ||
injectSource(importTemplate, after('import type { AuthenticationClientOptions }'), toClientFile), | ||
injectSource(declarationTemplate, after('export interface ServiceTypes'), toClientFile) | ||
injectSource( | ||
importTemplate, | ||
after<ServiceGeneratorContext>(({ language }) => | ||
language === 'ts' ? 'import type { AuthenticationClientOptions }' : 'import authenticationClient' | ||
), | ||
toClientFile | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { generator, toFile } from '@feathershq/pinion' | ||
import { renderSource } from '../../commons' | ||
import { ServiceGeneratorContext } from '../index' | ||
|
||
const sharedTemplate = ({ | ||
camelName, | ||
upperName, | ||
className, | ||
fileName, | ||
relative, | ||
path | ||
}: ServiceGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/service.shared.html | ||
import type { Params } from '@feathersjs/feathers' | ||
import type { ClientApplication } from '${relative}/client' | ||
import type { | ||
${upperName}, | ||
${upperName}Data, | ||
${upperName}Patch, | ||
${upperName}Query, | ||
${className} | ||
} from './${fileName}.class' | ||
export type { ${upperName}, ${upperName}Data, ${upperName}Patch, ${upperName}Query } | ||
export type ${upperName}ClientService = Pick< | ||
${className}<Params<${upperName}Query>>, | ||
typeof ${camelName}Methods[number] | ||
> | ||
export const ${camelName}Path = '${path}' | ||
export const ${camelName}Methods = ['find', 'get', 'create', 'patch', 'remove'] as const | ||
export const ${camelName}Client = (client: ClientApplication) => { | ||
const connection = client.get('connection') | ||
client.use(${camelName}Path, connection.service(${camelName}Path), { | ||
methods: ${camelName}Methods | ||
}) | ||
} | ||
// Add this service to the client service type index | ||
declare module '${relative}/client' { | ||
interface ServiceTypes { | ||
[${camelName}Path]: ${upperName}ClientService | ||
} | ||
} | ||
` | ||
|
||
export const generate = async (ctx: ServiceGeneratorContext) => | ||
generator(ctx).then( | ||
renderSource( | ||
sharedTemplate, | ||
toFile(({ lib, folder, fileName }: ServiceGeneratorContext) => [ | ||
lib, | ||
'services', | ||
...folder, | ||
`${fileName}.shared` | ||
]) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters