moonflower 0.10.2 → 0.10.4
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/dist/cli/cli.js +17 -7
- package/dist/cli/prettyprint.d.ts +1 -1
- package/dist/src/errors/HttpErrorHandler.d.ts.map +1 -1
- package/dist/src/hooks/authentication/useAuth.d.ts.map +1 -1
- package/dist/src/hooks/authentication/useOptionalAuth.d.ts.map +1 -1
- package/dist/src/hooks/useApiHeader/useApiHeader.d.ts.map +1 -1
- package/dist/src/hooks/useCookieParams.d.ts.map +1 -1
- package/dist/src/hooks/useExposeApiModel/useExposeApiModel.d.ts.map +1 -1
- package/dist/src/hooks/useHeaderParams.d.ts.map +1 -1
- package/dist/src/hooks/usePathParams.d.ts.map +1 -1
- package/dist/src/hooks/useQueryParams.d.ts.map +1 -1
- package/dist/src/hooks/useRequestBody.d.ts.map +1 -1
- package/dist/src/hooks/useRequestRawBody.d.ts.map +1 -1
- package/dist/src/hooks/useReturnValue.d.ts.map +1 -1
- package/dist/src/openapi/analyzerModule/analyzerModule.js +17 -7
- package/dist/src/openapi/analyzerModule/nodeParsers.d.ts +5 -5
- package/dist/src/openapi/analyzerModule/nodeParsers.d.ts.map +1 -1
- package/dist/src/openapi/analyzerModule/parseEndpoint.d.ts.map +1 -1
- package/dist/src/openapi/analyzerModule/parseExposedModels.d.ts.map +1 -1
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterA.spec.data.d.ts +0 -1
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterA.spec.data.d.ts.map +1 -1
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterB.spec.data.d.ts +0 -1
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterB.spec.data.d.ts.map +1 -1
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/discoverRouterFiles.d.ts +2 -2
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/discoverRouterFiles.d.ts.map +1 -1
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/discoverRouterFiles.js +17 -7
- package/dist/src/openapi/discoveryModule/discoverRouters/discoverRouters.d.ts +1 -1
- package/dist/src/openapi/discoveryModule/discoverRouters/discoverRouters.spec.data.d.ts +0 -1
- package/dist/src/openapi/discoveryModule/discoverRouters/discoverRouters.spec.data.d.ts.map +1 -1
- package/dist/src/openapi/generatorModule/generatePaths.d.ts +5 -5
- package/dist/src/openapi/generatorModule/generatePaths.d.ts.map +1 -1
- package/dist/src/openapi/generatorModule/generatorModule.d.ts +5 -5
- package/dist/src/openapi/initOpenApiEngine.d.ts.map +1 -1
- package/dist/src/openapi/router/OpenApiRouter.d.ts +0 -1
- package/dist/src/openapi/router/OpenApiRouter.d.ts.map +1 -1
- package/dist/src/router/Router.d.ts +1 -1
- package/dist/src/router/Router.d.ts.map +1 -1
- package/dist/src/router/Router.js +15 -24
- package/dist/src/router/parseEndpointReturnValue.d.ts +7 -1
- package/dist/src/router/parseEndpointReturnValue.d.ts.map +1 -1
- package/dist/src/router/parseEndpointReturnValue.js +11 -4
- package/dist/src/test/TestAppRouter.d.ts +0 -1
- package/dist/src/test/TestAppRouter.d.ts.map +1 -1
- package/dist/src/test/TestAppRouter.js +6 -0
- package/dist/src/utils/loadTestData.js +17 -7
- package/dist/src/utils/mockContext.d.ts +6 -6
- package/dist/src/utils/mockContext.d.ts.map +1 -1
- package/dist/src/utils/mockContext.js +17 -7
- package/dist/src/utils/nameOf.d.ts.map +1 -1
- package/dist/src/utils/object.d.ts +1 -1
- package/dist/src/utils/object.d.ts.map +1 -1
- package/dist/src/utils/printers.d.ts.map +1 -1
- package/dist/src/utils/printers.js +17 -7
- package/dist/src/utils/validationMessages.d.ts +4 -4
- package/dist/src/utils/validationMessages.d.ts.map +1 -1
- package/dist/src/validators/InternalParamWrappers.d.ts +1 -1
- package/dist/src/validators/InternalParamWrappers.d.ts.map +1 -1
- package/dist/src/validators/ParamWrappers.d.ts +3 -3
- package/dist/src/validators/ParamWrappers.d.ts.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +30 -30
- package/src/router/Router.ts +20 -30
- package/src/router/parseEndpointReturnValue.spec.ts +50 -3
- package/src/router/parseEndpointReturnValue.ts +11 -4
- package/src/test/TestAppRouter.ts +9 -0
- package/src/test/app.spec.ts +14 -0
package/src/router/Router.ts
CHANGED
|
@@ -20,6 +20,20 @@ export class Router<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
private async sendResponseValue<P extends string>(
|
|
24
|
+
ctx: Parameters<Parameters<KoaRouter['get']>[1]>[0],
|
|
25
|
+
callback: KoaRouter.Middleware<StateT, ContextT & ExtractedRequestParams<P>>
|
|
26
|
+
) {
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
const responseValue = await callback(ctx, undefined)
|
|
29
|
+
const { value, status, contentType } = parseEndpointReturnValue(responseValue)
|
|
30
|
+
ctx.body = value
|
|
31
|
+
if (status !== 'unset') {
|
|
32
|
+
ctx.status = status
|
|
33
|
+
}
|
|
34
|
+
ctx.set('Content-Type', contentType)
|
|
35
|
+
}
|
|
36
|
+
|
|
23
37
|
public use(...middleware: Array<KoaRouter.Middleware<StateT, ContextT>>) {
|
|
24
38
|
// @ts-ignore
|
|
25
39
|
this.koaRouter.use(...middleware)
|
|
@@ -46,11 +60,7 @@ export class Router<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
|
|
46
60
|
callback: KoaRouter.Middleware<StateT, ContextT & ExtractedRequestParams<P>>
|
|
47
61
|
) {
|
|
48
62
|
this.koaRouter.get(path, async (ctx) => {
|
|
49
|
-
|
|
50
|
-
const responseValue = await callback(ctx, undefined)
|
|
51
|
-
const { value, contentType } = parseEndpointReturnValue(responseValue)
|
|
52
|
-
ctx.body = value
|
|
53
|
-
ctx.set('Content-Type', contentType)
|
|
63
|
+
await this.sendResponseValue(ctx, callback)
|
|
54
64
|
})
|
|
55
65
|
return this
|
|
56
66
|
}
|
|
@@ -60,11 +70,7 @@ export class Router<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
|
|
60
70
|
callback: KoaRouter.Middleware<StateT, ContextT & ExtractedRequestParams<P>>
|
|
61
71
|
) {
|
|
62
72
|
this.koaRouter.post(path, async (ctx) => {
|
|
63
|
-
|
|
64
|
-
const responseValue = await callback(ctx, undefined)
|
|
65
|
-
const { value, contentType } = parseEndpointReturnValue(responseValue)
|
|
66
|
-
ctx.body = value
|
|
67
|
-
ctx.set('Content-Type', contentType)
|
|
73
|
+
await this.sendResponseValue(ctx, callback)
|
|
68
74
|
})
|
|
69
75
|
return this
|
|
70
76
|
}
|
|
@@ -74,11 +80,7 @@ export class Router<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
|
|
74
80
|
callback: KoaRouter.Middleware<StateT, ContextT & ExtractedRequestParams<P>>
|
|
75
81
|
) {
|
|
76
82
|
this.koaRouter.put(path, async (ctx) => {
|
|
77
|
-
|
|
78
|
-
const responseValue = await callback(ctx, undefined)
|
|
79
|
-
const { value, contentType } = parseEndpointReturnValue(responseValue)
|
|
80
|
-
ctx.body = value
|
|
81
|
-
ctx.set('Content-Type', contentType)
|
|
83
|
+
await this.sendResponseValue(ctx, callback)
|
|
82
84
|
})
|
|
83
85
|
return this
|
|
84
86
|
}
|
|
@@ -88,11 +90,7 @@ export class Router<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
|
|
88
90
|
callback: KoaRouter.Middleware<StateT, ContextT & ExtractedRequestParams<P>>
|
|
89
91
|
) {
|
|
90
92
|
this.koaRouter.delete(path, async (ctx) => {
|
|
91
|
-
|
|
92
|
-
const responseValue = await callback(ctx, undefined)
|
|
93
|
-
const { value, contentType } = parseEndpointReturnValue(responseValue)
|
|
94
|
-
ctx.body = value
|
|
95
|
-
ctx.set('Content-Type', contentType)
|
|
93
|
+
await this.sendResponseValue(ctx, callback)
|
|
96
94
|
})
|
|
97
95
|
return this
|
|
98
96
|
}
|
|
@@ -102,11 +100,7 @@ export class Router<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
|
|
102
100
|
callback: KoaRouter.Middleware<StateT, ContextT & ExtractedRequestParams<P>>
|
|
103
101
|
) {
|
|
104
102
|
this.koaRouter.del(path, async (ctx) => {
|
|
105
|
-
|
|
106
|
-
const responseValue = await callback(ctx, undefined)
|
|
107
|
-
const { value, contentType } = parseEndpointReturnValue(responseValue)
|
|
108
|
-
ctx.body = value
|
|
109
|
-
ctx.set('Content-Type', contentType)
|
|
103
|
+
await this.sendResponseValue(ctx, callback)
|
|
110
104
|
})
|
|
111
105
|
return this
|
|
112
106
|
}
|
|
@@ -116,11 +110,7 @@ export class Router<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
|
|
|
116
110
|
callback: KoaRouter.Middleware<StateT, ContextT & ExtractedRequestParams<P>>
|
|
117
111
|
) {
|
|
118
112
|
this.koaRouter.patch(path, async (ctx) => {
|
|
119
|
-
|
|
120
|
-
const responseValue = await callback(ctx, undefined)
|
|
121
|
-
const { value, contentType } = parseEndpointReturnValue(responseValue)
|
|
122
|
-
ctx.body = value
|
|
123
|
-
ctx.set('Content-Type', contentType)
|
|
113
|
+
await this.sendResponseValue(ctx, callback)
|
|
124
114
|
})
|
|
125
115
|
return this
|
|
126
116
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useReturnValue } from '../hooks/useReturnValue'
|
|
1
2
|
import { parseEndpointReturnValue } from './parseEndpointReturnValue'
|
|
2
3
|
|
|
3
4
|
describe('parseEndpointReturnValue', () => {
|
|
@@ -5,6 +6,16 @@ describe('parseEndpointReturnValue', () => {
|
|
|
5
6
|
const value = 'foo'
|
|
6
7
|
expect(parseEndpointReturnValue(value)).toEqual({
|
|
7
8
|
value,
|
|
9
|
+
status: 'unset',
|
|
10
|
+
contentType: 'text/plain',
|
|
11
|
+
})
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('returns number as plain text data', () => {
|
|
15
|
+
const value = 100
|
|
16
|
+
expect(parseEndpointReturnValue(value)).toEqual({
|
|
17
|
+
value: '100',
|
|
18
|
+
status: 'unset',
|
|
8
19
|
contentType: 'text/plain',
|
|
9
20
|
})
|
|
10
21
|
})
|
|
@@ -13,6 +24,7 @@ describe('parseEndpointReturnValue', () => {
|
|
|
13
24
|
const value = Buffer.from('foo')
|
|
14
25
|
expect(parseEndpointReturnValue(value)).toEqual({
|
|
15
26
|
value,
|
|
27
|
+
status: 'unset',
|
|
16
28
|
contentType: 'application/octet-stream',
|
|
17
29
|
})
|
|
18
30
|
})
|
|
@@ -21,15 +33,50 @@ describe('parseEndpointReturnValue', () => {
|
|
|
21
33
|
const value = { foo: 'bar' }
|
|
22
34
|
expect(parseEndpointReturnValue(value)).toEqual({
|
|
23
35
|
value: JSON.stringify(value),
|
|
36
|
+
status: 'unset',
|
|
24
37
|
contentType: 'application/json; charset=utf-8',
|
|
25
38
|
})
|
|
26
39
|
})
|
|
27
40
|
|
|
28
|
-
it('
|
|
29
|
-
const value = {
|
|
41
|
+
it('parses useReturnValue with string value correctly', () => {
|
|
42
|
+
const value: ReturnType<typeof useReturnValue> = {
|
|
43
|
+
_isUseReturnValue: true,
|
|
30
44
|
value: 'foo',
|
|
45
|
+
status: 418,
|
|
31
46
|
contentType: 'application/custom',
|
|
32
47
|
}
|
|
33
|
-
expect(parseEndpointReturnValue(value)).toEqual(
|
|
48
|
+
expect(parseEndpointReturnValue(value)).toEqual({
|
|
49
|
+
value: 'foo',
|
|
50
|
+
status: 418,
|
|
51
|
+
contentType: 'application/custom',
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('parses useReturnValue with number value correctly', () => {
|
|
56
|
+
const value: ReturnType<typeof useReturnValue> = {
|
|
57
|
+
_isUseReturnValue: true,
|
|
58
|
+
value: 100,
|
|
59
|
+
status: 418,
|
|
60
|
+
contentType: 'application/custom',
|
|
61
|
+
}
|
|
62
|
+
expect(parseEndpointReturnValue(value)).toEqual({
|
|
63
|
+
value: '100',
|
|
64
|
+
status: 418,
|
|
65
|
+
contentType: 'application/custom',
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('parses useReturnValue with Buffer value correctly', () => {
|
|
70
|
+
const value: ReturnType<typeof useReturnValue> = {
|
|
71
|
+
_isUseReturnValue: true,
|
|
72
|
+
value: Buffer.from('foo'),
|
|
73
|
+
status: 418,
|
|
74
|
+
contentType: 'application/custom',
|
|
75
|
+
}
|
|
76
|
+
expect(parseEndpointReturnValue(value)).toEqual({
|
|
77
|
+
value: Buffer.from('foo'),
|
|
78
|
+
status: 418,
|
|
79
|
+
contentType: 'application/custom',
|
|
80
|
+
})
|
|
34
81
|
})
|
|
35
82
|
})
|
|
@@ -2,30 +2,37 @@ export const parseEndpointReturnValue = (response: unknown) => {
|
|
|
2
2
|
if (
|
|
3
3
|
typeof response === 'object' &&
|
|
4
4
|
response &&
|
|
5
|
+
'_isUseReturnValue' in response &&
|
|
5
6
|
'value' in response &&
|
|
6
|
-
|
|
7
|
+
'status' in response &&
|
|
7
8
|
'contentType' in response &&
|
|
9
|
+
response['_isUseReturnValue'] === true &&
|
|
10
|
+
typeof response['status'] === 'number' &&
|
|
8
11
|
typeof response['contentType'] === 'string'
|
|
9
12
|
) {
|
|
10
13
|
return {
|
|
11
|
-
value: response.value,
|
|
14
|
+
value: Buffer.isBuffer(response.value) ? response.value : String(response.value),
|
|
15
|
+
status: response.status,
|
|
12
16
|
contentType: response.contentType,
|
|
13
17
|
}
|
|
14
18
|
}
|
|
15
|
-
if (
|
|
19
|
+
if (['string', 'boolean', 'number', 'bigint', 'symbol'].includes(typeof response)) {
|
|
16
20
|
return {
|
|
17
|
-
value: response,
|
|
21
|
+
value: String(response),
|
|
22
|
+
status: 'unset' as const,
|
|
18
23
|
contentType: 'text/plain',
|
|
19
24
|
}
|
|
20
25
|
}
|
|
21
26
|
if (Buffer.isBuffer(response)) {
|
|
22
27
|
return {
|
|
23
28
|
value: response,
|
|
29
|
+
status: 'unset' as const,
|
|
24
30
|
contentType: 'application/octet-stream',
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
33
|
return {
|
|
28
34
|
value: JSON.stringify(response, (_, value) => (typeof value === 'bigint' ? value.toString() : value)),
|
|
35
|
+
status: 'unset' as const,
|
|
29
36
|
contentType: 'application/json; charset=utf-8',
|
|
30
37
|
}
|
|
31
38
|
}
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
NonEmptyStringValidator,
|
|
5
5
|
UnauthorizedError,
|
|
6
6
|
useQueryParams,
|
|
7
|
+
useReturnValue,
|
|
7
8
|
} from '..'
|
|
8
9
|
import { Router as RenamedRouter } from '../router/Router'
|
|
9
10
|
|
|
@@ -67,6 +68,14 @@ myRouter.get('/test/get/bigint', () => {
|
|
|
67
68
|
}
|
|
68
69
|
})
|
|
69
70
|
|
|
71
|
+
myRouter.get('/test/get/useReturnValue', () => {
|
|
72
|
+
return useReturnValue('foo', 418, 'text/custom')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
myRouter.get('/test/get/useReturnValue/buffer', () => {
|
|
76
|
+
return useReturnValue(Buffer.from('foo'), 418, 'text/custom')
|
|
77
|
+
})
|
|
78
|
+
|
|
70
79
|
myRouter.get('/test/get/middleware-data', (ctx) => {
|
|
71
80
|
return {
|
|
72
81
|
user: ctx.user,
|
package/src/test/app.spec.ts
CHANGED
|
@@ -81,6 +81,20 @@ describe('TestAppRouter', () => {
|
|
|
81
81
|
expect(response.text).toBe(JSON.stringify({ foo: '100' }))
|
|
82
82
|
})
|
|
83
83
|
|
|
84
|
+
it('handles useReturnValue return value correctly', async () => {
|
|
85
|
+
const response = await request(app.callback()).get('/test/get/useReturnValue')
|
|
86
|
+
expect(response.text).toBe('foo')
|
|
87
|
+
expect(response.status).toBe(418)
|
|
88
|
+
expect(response.header['content-type']).toEqual('text/custom')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('handles useReturnValue binary return value correctly', async () => {
|
|
92
|
+
const response = await request(app.callback()).get('/test/get/useReturnValue')
|
|
93
|
+
expect(response.text).toBe('foo')
|
|
94
|
+
expect(response.status).toBe(418)
|
|
95
|
+
expect(response.header['content-type']).toEqual('text/custom')
|
|
96
|
+
})
|
|
97
|
+
|
|
84
98
|
it('includes middleware data from context', async () => {
|
|
85
99
|
const response = await request(app.callback()).get('/test/get/middleware-data')
|
|
86
100
|
expect(response.status).toBe(200)
|