Skip to content

Commit

Permalink
feat(clients): allow specifying a custom client id (#343)
Browse files Browse the repository at this point in the history
* feat(clients): allow specifying a custom client id

* update client

* fix

* tests

* bump package

* api.rest
  • Loading branch information
samuelmasse committed Feb 9, 2022
1 parent 8bc2369 commit cd42f30
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
5 changes: 5 additions & 0 deletions misc/api.rest
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ client_id=CLOUD_CLIENT

### Create client
POST {{baseUrl}}/api/admin/clients
Content-Type: application/json
Authorization: Bearer {{authToken}}
x-bp-messaging-admin-key: {{adminKey}}

{
// "id": "customid"
}

### Sync client
POST {{baseUrl}}/api/admin/clients/sync
Content-Type: application/json
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botpress/messaging-client",
"version": "1.0.1",
"version": "1.0.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"source": "src/index.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { MessagingChannelBase } from './base'
import { handleNotFound } from './errors'

export abstract class MessagingChannelApi extends MessagingChannelBase {
async createClient(): Promise<{ id: string; token: string }> {
return (await this.http.post('/admin/clients', undefined, { headers: this.adminHeader })).data
async createClient(id?: uuid): Promise<{ id: uuid; token: string }> {
return (await this.http.post('/admin/clients', { id }, { headers: this.adminHeader })).data
}

async syncClient(config: { name: string; id?: uuid; token?: string }): Promise<{ id: uuid; token: string }> {
Expand Down
32 changes: 29 additions & 3 deletions packages/client/test/e2e/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v4 as uuid } from 'uuid'
import { v4 as uuid, validate as validateUuid } from 'uuid'
import { Conversation, Message, MessagingChannel, MessagingClient, SyncRequest, SyncWebhook, User } from '../../src'

const FAKE_UUID = uuid()
Expand All @@ -19,6 +19,34 @@ describe('Http Client', () => {
expect((client as any).channel.http.defaults.baseURL).toContain(url)
})

const url = 'http://localhost:3100'
const adminClient = new MessagingChannel({ url, adminKey: process.env.ADMIN_KEY })

describe('Clients', () => {
const customId = uuid()

test('Should create a messaging client', async () => {
const client = await adminClient.createClient()

expect(validateUuid(client.id)).toBeTruthy()
expect(client.token).toBeDefined()
expect(client.token.length).toBe(125)
})

test('Should create a messaging client that has the specified id', async () => {
const client = await adminClient.createClient(customId)

expect(validateUuid(client.id)).toBeTruthy()
expect(client.id).toBe(customId)
expect(client.token).toBeDefined()
expect(client.token.length).toBe(125)
})

test('Should not be able to create another client with the same id', async () => {
await expect(adminClient.createClient(customId)).rejects.toThrow(new Error('Request failed with status code 403'))
})
})

const state: {
clientId?: string
clientToken?: string
Expand All @@ -27,8 +55,6 @@ describe('Http Client', () => {
message?: Message
webhooks?: SyncWebhook[]
} = {}
const url = 'http://localhost:3100'
const adminClient = new MessagingChannel({ url, adminKey: process.env.ADMIN_KEY })
let client: MessagingClient
const webhooks = [{ url: 'http://un.known.url' }, { url: 'http://second.un.known.url' }]

Expand Down
8 changes: 6 additions & 2 deletions packages/server/src/clients/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ export class ClientApi {
}

async create(req: Request, res: Response) {
const clientId = uuidv4()
const clientId: uuid = req.body.id

const provider = await this.providers.create(clientId, false)
if (clientId && (await this.clients.fetchById(clientId))) {
return res.status(403).send(`client with id "${clientId}" already exists`)
}

const provider = await this.providers.create(clientId || uuidv4(), false)
const client = await this.clients.create(provider.id, clientId)

const rawToken = await this.clientTokens.generateToken()
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/clients/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Joi from 'joi'
import { ReqSchema } from '../base/schema'

const Api = {
Create: ReqSchema(),
Create: ReqSchema({ body: { id: Joi.string().uuid().optional() } }),

Sync: ReqSchema({
body: { id: Joi.string().optional(), token: Joi.string().optional(), name: Joi.string().required() }
Expand Down

0 comments on commit cd42f30

Please sign in to comment.