Skip to content

Commit

Permalink
feat(cli): Add authentication client to generated client (#2801)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Oct 15, 2022
1 parent f394098 commit bd59f91
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/authentication-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare module '@feathersjs/feathers/lib/declarations' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Application<Services, Settings> {
// eslint-disable-line
io?: any
io: any
rest?: any
authentication: AuthenticationClient
authenticate: AuthenticationClient['authenticate']
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export const generate = (ctx: AppGeneratorArguments) =>
'@feathersjs/configuration',
'@feathersjs/transport-commons',
'@feathersjs/authentication',
'@feathersjs/authentication-client',
'winston'
)

Expand Down Expand Up @@ -211,8 +212,7 @@ export const generate = (ctx: AppGeneratorArguments) =>
'cross-env',
'prettier',
'@feathersjs/cli',
'@feathersjs/rest-client',
'@feathersjs/authentication-client'
'@feathersjs/rest-client'
)

if (language === 'ts') {
Expand Down
21 changes: 19 additions & 2 deletions packages/cli/src/app/templates/client.tpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@ import { generator, toFile } from '@feathershq/pinion'
import { renderSource } from '../../commons'
import { AppGeneratorContext } from '../index'

const template = ({}: AppGeneratorContext) => /* ts */ `import { feathers } from '@feathersjs/feathers'
const template = ({
name,
language
}: AppGeneratorContext) => /* ts */ `import { feathers } from '@feathersjs/feathers'
import type { TransportConnection, Params } from '@feathersjs/feathers'
import authenticationClient from '@feathersjs/authentication-client'
import type { AuthenticationClientOptions } from '@feathersjs/authentication-client'
export interface ServiceTypes {
//
}
export const createClient = <Configuration = any> (connection: TransportConnection<ServiceTypes>) => {
/**
* Returns a ${language === 'ts' ? 'typed' : ''} client for the ${name} app.
*
* @param connection The REST or Socket.io Feathers client connection
* @param authenticationOptions Additional settings for the authentication client
* @see https://dove.feathersjs.com/api/client.html
* @returns The Feathers client application
*/
export const createClient = <Configuration = any> (
connection: TransportConnection<ServiceTypes>,
authenticationOptions: Partial<AuthenticationClientOptions> = {}
) => {
const client = feathers<ServiceTypes, Configuration>()
client.configure(connection)
client.configure(authenticationClient(authenticationOptions))
return client
}
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/src/authentication/templates/client.test.tpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ const appUrl = \`http://\${app.get('host')}:\${port}\`
describe('application client tests', () => {
const client = createClient(rest(appUrl).axios(axios))
client.configure(authenticationClient())
before(async () => {
await app.listen(port)
Expand Down
21 changes: 7 additions & 14 deletions packages/cli/src/service/templates/client.tpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ export type {
}
`

const methodsTemplate = ({ camelName, upperName, className, type }: ServiceGeneratorContext) =>
`const ${camelName}ServiceMethods = ['find', 'get', 'create', 'update', 'patch', 'remove'] as const
const methodsTemplate = ({ camelName, upperName, className, type }: ServiceGeneratorContext) => `
const ${camelName}ServiceMethods = ['find', 'get', 'create', 'update', 'patch', 'remove'] as const
type ${upperName}ClientService = Pick<${className}${
type !== 'custom' ? `<Params<${upperName}Query>>` : ''
}, typeof ${camelName}ServiceMethods[number]>
`
type !== 'custom' ? `<Params<${upperName}Query>>` : ''
}, typeof ${camelName}ServiceMethods[number]>`

const declarationTemplate = ({ path, upperName }: ServiceGeneratorContext) =>
` '${path}': ${upperName}ClientService`
Expand All @@ -41,24 +40,18 @@ const toClientFile = toFile<ServiceGeneratorContext>(({ lib }) => [lib, 'client'

export const generate = async (ctx: ServiceGeneratorContext) =>
generator(ctx)
.then(
injectSource(
registrationTemplate,
before('return client'),
toFile<ServiceGeneratorContext>(({ lib }) => [lib, 'client'])
)
)
.then(injectSource(registrationTemplate, before('return client'), toClientFile))
.then(
when(
(ctx) => ctx.language === 'js',
injectSource(methodsTemplate, before('\nexport const createClient'), toClientFile)
injectSource(methodsTemplate, after('import authenticationClient'), toClientFile)
)
)
.then(
when(
(ctx) => ctx.language === 'ts',
injectSource(methodsTemplate, before('\nexport interface ServiceTypes'), toClientFile),
injectSource(importTemplate, after("from '@feathersjs/feathers'"), toClientFile),
injectSource(methodsTemplate, before('\nexport interface ServiceTypes'), toClientFile),
injectSource(declarationTemplate, after('export interface ServiceTypes'), toClientFile)
)
)
3 changes: 1 addition & 2 deletions packages/socketio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const debug = createDebug('@feathersjs/socketio')
declare module '@feathersjs/feathers/lib/declarations' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Application<Services, Settings> {
// eslint-disable-line
io: Server
io: any
listen(options: any): Promise<http.Server>
}
}
Expand Down

0 comments on commit bd59f91

Please # to comment.