|
| 1 | +import { os } from '@orpc/server' |
| 2 | +import { z } from 'zod' |
| 3 | +import { ZodToJsonSchemaConverter } from '../../../zod/src' |
| 4 | +import { OpenAPIHandler } from '../adapters/fetch/openapi-handler' |
| 5 | +import { OpenAPIGenerator } from '../openapi-generator' |
| 6 | +import { OpenAPIReferencePlugin } from './openapi-reference' |
| 7 | + |
| 8 | +describe('openAPIReferencePlugin', () => { |
| 9 | + const jsonSchemaConverter = new ZodToJsonSchemaConverter() |
| 10 | + const generator = new OpenAPIGenerator({ |
| 11 | + schemaConverters: [jsonSchemaConverter], |
| 12 | + }) |
| 13 | + const router = { ping: os.input(z.object({ name: z.string() })).handler(() => 'pong') } |
| 14 | + |
| 15 | + it('serve docs and spec endpoints', async () => { |
| 16 | + const handler = new OpenAPIHandler(router, { |
| 17 | + plugins: [ |
| 18 | + new OpenAPIReferencePlugin({ |
| 19 | + schemaConverters: [jsonSchemaConverter], |
| 20 | + }), |
| 21 | + ], |
| 22 | + }) |
| 23 | + |
| 24 | + const { response } = await handler.handle(new Request('http://localhost:3000')) |
| 25 | + |
| 26 | + expect(response!.status).toBe(200) |
| 27 | + expect(response!.headers.get('content-type')).toBe('text/html') |
| 28 | + expect(await response!.text()).toContain('<title>API Reference</title>') |
| 29 | + |
| 30 | + const { response: specResponse } = await handler.handle(new Request('http://localhost:3000/spec.json')) |
| 31 | + |
| 32 | + expect(specResponse!.status).toBe(200) |
| 33 | + expect(specResponse!.headers.get('content-type')).toBe('application/json') |
| 34 | + expect(await specResponse!.json()).toEqual({ |
| 35 | + ...await generator.generate(router), |
| 36 | + servers: [{ url: 'http://localhost:3000/' }], |
| 37 | + }) |
| 38 | + |
| 39 | + expect( |
| 40 | + await handler.handle(new Request('http://localhost:3000/not_found')), |
| 41 | + ).toEqual({ matched: false }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('serve docs and spec endpoints with prefix', async () => { |
| 45 | + const handler = new OpenAPIHandler(router, { |
| 46 | + plugins: [ |
| 47 | + new OpenAPIReferencePlugin({ |
| 48 | + schemaConverters: [jsonSchemaConverter], |
| 49 | + }), |
| 50 | + ], |
| 51 | + }) |
| 52 | + |
| 53 | + const { response } = await handler.handle(new Request('http://localhost:3000/api'), { |
| 54 | + prefix: '/api', |
| 55 | + }) |
| 56 | + |
| 57 | + expect(response!.status).toBe(200) |
| 58 | + expect(response!.headers.get('content-type')).toBe('text/html') |
| 59 | + expect(await response!.text()).toContain('<title>API Reference</title>') |
| 60 | + |
| 61 | + const { response: specResponse } = await handler.handle(new Request('http://localhost:3000/api/spec.json'), { |
| 62 | + prefix: '/api', |
| 63 | + }) |
| 64 | + |
| 65 | + expect(specResponse!.status).toBe(200) |
| 66 | + expect(specResponse!.headers.get('content-type')).toBe('application/json') |
| 67 | + expect(await specResponse!.json()).toEqual({ |
| 68 | + ...await generator.generate(router), |
| 69 | + servers: [{ url: 'http://localhost:3000/api' }], |
| 70 | + }) |
| 71 | + |
| 72 | + expect( |
| 73 | + await handler.handle(new Request('http://localhost:3000'), { |
| 74 | + prefix: '/api', |
| 75 | + }), |
| 76 | + ).toEqual({ matched: false }) |
| 77 | + |
| 78 | + expect( |
| 79 | + await handler.handle(new Request('http://localhost:3000/spec.json'), { |
| 80 | + prefix: '/api', |
| 81 | + }), |
| 82 | + ).toEqual({ matched: false }) |
| 83 | + |
| 84 | + expect( |
| 85 | + await handler.handle(new Request('http://localhost:3000/api/not_found'), { |
| 86 | + prefix: '/api', |
| 87 | + }), |
| 88 | + ).toEqual({ matched: false }) |
| 89 | + }) |
| 90 | + |
| 91 | + it('not serve docs and spec endpoints if procedure matched', async () => { |
| 92 | + const router = { |
| 93 | + ping: os.route({ method: 'GET', path: '/' }).handler(() => 'pong'), |
| 94 | + pong: os.route({ method: 'GET', path: '/spec.json' }).handler(() => 'ping'), |
| 95 | + } |
| 96 | + |
| 97 | + const handler = new OpenAPIHandler(router, { |
| 98 | + plugins: [ |
| 99 | + new OpenAPIReferencePlugin({ |
| 100 | + schemaConverters: [jsonSchemaConverter], |
| 101 | + }), |
| 102 | + ], |
| 103 | + }) |
| 104 | + |
| 105 | + const { response } = await handler.handle(new Request('http://localhost:3000')) |
| 106 | + expect(await response!.json()).toEqual('pong') |
| 107 | + |
| 108 | + const { response: specResponse } = await handler.handle(new Request('http://localhost:3000/spec.json')) |
| 109 | + expect(await specResponse!.json()).toEqual('ping') |
| 110 | + |
| 111 | + const { matched } = await handler.handle(new Request('http://localhost:3000/not_found')) |
| 112 | + expect(matched).toBe(false) |
| 113 | + }) |
| 114 | + |
| 115 | + it('with config', async () => { |
| 116 | + const handler = new OpenAPIHandler(router, { |
| 117 | + plugins: [ |
| 118 | + new OpenAPIReferencePlugin({ |
| 119 | + schemaConverters: [jsonSchemaConverter], |
| 120 | + docsConfig: async () => ({ foo: '__SOME_VALUE__' }), |
| 121 | + }), |
| 122 | + ], |
| 123 | + }) |
| 124 | + |
| 125 | + const { response } = await handler.handle(new Request('http://localhost:3000')) |
| 126 | + |
| 127 | + expect(response!.status).toBe(200) |
| 128 | + expect(response!.headers.get('content-type')).toBe('text/html') |
| 129 | + expect(await response!.text()).toContain('__SOME_VALUE__') |
| 130 | + }) |
| 131 | +}) |
0 commit comments