|
| 1 | +import type { OpenAPI } from './openapi' |
| 2 | +import { oc } from '@orpc/contract' |
| 3 | +import { os } from '@orpc/server' |
| 4 | +import { applyCustomOpenAPIOperation, customOpenAPIOperation, getCustomOpenAPIOperation } from './openapi-custom' |
| 5 | + |
| 6 | +it('customOpenAPIOperation & getCustomOpenAPIOperation', () => { |
| 7 | + const customed = customOpenAPIOperation({ value: 123 }, { security: [{ bearerAuth: [] }] }) |
| 8 | + |
| 9 | + expect(customed).toEqual({ value: 123 }) |
| 10 | + expect(getCustomOpenAPIOperation(customed)).toEqual({ security: [{ bearerAuth: [] }] }) |
| 11 | +}) |
| 12 | + |
| 13 | +describe('applyCustomOpenAPIOperation', () => { |
| 14 | + it('no custom operation', () => { |
| 15 | + const procedure = os.handler(() => {}) |
| 16 | + |
| 17 | + const operation: OpenAPI.OperationObject = { |
| 18 | + parameters: [{ |
| 19 | + name: 'id', |
| 20 | + in: 'path', |
| 21 | + required: true, |
| 22 | + schema: { |
| 23 | + type: 'string', |
| 24 | + }, |
| 25 | + }], |
| 26 | + } |
| 27 | + |
| 28 | + expect(applyCustomOpenAPIOperation(operation, procedure)).toBe(operation) |
| 29 | + }) |
| 30 | + |
| 31 | + it('custom at errors', () => { |
| 32 | + const contract = oc.errors({ |
| 33 | + AUTHENTICATION_FAILED: customOpenAPIOperation({}, { |
| 34 | + security: [{ bearerAuth: [] }], |
| 35 | + }), |
| 36 | + TEST: undefined, // ensure check undefinable error map item |
| 37 | + }) |
| 38 | + |
| 39 | + const operation: OpenAPI.OperationObject = { |
| 40 | + parameters: [{ |
| 41 | + name: 'id', |
| 42 | + in: 'path', |
| 43 | + required: true, |
| 44 | + schema: { |
| 45 | + type: 'string', |
| 46 | + }, |
| 47 | + }], |
| 48 | + } |
| 49 | + |
| 50 | + expect(applyCustomOpenAPIOperation(operation, contract)).toEqual({ |
| 51 | + ...operation, |
| 52 | + security: [{ bearerAuth: [] }], |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + it('custom at middlewares', () => { |
| 57 | + const requiredAuth = os.middleware(({ next }) => next()) |
| 58 | + const procedure = os |
| 59 | + .use(customOpenAPIOperation(requiredAuth, { |
| 60 | + security: [{ bearerAuth: [] }], |
| 61 | + })) |
| 62 | + .handler(() => {}) |
| 63 | + |
| 64 | + const operation: OpenAPI.OperationObject = { |
| 65 | + parameters: [{ |
| 66 | + name: 'id', |
| 67 | + in: 'path', |
| 68 | + required: true, |
| 69 | + schema: { |
| 70 | + type: 'string', |
| 71 | + }, |
| 72 | + }], |
| 73 | + } |
| 74 | + |
| 75 | + expect(applyCustomOpenAPIOperation(operation, procedure)).toEqual({ |
| 76 | + ...operation, |
| 77 | + security: [{ bearerAuth: [] }], |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it('callback override', () => { |
| 82 | + const requiredAuth = os.middleware(({ next }) => next()) |
| 83 | + const callback = vi.fn() |
| 84 | + const procedure = os |
| 85 | + .use(customOpenAPIOperation(requiredAuth, callback)) |
| 86 | + .handler(() => { }) |
| 87 | + |
| 88 | + const operation: OpenAPI.OperationObject = { |
| 89 | + parameters: [{ |
| 90 | + name: 'id', |
| 91 | + in: 'path', |
| 92 | + required: true, |
| 93 | + schema: { |
| 94 | + type: 'string', |
| 95 | + }, |
| 96 | + }], |
| 97 | + } |
| 98 | + |
| 99 | + callback.mockReturnValue('__mocked__') |
| 100 | + |
| 101 | + expect(applyCustomOpenAPIOperation(operation, procedure)).toEqual('__mocked__') |
| 102 | + |
| 103 | + expect(callback).toBeCalledTimes(1) |
| 104 | + expect(callback).toBeCalledWith(operation, procedure) |
| 105 | + }) |
| 106 | +}) |
0 commit comments