Skip to content

Commit

Permalink
Add Authorization header to OpenAPI docs
Browse files Browse the repository at this point in the history
Closes #43

Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
  • Loading branch information
flakey5 committed May 15, 2024
1 parent acf323c commit 6c4abab
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
10 changes: 3 additions & 7 deletions config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,9 @@ export interface AiWarpConfig {
prefix?: string;
path?: string;
};
plugins?:
| {
[k: string]: unknown;
}
| {
[k: string]: unknown;
};
plugins?: {
[k: string]: unknown;
};
}[];
};
module?: string;
Expand Down
22 changes: 22 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ const stackable: Stackable<AiWarpConfig> = async function (fastify, opts) {
})
}

if (config.service === undefined) {
config.service = {}
}

const currentOpenApiConfig = typeof config.service.openapi === 'object' ? config.service.openapi : {}
if (config.auth?.jwt !== undefined) {
config.service.openapi = {
...currentOpenApiConfig,
components: {
...currentOpenApiConfig.components,
securitySchemes: {
...currentOpenApiConfig.components?.securitySchemes,
aiWarpJwtToken: {
type: 'apiKey',
in: 'header',
name: 'Authorization'
}
}
}
}
}

await fastify.register(platformaticService, opts)

await fastify.register(warpPlugin, opts) // needs to be registered here for fastify.ai to be decorated
Expand Down
7 changes: 7 additions & 0 deletions plugins/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ function isAFastifyError (object: object): object is FastifyError {
const InternalServerError = createError('INTERNAL_SERVER_ERROR', 'Internal Server Error', 500)

const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
let security: Array<Record<string, string[]>> | undefined
if (fastify.platformatic.config.auth?.jwt !== undefined) {
security = [{ aiWarpJwtToken: [] }]
}

fastify.route({
url: '/api/v1/prompt',
method: 'POST',
schema: {
security,
body: Type.Object({
prompt: Type.String(),
chatHistory: Type.Optional(Type.Array(Type.Object({
Expand Down Expand Up @@ -55,6 +61,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
method: 'POST',
schema: {
produces: ['text/event-stream'],
security,
body: Type.Object({
prompt: Type.String(),
chatHistory: Type.Optional(Type.Array(Type.Object({
Expand Down
2 changes: 1 addition & 1 deletion schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$id": "ai-warp",
"version": "1.37.1",
"version": "1.39.0",
"title": "Ai Warp Config",
"type": "object",
"properties": {
Expand Down
41 changes: 41 additions & 0 deletions tests/e2e/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,47 @@ it('provides all paths in OpenAPI', async () => {
await app.close()
})

it('provides header for jwt token in OpenAPI', async () => {
const [app, port] = await buildAiWarpApp({
aiProvider: {
openai: {
model: 'gpt-3.5-turbo',
apiKey: ''
}
},
auth: {
jwt: {
secret: 'asd'
}
}
})

await app.start()

const res = await fetch(`http://localhost:${port}/documentation/json`)
const body = await res.json()

assert.deepStrictEqual(body.components, {
securitySchemes: {
aiWarpJwtToken: {
type: 'apiKey',
in: 'header',
name: 'Authorization'
}
},
schemas: {}
})

const endpoints = ['/api/v1/prompt', '/api/v1/stream']
for (const endpoint of endpoints) {
assert.deepStrictEqual(body.paths[endpoint].post.security, [
{ aiWarpJwtToken: [] }
])
}

await app.close()
})

it('prompt with wrong JSON', async () => {
const [app, port] = await buildAiWarpApp({
aiProvider: {
Expand Down

0 comments on commit 6c4abab

Please sign in to comment.