@furystack/rest-service 4.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +339 -0
- package/README.md +219 -0
- package/dist/actions/error-action.d.ts +14 -0
- package/dist/actions/error-action.d.ts.map +1 -0
- package/dist/actions/error-action.js +29 -0
- package/dist/actions/error-action.js.map +1 -0
- package/dist/actions/error-action.spec.d.ts +2 -0
- package/dist/actions/error-action.spec.d.ts.map +1 -0
- package/dist/actions/error-action.spec.js +51 -0
- package/dist/actions/error-action.spec.js.map +1 -0
- package/dist/actions/get-current-user.d.ts +11 -0
- package/dist/actions/get-current-user.d.ts.map +1 -0
- package/dist/actions/get-current-user.js +15 -0
- package/dist/actions/get-current-user.js.map +1 -0
- package/dist/actions/get-current-user.spec.d.ts +2 -0
- package/dist/actions/get-current-user.spec.d.ts.map +1 -0
- package/dist/actions/get-current-user.spec.js +20 -0
- package/dist/actions/get-current-user.spec.js.map +1 -0
- package/dist/actions/index.d.ts +7 -0
- package/dist/actions/index.d.ts.map +1 -0
- package/dist/actions/index.js +10 -0
- package/dist/actions/index.js.map +1 -0
- package/dist/actions/is-authenticated.d.ts +14 -0
- package/dist/actions/is-authenticated.d.ts.map +1 -0
- package/dist/actions/is-authenticated.js +17 -0
- package/dist/actions/is-authenticated.js.map +1 -0
- package/dist/actions/is-authenticated.spec.d.ts +2 -0
- package/dist/actions/is-authenticated.spec.d.ts.map +1 -0
- package/dist/actions/is-authenticated.spec.js +19 -0
- package/dist/actions/is-authenticated.spec.js.map +1 -0
- package/dist/actions/login-action.spec.d.ts +2 -0
- package/dist/actions/login-action.spec.d.ts.map +1 -0
- package/dist/actions/login-action.spec.js +35 -0
- package/dist/actions/login-action.spec.js.map +1 -0
- package/dist/actions/login.d.ts +16 -0
- package/dist/actions/login.d.ts.map +1 -0
- package/dist/actions/login.js +26 -0
- package/dist/actions/login.js.map +1 -0
- package/dist/actions/logout-action.spec.d.ts +2 -0
- package/dist/actions/logout-action.spec.d.ts.map +1 -0
- package/dist/actions/logout-action.spec.js +23 -0
- package/dist/actions/logout-action.spec.js.map +1 -0
- package/dist/actions/logout.d.ts +14 -0
- package/dist/actions/logout.d.ts.map +1 -0
- package/dist/actions/logout.js +20 -0
- package/dist/actions/logout.js.map +1 -0
- package/dist/actions/not-found-action.d.ts +10 -0
- package/dist/actions/not-found-action.d.ts.map +1 -0
- package/dist/actions/not-found-action.js +14 -0
- package/dist/actions/not-found-action.js.map +1 -0
- package/dist/actions/not-found-action.spec.d.ts +2 -0
- package/dist/actions/not-found-action.spec.d.ts.map +1 -0
- package/dist/actions/not-found-action.spec.js +17 -0
- package/dist/actions/not-found-action.spec.js.map +1 -0
- package/dist/add-cors-header.spec.d.ts +2 -0
- package/dist/add-cors-header.spec.d.ts.map +1 -0
- package/dist/add-cors-header.spec.js +99 -0
- package/dist/add-cors-header.spec.js.map +1 -0
- package/dist/api-manager.d.ts +61 -0
- package/dist/api-manager.d.ts.map +1 -0
- package/dist/api-manager.js +144 -0
- package/dist/api-manager.js.map +1 -0
- package/dist/authenticate.d.ts +5 -0
- package/dist/authenticate.d.ts.map +1 -0
- package/dist/authenticate.js +20 -0
- package/dist/authenticate.js.map +1 -0
- package/dist/authenticate.spec.d.ts +2 -0
- package/dist/authenticate.spec.d.ts.map +1 -0
- package/dist/authenticate.spec.js +59 -0
- package/dist/authenticate.spec.js.map +1 -0
- package/dist/authorize.d.ts +5 -0
- package/dist/authorize.d.ts.map +1 -0
- package/dist/authorize.js +22 -0
- package/dist/authorize.js.map +1 -0
- package/dist/authorize.spec.d.ts +2 -0
- package/dist/authorize.spec.d.ts.map +1 -0
- package/dist/authorize.spec.js +55 -0
- package/dist/authorize.spec.js.map +1 -0
- package/dist/endpoint-generators/create-delete-endpoint.d.ts +17 -0
- package/dist/endpoint-generators/create-delete-endpoint.d.ts.map +1 -0
- package/dist/endpoint-generators/create-delete-endpoint.js +24 -0
- package/dist/endpoint-generators/create-delete-endpoint.js.map +1 -0
- package/dist/endpoint-generators/create-delete-endpoint.spec.d.ts +2 -0
- package/dist/endpoint-generators/create-delete-endpoint.spec.d.ts.map +1 -0
- package/dist/endpoint-generators/create-delete-endpoint.spec.js +33 -0
- package/dist/endpoint-generators/create-delete-endpoint.spec.js.map +1 -0
- package/dist/endpoint-generators/create-get-collection-endpoint.d.ts +17 -0
- package/dist/endpoint-generators/create-get-collection-endpoint.d.ts.map +1 -0
- package/dist/endpoint-generators/create-get-collection-endpoint.js +26 -0
- package/dist/endpoint-generators/create-get-collection-endpoint.js.map +1 -0
- package/dist/endpoint-generators/create-get-collection-endpoint.spec.d.ts +2 -0
- package/dist/endpoint-generators/create-get-collection-endpoint.spec.d.ts.map +1 -0
- package/dist/endpoint-generators/create-get-collection-endpoint.spec.js +143 -0
- package/dist/endpoint-generators/create-get-collection-endpoint.spec.js.map +1 -0
- package/dist/endpoint-generators/create-get-entity-endpoint.d.ts +17 -0
- package/dist/endpoint-generators/create-get-entity-endpoint.d.ts.map +1 -0
- package/dist/endpoint-generators/create-get-entity-endpoint.js +29 -0
- package/dist/endpoint-generators/create-get-entity-endpoint.js.map +1 -0
- package/dist/endpoint-generators/create-get-entity-endpoint.spec.d.ts +2 -0
- package/dist/endpoint-generators/create-get-entity-endpoint.spec.d.ts.map +1 -0
- package/dist/endpoint-generators/create-get-entity-endpoint.spec.js +74 -0
- package/dist/endpoint-generators/create-get-entity-endpoint.spec.js.map +1 -0
- package/dist/endpoint-generators/create-patch-endpoint.d.ts +18 -0
- package/dist/endpoint-generators/create-patch-endpoint.d.ts.map +1 -0
- package/dist/endpoint-generators/create-patch-endpoint.js +26 -0
- package/dist/endpoint-generators/create-patch-endpoint.js.map +1 -0
- package/dist/endpoint-generators/create-patch-endpoint.spec.d.ts +2 -0
- package/dist/endpoint-generators/create-patch-endpoint.spec.d.ts.map +1 -0
- package/dist/endpoint-generators/create-patch-endpoint.spec.js +36 -0
- package/dist/endpoint-generators/create-patch-endpoint.spec.js.map +1 -0
- package/dist/endpoint-generators/create-post-endpoint.d.ts +18 -0
- package/dist/endpoint-generators/create-post-endpoint.d.ts.map +1 -0
- package/dist/endpoint-generators/create-post-endpoint.js +29 -0
- package/dist/endpoint-generators/create-post-endpoint.js.map +1 -0
- package/dist/endpoint-generators/create-post-endpoint.spec.d.ts +2 -0
- package/dist/endpoint-generators/create-post-endpoint.spec.d.ts.map +1 -0
- package/dist/endpoint-generators/create-post-endpoint.spec.js +34 -0
- package/dist/endpoint-generators/create-post-endpoint.spec.js.map +1 -0
- package/dist/endpoint-generators/index.d.ts +6 -0
- package/dist/endpoint-generators/index.d.ts.map +1 -0
- package/dist/endpoint-generators/index.js +9 -0
- package/dist/endpoint-generators/index.js.map +1 -0
- package/dist/endpoint-generators/utils.d.ts +9 -0
- package/dist/endpoint-generators/utils.d.ts.map +1 -0
- package/dist/endpoint-generators/utils.js +27 -0
- package/dist/endpoint-generators/utils.js.map +1 -0
- package/dist/http-authentication-settings.d.ts +17 -0
- package/dist/http-authentication-settings.d.ts.map +1 -0
- package/dist/http-authentication-settings.js +26 -0
- package/dist/http-authentication-settings.js.map +1 -0
- package/dist/http-user-context.d.ts +54 -0
- package/dist/http-user-context.d.ts.map +1 -0
- package/dist/http-user-context.js +153 -0
- package/dist/http-user-context.js.map +1 -0
- package/dist/http-user-context.spec.d.ts +4 -0
- package/dist/http-user-context.spec.d.ts.map +1 -0
- package/dist/http-user-context.spec.js +267 -0
- package/dist/http-user-context.spec.js.map +1 -0
- package/dist/incoming-message-extensions.d.ts +8 -0
- package/dist/incoming-message-extensions.d.ts.map +1 -0
- package/dist/incoming-message-extensions.js +14 -0
- package/dist/incoming-message-extensions.js.map +1 -0
- package/dist/incoming-message-extensions.spec.d.ts +2 -0
- package/dist/incoming-message-extensions.spec.d.ts.map +1 -0
- package/dist/incoming-message-extensions.spec.js +39 -0
- package/dist/incoming-message-extensions.spec.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/injector-extensions.d.ts +21 -0
- package/dist/injector-extensions.d.ts.map +1 -0
- package/dist/injector-extensions.js +14 -0
- package/dist/injector-extensions.js.map +1 -0
- package/dist/injector-extensions.spec.d.ts +2 -0
- package/dist/injector-extensions.spec.d.ts.map +1 -0
- package/dist/injector-extensions.spec.js +19 -0
- package/dist/injector-extensions.spec.js.map +1 -0
- package/dist/models/cors-options.d.ts +22 -0
- package/dist/models/cors-options.d.ts.map +1 -0
- package/dist/models/cors-options.js +3 -0
- package/dist/models/cors-options.js.map +1 -0
- package/dist/models/default-session.d.ts +14 -0
- package/dist/models/default-session.d.ts.map +1 -0
- package/dist/models/default-session.js +10 -0
- package/dist/models/default-session.js.map +1 -0
- package/dist/request-action-implementation.d.ts +54 -0
- package/dist/request-action-implementation.d.ts.map +1 -0
- package/dist/request-action-implementation.js +42 -0
- package/dist/request-action-implementation.js.map +1 -0
- package/dist/rest-service.integration.spec.d.ts +2 -0
- package/dist/rest-service.integration.spec.d.ts.map +1 -0
- package/dist/rest-service.integration.spec.js +129 -0
- package/dist/rest-service.integration.spec.js.map +1 -0
- package/dist/rest.integration.test.d.ts +58 -0
- package/dist/rest.integration.test.d.ts.map +1 -0
- package/dist/rest.integration.test.js +94 -0
- package/dist/rest.integration.test.js.map +1 -0
- package/dist/schema-validator/index.d.ts +3 -0
- package/dist/schema-validator/index.d.ts.map +1 -0
- package/dist/schema-validator/index.js +6 -0
- package/dist/schema-validator/index.js.map +1 -0
- package/dist/schema-validator/schema-validation-error.d.ts +10 -0
- package/dist/schema-validator/schema-validation-error.d.ts.map +1 -0
- package/dist/schema-validator/schema-validation-error.js +15 -0
- package/dist/schema-validator/schema-validation-error.js.map +1 -0
- package/dist/schema-validator/schema-validator.d.ts +20 -0
- package/dist/schema-validator/schema-validator.d.ts.map +1 -0
- package/dist/schema-validator/schema-validator.js +36 -0
- package/dist/schema-validator/schema-validator.js.map +1 -0
- package/dist/schema-validator/schema-validator.test.d.ts +2 -0
- package/dist/schema-validator/schema-validator.test.d.ts.map +1 -0
- package/dist/schema-validator/schema-validator.test.js +62 -0
- package/dist/schema-validator/schema-validator.test.js.map +1 -0
- package/dist/schema-validator/validate-examples.d.ts +37 -0
- package/dist/schema-validator/validate-examples.d.ts.map +1 -0
- package/dist/schema-validator/validate-examples.js +29 -0
- package/dist/schema-validator/validate-examples.js.map +1 -0
- package/dist/server-manager.d.ts +30 -0
- package/dist/server-manager.d.ts.map +1 -0
- package/dist/server-manager.js +71 -0
- package/dist/server-manager.js.map +1 -0
- package/dist/server-response-extensions.d.ts +21 -0
- package/dist/server-response-extensions.d.ts.map +1 -0
- package/dist/server-response-extensions.js +15 -0
- package/dist/server-response-extensions.js.map +1 -0
- package/dist/server-response-extensions.spec.d.ts +2 -0
- package/dist/server-response-extensions.spec.d.ts.map +1 -0
- package/dist/server-response-extensions.spec.js +49 -0
- package/dist/server-response-extensions.spec.js.map +1 -0
- package/dist/utils.d.ts +24 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +66 -0
- package/dist/utils.js.map +1 -0
- package/dist/validate.d.ts +18 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.integration.schema.d.ts +69 -0
- package/dist/validate.integration.schema.d.ts.map +1 -0
- package/dist/validate.integration.schema.js +3 -0
- package/dist/validate.integration.schema.js.map +1 -0
- package/dist/validate.integration.spec.d.ts +13 -0
- package/dist/validate.integration.spec.d.ts.map +1 -0
- package/dist/validate.integration.spec.js +223 -0
- package/dist/validate.integration.spec.js.map +1 -0
- package/dist/validate.integration.spec.schema.json +749 -0
- package/dist/validate.js +49 -0
- package/dist/validate.js.map +1 -0
- package/package.json +56 -0
- package/src/actions/error-action.spec.ts +54 -0
- package/src/actions/error-action.ts +34 -0
- package/src/actions/get-current-user.spec.ts +23 -0
- package/src/actions/get-current-user.ts +15 -0
- package/src/actions/index.ts +6 -0
- package/src/actions/is-authenticated.spec.ts +18 -0
- package/src/actions/is-authenticated.ts +13 -0
- package/src/actions/login-action.spec.ts +41 -0
- package/src/actions/login.ts +26 -0
- package/src/actions/logout-action.spec.ts +27 -0
- package/src/actions/logout.ts +16 -0
- package/src/actions/not-found-action.spec.ts +17 -0
- package/src/actions/not-found-action.ts +13 -0
- package/src/add-cors-header.spec.ts +133 -0
- package/src/api-manager.ts +222 -0
- package/src/authenticate.spec.ts +78 -0
- package/src/authenticate.ts +22 -0
- package/src/authorize.spec.ts +69 -0
- package/src/authorize.ts +19 -0
- package/src/endpoint-generators/create-delete-endpoint.spec.ts +34 -0
- package/src/endpoint-generators/create-delete-endpoint.ts +25 -0
- package/src/endpoint-generators/create-get-collection-endpoint.spec.ts +164 -0
- package/src/endpoint-generators/create-get-collection-endpoint.ts +28 -0
- package/src/endpoint-generators/create-get-entity-endpoint.spec.ts +75 -0
- package/src/endpoint-generators/create-get-entity-endpoint.ts +29 -0
- package/src/endpoint-generators/create-patch-endpoint.spec.ts +36 -0
- package/src/endpoint-generators/create-patch-endpoint.ts +27 -0
- package/src/endpoint-generators/create-post-endpoint.spec.ts +32 -0
- package/src/endpoint-generators/create-post-endpoint.ts +30 -0
- package/src/endpoint-generators/index.ts +5 -0
- package/src/endpoint-generators/utils.ts +34 -0
- package/src/http-authentication-settings.ts +23 -0
- package/src/http-user-context.spec.ts +299 -0
- package/src/http-user-context.ts +160 -0
- package/src/incoming-message-extensions.spec.ts +41 -0
- package/src/incoming-message-extensions.ts +19 -0
- package/src/index.ts +16 -0
- package/src/injector-extensions.spec.ts +19 -0
- package/src/injector-extensions.ts +35 -0
- package/src/models/cors-options.ts +21 -0
- package/src/models/default-session.ts +14 -0
- package/src/request-action-implementation.ts +70 -0
- package/src/rest-service.integration.spec.ts +166 -0
- package/src/rest.integration.test.ts +112 -0
- package/src/schema-validator/index.ts +2 -0
- package/src/schema-validator/schema-validation-error.ts +11 -0
- package/src/schema-validator/schema-validator.test.ts +72 -0
- package/src/schema-validator/schema-validator.ts +31 -0
- package/src/schema-validator/validate-examples.ts +38 -0
- package/src/server-manager.ts +88 -0
- package/src/server-response-extensions.spec.ts +53 -0
- package/src/server-response-extensions.ts +30 -0
- package/src/utils.ts +65 -0
- package/src/validate.integration.schema.ts +50 -0
- package/src/validate.integration.spec.schema.json +779 -0
- package/src/validate.integration.spec.ts +218 -0
- package/src/validate.ts +60 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { Injector } from '@furystack/inject'
|
|
2
|
+
import { createClient } from '@furystack/rest-client-got'
|
|
3
|
+
import { usingAsync } from '@furystack/utils'
|
|
4
|
+
import { RequestError } from 'got/dist/source'
|
|
5
|
+
import { JsonResult } from './request-action-implementation'
|
|
6
|
+
import { Validate } from './validate'
|
|
7
|
+
import './injector-extensions'
|
|
8
|
+
|
|
9
|
+
import schema from './validate.integration.spec.schema.json'
|
|
10
|
+
import { ValidationApi } from 'validate.integration.schema'
|
|
11
|
+
|
|
12
|
+
// To recreate: yarn ts-json-schema-generator -f tsconfig.json --no-type-check -p packages/rest-service/src/validate.integration.schema.ts -o packages/rest-service/src/validate.integration.spec.schema.json
|
|
13
|
+
|
|
14
|
+
export const createValidateApi = async () => {
|
|
15
|
+
const injector = new Injector()
|
|
16
|
+
const port = Math.round(Math.random() * 1000) + 10000
|
|
17
|
+
injector.useRestService<ValidationApi>({
|
|
18
|
+
api: {
|
|
19
|
+
GET: {
|
|
20
|
+
'/validate-query': Validate({
|
|
21
|
+
schema,
|
|
22
|
+
schemaName: 'ValidateQuery',
|
|
23
|
+
})(async ({ getQuery }) => JsonResult({ ...getQuery() })),
|
|
24
|
+
'/validate-url/:id': Validate({
|
|
25
|
+
schema,
|
|
26
|
+
schemaName: 'ValidateUrl',
|
|
27
|
+
})(async ({ getUrlParams }) => JsonResult({ ...getUrlParams() })),
|
|
28
|
+
'/validate-headers': Validate({
|
|
29
|
+
schema,
|
|
30
|
+
schemaName: 'ValidateHeaders',
|
|
31
|
+
})(async ({ headers }) => JsonResult({ ...headers })),
|
|
32
|
+
'/mock': undefined as any, // ToDo: Generator and test
|
|
33
|
+
'/mock/:id': undefined as any, // ToDo: Generator and test
|
|
34
|
+
},
|
|
35
|
+
POST: {
|
|
36
|
+
'/validate-body': Validate({
|
|
37
|
+
schema,
|
|
38
|
+
schemaName: 'ValidateBody',
|
|
39
|
+
})(async ({ getBody }) => {
|
|
40
|
+
const body = await getBody()
|
|
41
|
+
return JsonResult({ ...body })
|
|
42
|
+
}),
|
|
43
|
+
'/mock': undefined as any, // ToDo: Generator and test
|
|
44
|
+
},
|
|
45
|
+
PATCH: {
|
|
46
|
+
'/mock/:id': undefined as any, // ToDo: Generator and test
|
|
47
|
+
},
|
|
48
|
+
DELETE: {
|
|
49
|
+
'/mock/:id': undefined as any, // ToDo: Generator and test
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
port,
|
|
53
|
+
root: '/api',
|
|
54
|
+
})
|
|
55
|
+
const client = createClient<ValidationApi>({
|
|
56
|
+
endpointUrl: `http://localhost:${port}/api`,
|
|
57
|
+
})
|
|
58
|
+
return {
|
|
59
|
+
dispose: injector.dispose.bind(injector),
|
|
60
|
+
client,
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe('Validation integration tests', () => {
|
|
65
|
+
describe('Validation errors', () => {
|
|
66
|
+
it('Should validate query', async () => {
|
|
67
|
+
await usingAsync(await createValidateApi(), async ({ client }) => {
|
|
68
|
+
expect.assertions(5)
|
|
69
|
+
try {
|
|
70
|
+
await client({
|
|
71
|
+
method: 'GET',
|
|
72
|
+
action: '/validate-query',
|
|
73
|
+
query: undefined as any,
|
|
74
|
+
})
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (error instanceof RequestError) {
|
|
77
|
+
expect(error.message).toBe('Response code 400 (Bad Request)')
|
|
78
|
+
expect(error.response?.statusCode).toBe(400)
|
|
79
|
+
const responseJson = JSON.parse(error.response?.body as string)
|
|
80
|
+
expect(responseJson.errors[0].params.missingProperty).toEqual('foo')
|
|
81
|
+
expect(responseJson.errors[1].params.missingProperty).toEqual('bar')
|
|
82
|
+
expect(responseJson.errors[2].params.missingProperty).toEqual('baz')
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
it('Should validate url', async () => {
|
|
88
|
+
await usingAsync(await createValidateApi(), async ({ client }) => {
|
|
89
|
+
expect.assertions(4)
|
|
90
|
+
try {
|
|
91
|
+
await client({
|
|
92
|
+
method: 'GET',
|
|
93
|
+
action: '/validate-url/:id',
|
|
94
|
+
url: undefined as any,
|
|
95
|
+
})
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (error instanceof RequestError) {
|
|
98
|
+
expect(error.message).toBe('Response code 400 (Bad Request)')
|
|
99
|
+
expect(error.response?.statusCode).toBe(400)
|
|
100
|
+
const responseJson = JSON.parse(error.response?.body as string)
|
|
101
|
+
expect(responseJson.errors[0].params.type).toEqual('number')
|
|
102
|
+
expect(responseJson.errors[0].instancePath).toEqual('/url/id')
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
it('Should validate headers', async () => {
|
|
108
|
+
await usingAsync(await createValidateApi(), async ({ client }) => {
|
|
109
|
+
expect.assertions(3)
|
|
110
|
+
try {
|
|
111
|
+
await client({
|
|
112
|
+
method: 'GET',
|
|
113
|
+
action: '/validate-headers',
|
|
114
|
+
headers: undefined as any,
|
|
115
|
+
})
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (error instanceof RequestError) {
|
|
118
|
+
expect(error.message).toBe('Response code 400 (Bad Request)')
|
|
119
|
+
expect(error.response?.statusCode).toBe(400)
|
|
120
|
+
const responseJson = JSON.parse(error.response?.body as string)
|
|
121
|
+
expect(
|
|
122
|
+
responseJson.errors.find((e: any) => e.keyword === 'required' && e.message.includes('foo')),
|
|
123
|
+
).toBeDefined()
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
it('Should validate body', async () => {
|
|
129
|
+
await usingAsync(await createValidateApi(), async ({ client }) => {
|
|
130
|
+
expect.assertions(3)
|
|
131
|
+
try {
|
|
132
|
+
await client({
|
|
133
|
+
method: 'POST',
|
|
134
|
+
action: '/validate-body',
|
|
135
|
+
body: undefined as any,
|
|
136
|
+
})
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (error instanceof RequestError) {
|
|
139
|
+
expect(error.message).toBe('Response code 400 (Bad Request)')
|
|
140
|
+
expect(error.response?.statusCode).toBe(400)
|
|
141
|
+
const responseJson = JSON.parse(error.response?.body as string)
|
|
142
|
+
expect(responseJson.errors[0].params.missingProperty).toEqual('body')
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
describe('Validation Success', () => {
|
|
150
|
+
it('Should validate query', async () => {
|
|
151
|
+
await usingAsync(await createValidateApi(), async ({ client }) => {
|
|
152
|
+
const result = await client({
|
|
153
|
+
method: 'GET',
|
|
154
|
+
action: '/validate-query',
|
|
155
|
+
query: {
|
|
156
|
+
foo: 'foo',
|
|
157
|
+
bar: 2,
|
|
158
|
+
baz: false,
|
|
159
|
+
},
|
|
160
|
+
})
|
|
161
|
+
expect(result.response.statusCode).toBe(200)
|
|
162
|
+
const responseJson = result.getJson()
|
|
163
|
+
expect(responseJson.foo).toBe('foo')
|
|
164
|
+
expect(responseJson.bar).toBe(2)
|
|
165
|
+
expect(responseJson.baz).toBe(false)
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
it('Should validate url', async () => {
|
|
169
|
+
await usingAsync(await createValidateApi(), async ({ client }) => {
|
|
170
|
+
const result = await client({
|
|
171
|
+
method: 'GET',
|
|
172
|
+
action: '/validate-url/:id',
|
|
173
|
+
url: { id: 3 },
|
|
174
|
+
})
|
|
175
|
+
expect(result.response.statusCode).toBe(200)
|
|
176
|
+
const responseJson = result.getJson()
|
|
177
|
+
expect(responseJson.id).toBe(3)
|
|
178
|
+
})
|
|
179
|
+
})
|
|
180
|
+
it('Should validate headers', async () => {
|
|
181
|
+
await usingAsync(await createValidateApi(), async ({ client }) => {
|
|
182
|
+
const result = await client({
|
|
183
|
+
method: 'GET',
|
|
184
|
+
action: '/validate-headers',
|
|
185
|
+
headers: {
|
|
186
|
+
foo: 'foo',
|
|
187
|
+
bar: 42,
|
|
188
|
+
baz: true,
|
|
189
|
+
},
|
|
190
|
+
})
|
|
191
|
+
expect(result.response.statusCode).toBe(200)
|
|
192
|
+
const responseJson = result.getJson()
|
|
193
|
+
expect(responseJson.foo).toBe('foo')
|
|
194
|
+
expect(responseJson.bar).toBe(42)
|
|
195
|
+
expect(responseJson.baz).toBe(true)
|
|
196
|
+
})
|
|
197
|
+
})
|
|
198
|
+
it('Should validate body', async () => {
|
|
199
|
+
await usingAsync(await createValidateApi(), async ({ client }) => {
|
|
200
|
+
const result = await client({
|
|
201
|
+
method: 'POST',
|
|
202
|
+
action: '/validate-body',
|
|
203
|
+
body: {
|
|
204
|
+
foo: 'foo',
|
|
205
|
+
bar: 42,
|
|
206
|
+
baz: true,
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
expect(result.response.statusCode).toBe(200)
|
|
211
|
+
const responseJson = result.getJson()
|
|
212
|
+
expect(responseJson.foo).toBe('foo')
|
|
213
|
+
expect(responseJson.bar).toBe(42)
|
|
214
|
+
expect(responseJson.baz).toBe(true)
|
|
215
|
+
})
|
|
216
|
+
})
|
|
217
|
+
})
|
|
218
|
+
})
|
package/src/validate.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ActionResult, RequestAction, RequestActionOptions } from './request-action-implementation'
|
|
2
|
+
import { SchemaValidator } from './schema-validator'
|
|
3
|
+
|
|
4
|
+
export const Validate =
|
|
5
|
+
<TSchema extends { definitions: { [K: string]: { required?: string[] } | any } }>(validationOptions: {
|
|
6
|
+
/**
|
|
7
|
+
* The Schema object
|
|
8
|
+
*/
|
|
9
|
+
schema: TSchema
|
|
10
|
+
/**
|
|
11
|
+
* Entity key from the JSON Schema object
|
|
12
|
+
*/
|
|
13
|
+
schemaName: keyof TSchema['definitions']
|
|
14
|
+
}) =>
|
|
15
|
+
<T extends { result: any }>(action: RequestAction<T>): RequestAction<T> => {
|
|
16
|
+
const schema = { ...validationOptions.schema }
|
|
17
|
+
|
|
18
|
+
Object.values(schema.definitions).forEach((definition) => {
|
|
19
|
+
if (definition.required && definition.required.includes('result')) {
|
|
20
|
+
definition.required = definition.required.filter((value: any) => value !== 'result')
|
|
21
|
+
}
|
|
22
|
+
definition.additionalProperties = true
|
|
23
|
+
if (definition.properties?.headers) {
|
|
24
|
+
definition.properties.headers.additionalProperties = true
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const validator = new SchemaValidator(schema, { coerceTypes: true })
|
|
29
|
+
|
|
30
|
+
return async (args: RequestActionOptions<T>): Promise<ActionResult<T>> => {
|
|
31
|
+
const anyArgs = args as any
|
|
32
|
+
let body!: any
|
|
33
|
+
const { headers } = anyArgs
|
|
34
|
+
const query = anyArgs.getQuery?.()
|
|
35
|
+
const url = anyArgs.getUrlParams?.()
|
|
36
|
+
try {
|
|
37
|
+
body = await anyArgs.getBody?.()
|
|
38
|
+
} catch (error) {
|
|
39
|
+
// ignore
|
|
40
|
+
}
|
|
41
|
+
validator.isValid(
|
|
42
|
+
{
|
|
43
|
+
...(query ? { query } : {}),
|
|
44
|
+
...(body ? { body } : {}),
|
|
45
|
+
...(url ? { url } : {}),
|
|
46
|
+
...(headers ? { headers } : {}),
|
|
47
|
+
},
|
|
48
|
+
{ schemaName: validationOptions.schemaName },
|
|
49
|
+
)
|
|
50
|
+
return await action({
|
|
51
|
+
request: args.request,
|
|
52
|
+
response: args.response,
|
|
53
|
+
injector: args.injector,
|
|
54
|
+
headers,
|
|
55
|
+
getQuery: () => query,
|
|
56
|
+
getUrlParams: () => url,
|
|
57
|
+
getBody: () => Promise.resolve(body),
|
|
58
|
+
} as any)
|
|
59
|
+
}
|
|
60
|
+
}
|