moonflower 0.9.0
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/.eslintrc.js +26 -0
- package/.prettierrc.js +7 -0
- package/README.md +383 -0
- package/cli/cli.ts +59 -0
- package/cli/entry.cjs +3 -0
- package/cli/prettyprint.ts +16 -0
- package/dist/cli/cli.d.ts +2 -0
- package/dist/cli/cli.d.ts.map +1 -0
- package/dist/cli/cli.js +79 -0
- package/dist/cli/prettyprint.d.ts +4 -0
- package/dist/cli/prettyprint.d.ts.map +1 -0
- package/dist/cli/prettyprint.js +18 -0
- package/dist/src/errors/BaseHttpError.d.ts +13 -0
- package/dist/src/errors/BaseHttpError.d.ts.map +1 -0
- package/dist/src/errors/BaseHttpError.js +13 -0
- package/dist/src/errors/HttpErrorHandler.d.ts +3 -0
- package/dist/src/errors/HttpErrorHandler.d.ts.map +1 -0
- package/dist/src/errors/HttpErrorHandler.js +23 -0
- package/dist/src/errors/UserFacingErrors.d.ts +11 -0
- package/dist/src/errors/UserFacingErrors.d.ts.map +1 -0
- package/dist/src/errors/UserFacingErrors.js +23 -0
- package/dist/src/hooks/authentication/useAuth.d.ts +3 -0
- package/dist/src/hooks/authentication/useAuth.d.ts.map +1 -0
- package/dist/src/hooks/authentication/useAuth.js +7 -0
- package/dist/src/hooks/authentication/useOptionalAuth.d.ts +3 -0
- package/dist/src/hooks/authentication/useOptionalAuth.d.ts.map +1 -0
- package/dist/src/hooks/authentication/useOptionalAuth.js +16 -0
- package/dist/src/hooks/useApiEndpoint.d.ts +8 -0
- package/dist/src/hooks/useApiEndpoint.d.ts.map +1 -0
- package/dist/src/hooks/useApiEndpoint.js +7 -0
- package/dist/src/hooks/useApiHeader/index.d.ts +2 -0
- package/dist/src/hooks/useApiHeader/index.d.ts.map +1 -0
- package/dist/src/hooks/useApiHeader/index.js +17 -0
- package/dist/src/hooks/useApiHeader/useApiHeader.d.ts +3 -0
- package/dist/src/hooks/useApiHeader/useApiHeader.d.ts.map +1 -0
- package/dist/src/hooks/useApiHeader/useApiHeader.js +6 -0
- package/dist/src/hooks/useApiHeader/useApiHeader.spec.data.d.ts +2 -0
- package/dist/src/hooks/useApiHeader/useApiHeader.spec.data.d.ts.map +1 -0
- package/dist/src/hooks/useApiHeader/useApiHeader.spec.data.js +22 -0
- package/dist/src/hooks/useCookieParams.d.ts +9 -0
- package/dist/src/hooks/useCookieParams.d.ts.map +1 -0
- package/dist/src/hooks/useCookieParams.js +50 -0
- package/dist/src/hooks/useExposeApiModel/index.d.ts +2 -0
- package/dist/src/hooks/useExposeApiModel/index.d.ts.map +1 -0
- package/dist/src/hooks/useExposeApiModel/index.js +17 -0
- package/dist/src/hooks/useExposeApiModel/useExposeApiModel.d.ts +3 -0
- package/dist/src/hooks/useExposeApiModel/useExposeApiModel.d.ts.map +1 -0
- package/dist/src/hooks/useExposeApiModel/useExposeApiModel.js +9 -0
- package/dist/src/hooks/useExposeApiModel/useExposeApiModel.spec.data.d.ts +2 -0
- package/dist/src/hooks/useExposeApiModel/useExposeApiModel.spec.data.d.ts.map +1 -0
- package/dist/src/hooks/useExposeApiModel/useExposeApiModel.spec.data.js +16 -0
- package/dist/src/hooks/useHeaderParams.d.ts +12 -0
- package/dist/src/hooks/useHeaderParams.d.ts.map +1 -0
- package/dist/src/hooks/useHeaderParams.js +52 -0
- package/dist/src/hooks/usePathParams.d.ts +22 -0
- package/dist/src/hooks/usePathParams.d.ts.map +1 -0
- package/dist/src/hooks/usePathParams.js +46 -0
- package/dist/src/hooks/useQueryParams.d.ts +9 -0
- package/dist/src/hooks/useQueryParams.d.ts.map +1 -0
- package/dist/src/hooks/useQueryParams.js +50 -0
- package/dist/src/hooks/useRequestBody.d.ts +9 -0
- package/dist/src/hooks/useRequestBody.d.ts.map +1 -0
- package/dist/src/hooks/useRequestBody.js +59 -0
- package/dist/src/hooks/useRequestRawBody.d.ts +7 -0
- package/dist/src/hooks/useRequestRawBody.d.ts.map +1 -0
- package/dist/src/hooks/useRequestRawBody.js +34 -0
- package/dist/src/index.d.ts +18 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +33 -0
- package/dist/src/openapi/analyzerModule/analyzerModule.d.ts +18 -0
- package/dist/src/openapi/analyzerModule/analyzerModule.d.ts.map +1 -0
- package/dist/src/openapi/analyzerModule/analyzerModule.js +192 -0
- package/dist/src/openapi/analyzerModule/nodeParsers.d.ts +19 -0
- package/dist/src/openapi/analyzerModule/nodeParsers.d.ts.map +1 -0
- package/dist/src/openapi/analyzerModule/nodeParsers.js +521 -0
- package/dist/src/openapi/analyzerModule/parseEndpoint.d.ts +4 -0
- package/dist/src/openapi/analyzerModule/parseEndpoint.d.ts.map +1 -0
- package/dist/src/openapi/analyzerModule/parseEndpoint.js +246 -0
- package/dist/src/openapi/analyzerModule/parseExposedModels.d.ts +5 -0
- package/dist/src/openapi/analyzerModule/parseExposedModels.d.ts.map +1 -0
- package/dist/src/openapi/analyzerModule/parseExposedModels.js +32 -0
- package/dist/src/openapi/analyzerModule/test/openApiAnalyzer.spec.data.d.ts +2 -0
- package/dist/src/openapi/analyzerModule/test/openApiAnalyzer.spec.data.d.ts.map +1 -0
- package/dist/src/openapi/analyzerModule/test/openApiAnalyzer.spec.data.js +400 -0
- package/dist/src/openapi/analyzerModule/types.d.ts +53 -0
- package/dist/src/openapi/analyzerModule/types.d.ts.map +1 -0
- package/dist/src/openapi/analyzerModule/types.js +2 -0
- package/dist/src/openapi/discoveryModule/discoverImports/discoverImports.d.ts +8 -0
- package/dist/src/openapi/discoveryModule/discoverImports/discoverImports.d.ts.map +1 -0
- package/dist/src/openapi/discoveryModule/discoverImports/discoverImports.js +33 -0
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterA.spec.data.d.ts +4 -0
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterA.spec.data.d.ts.map +1 -0
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterA.spec.data.js +8 -0
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterB.spec.data.d.ts +4 -0
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterB.spec.data.d.ts.map +1 -0
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterB.spec.data.js +8 -0
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/discoverRouterFiles.d.ts +17 -0
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/discoverRouterFiles.d.ts.map +1 -0
- package/dist/src/openapi/discoveryModule/discoverRouterFiles/discoverRouterFiles.js +80 -0
- package/dist/src/openapi/discoveryModule/discoverRouters/discoverRouters.d.ts +6 -0
- package/dist/src/openapi/discoveryModule/discoverRouters/discoverRouters.d.ts.map +1 -0
- package/dist/src/openapi/discoveryModule/discoverRouters/discoverRouters.js +31 -0
- package/dist/src/openapi/discoveryModule/discoverRouters/discoverRouters.spec.data.d.ts +5 -0
- package/dist/src/openapi/discoveryModule/discoverRouters/discoverRouters.spec.data.d.ts.map +1 -0
- package/dist/src/openapi/discoveryModule/discoverRouters/discoverRouters.spec.data.js +38 -0
- package/dist/src/openapi/discoveryModule/index.d.ts +3 -0
- package/dist/src/openapi/discoveryModule/index.d.ts.map +1 -0
- package/dist/src/openapi/discoveryModule/index.js +18 -0
- package/dist/src/openapi/generatorModule/generateComponentSchemas.d.ts +4 -0
- package/dist/src/openapi/generatorModule/generateComponentSchemas.d.ts.map +1 -0
- package/dist/src/openapi/generatorModule/generateComponentSchemas.js +12 -0
- package/dist/src/openapi/generatorModule/generatePaths.d.ts +10 -0
- package/dist/src/openapi/generatorModule/generatePaths.d.ts.map +1 -0
- package/dist/src/openapi/generatorModule/generatePaths.js +116 -0
- package/dist/src/openapi/generatorModule/generatorModule.d.ts +16 -0
- package/dist/src/openapi/generatorModule/generatorModule.d.ts.map +1 -0
- package/dist/src/openapi/generatorModule/generatorModule.js +18 -0
- package/dist/src/openapi/generatorModule/getSchema.d.ts +36 -0
- package/dist/src/openapi/generatorModule/getSchema.d.ts.map +1 -0
- package/dist/src/openapi/generatorModule/getSchema.js +133 -0
- package/dist/src/openapi/generatorModule/index.d.ts +5 -0
- package/dist/src/openapi/generatorModule/index.d.ts.map +1 -0
- package/dist/src/openapi/generatorModule/index.js +20 -0
- package/dist/src/openapi/generatorModule/test/openApiGenerator.spec.data.d.ts +515 -0
- package/dist/src/openapi/generatorModule/test/openApiGenerator.spec.data.d.ts.map +1 -0
- package/dist/src/openapi/generatorModule/test/openApiGenerator.spec.data.js +1119 -0
- package/dist/src/openapi/initOpenApiEngine.d.ts +4 -0
- package/dist/src/openapi/initOpenApiEngine.d.ts.map +1 -0
- package/dist/src/openapi/initOpenApiEngine.js +14 -0
- package/dist/src/openapi/manager/OpenApiManager.d.ts +67 -0
- package/dist/src/openapi/manager/OpenApiManager.d.ts.map +1 -0
- package/dist/src/openapi/manager/OpenApiManager.js +86 -0
- package/dist/src/openapi/router/OpenApiRouter.d.ts +4 -0
- package/dist/src/openapi/router/OpenApiRouter.d.ts.map +1 -0
- package/dist/src/openapi/router/OpenApiRouter.js +11 -0
- package/dist/src/openapi/types.d.ts +81 -0
- package/dist/src/openapi/types.d.ts.map +1 -0
- package/dist/src/openapi/types.js +2 -0
- package/dist/src/router/Router.d.ts +23 -0
- package/dist/src/router/Router.d.ts.map +1 -0
- package/dist/src/router/Router.js +81 -0
- package/dist/src/router/responseValueToJson.d.ts +2 -0
- package/dist/src/router/responseValueToJson.d.ts.map +1 -0
- package/dist/src/router/responseValueToJson.js +10 -0
- package/dist/src/setupTests.d.ts +1 -0
- package/dist/src/setupTests.d.ts.map +1 -0
- package/dist/src/setupTests.js +3 -0
- package/dist/src/test/TestAppRouter.d.ts +8 -0
- package/dist/src/test/TestAppRouter.d.ts.map +1 -0
- package/dist/src/test/TestAppRouter.js +58 -0
- package/dist/src/test/app.d.ts +3 -0
- package/dist/src/test/app.d.ts.map +1 -0
- package/dist/src/test/app.js +41 -0
- package/dist/src/utils/TypeUtils.d.ts +22 -0
- package/dist/src/utils/TypeUtils.d.ts.map +1 -0
- package/dist/src/utils/TypeUtils.js +2 -0
- package/dist/src/utils/fromZodSchema.d.ts +2 -0
- package/dist/src/utils/fromZodSchema.d.ts.map +1 -0
- package/dist/src/utils/fromZodSchema.js +6 -0
- package/dist/src/utils/loadTestData.d.ts +2 -0
- package/dist/src/utils/loadTestData.d.ts.map +1 -0
- package/dist/src/utils/loadTestData.js +39 -0
- package/dist/src/utils/mockContext.d.ts +20 -0
- package/dist/src/utils/mockContext.d.ts.map +1 -0
- package/dist/src/utils/mockContext.js +85 -0
- package/dist/src/utils/nameOf.d.ts +5 -0
- package/dist/src/utils/nameOf.d.ts.map +1 -0
- package/dist/src/utils/nameOf.js +7 -0
- package/dist/src/utils/object.d.ts +7 -0
- package/dist/src/utils/object.d.ts.map +1 -0
- package/dist/src/utils/object.js +21 -0
- package/dist/src/utils/printers.d.ts +6 -0
- package/dist/src/utils/printers.d.ts.map +1 -0
- package/dist/src/utils/printers.js +76 -0
- package/dist/src/utils/validationMessages.d.ts +18 -0
- package/dist/src/utils/validationMessages.d.ts.map +1 -0
- package/dist/src/utils/validationMessages.js +43 -0
- package/dist/src/validators/BuiltInValidators.d.ts +61 -0
- package/dist/src/validators/BuiltInValidators.d.ts.map +1 -0
- package/dist/src/validators/BuiltInValidators.js +66 -0
- package/dist/src/validators/InternalParamWrappers.d.ts +5 -0
- package/dist/src/validators/InternalParamWrappers.d.ts.map +1 -0
- package/dist/src/validators/InternalParamWrappers.js +5 -0
- package/dist/src/validators/ParamWrappers.d.ts +11 -0
- package/dist/src/validators/ParamWrappers.d.ts.map +1 -0
- package/dist/src/validators/ParamWrappers.js +9 -0
- package/dist/src/validators/types.d.ts +18 -0
- package/dist/src/validators/types.d.ts.map +1 -0
- package/dist/src/validators/types.js +2 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +59 -0
- package/src/errors/BaseHttpError.ts +16 -0
- package/src/errors/HttpErrorHandler.ts +20 -0
- package/src/errors/UserFacingErrors.ts +39 -0
- package/src/hooks/authentication/useAuth.ts +8 -0
- package/src/hooks/authentication/useOptionalAuth.ts +17 -0
- package/src/hooks/useApiEndpoint.spec.ts +11 -0
- package/src/hooks/useApiEndpoint.ts +10 -0
- package/src/hooks/useApiHeader/index.ts +1 -0
- package/src/hooks/useApiHeader/useApiHeader.spec.data.ts +22 -0
- package/src/hooks/useApiHeader/useApiHeader.spec.ts +34 -0
- package/src/hooks/useApiHeader/useApiHeader.ts +6 -0
- package/src/hooks/useCookieParams.spec.ts +174 -0
- package/src/hooks/useCookieParams.ts +73 -0
- package/src/hooks/useExposeApiModel/index.ts +1 -0
- package/src/hooks/useExposeApiModel/useExposeApiModel.spec.data.ts +48 -0
- package/src/hooks/useExposeApiModel/useExposeApiModel.spec.ts +388 -0
- package/src/hooks/useExposeApiModel/useExposeApiModel.ts +9 -0
- package/src/hooks/useHeaderParams.spec.ts +186 -0
- package/src/hooks/useHeaderParams.ts +83 -0
- package/src/hooks/usePathParams.spec.ts +161 -0
- package/src/hooks/usePathParams.ts +89 -0
- package/src/hooks/useQueryParams.spec.ts +224 -0
- package/src/hooks/useQueryParams.ts +73 -0
- package/src/hooks/useRequestBody.spec.ts +215 -0
- package/src/hooks/useRequestBody.ts +94 -0
- package/src/hooks/useRequestRawBody.spec.ts +154 -0
- package/src/hooks/useRequestRawBody.ts +56 -0
- package/src/index.ts +17 -0
- package/src/openapi/analyzerModule/analyzerModule.ts +228 -0
- package/src/openapi/analyzerModule/nodeParsers.ts +648 -0
- package/src/openapi/analyzerModule/parseEndpoint.ts +305 -0
- package/src/openapi/analyzerModule/parseExposedModels.ts +34 -0
- package/src/openapi/analyzerModule/test/openApiAnalyzer.spec.data.ts +521 -0
- package/src/openapi/analyzerModule/test/openApiAnalyzer.spec.ts +1043 -0
- package/src/openapi/analyzerModule/types.ts +72 -0
- package/src/openapi/discoveryModule/discoverImports/discoverImports.ts +43 -0
- package/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterA.spec.data.ts +7 -0
- package/src/openapi/discoveryModule/discoverRouterFiles/data/testRouterB.spec.data.ts +7 -0
- package/src/openapi/discoveryModule/discoverRouterFiles/discoverRouterFiles.spec.ts +36 -0
- package/src/openapi/discoveryModule/discoverRouterFiles/discoverRouterFiles.ts +80 -0
- package/src/openapi/discoveryModule/discoverRouters/discoverRouters.spec.data.ts +42 -0
- package/src/openapi/discoveryModule/discoverRouters/discoverRouters.spec.ts +18 -0
- package/src/openapi/discoveryModule/discoverRouters/discoverRouters.ts +39 -0
- package/src/openapi/discoveryModule/index.ts +2 -0
- package/src/openapi/generatorModule/generateComponentSchemas.ts +12 -0
- package/src/openapi/generatorModule/generatePaths.ts +138 -0
- package/src/openapi/generatorModule/generatorModule.ts +17 -0
- package/src/openapi/generatorModule/getSchema.ts +169 -0
- package/src/openapi/generatorModule/index.ts +4 -0
- package/src/openapi/generatorModule/test/openApiGenerator.spec.data.ts +1119 -0
- package/src/openapi/generatorModule/test/openApiGenerator.spec.ts +783 -0
- package/src/openapi/initOpenApiEngine.ts +20 -0
- package/src/openapi/manager/OpenApiManager.ts +153 -0
- package/src/openapi/router/OpenApiRouter.ts +11 -0
- package/src/openapi/types.ts +86 -0
- package/src/router/Router.ts +123 -0
- package/src/router/responseValueToJson.ts +6 -0
- package/src/setupTests.ts +3 -0
- package/src/test/TestAppRouter.ts +76 -0
- package/src/test/app.spec.ts +130 -0
- package/src/test/app.ts +43 -0
- package/src/utils/TypeUtils.ts +51 -0
- package/src/utils/loadTestData.ts +15 -0
- package/src/utils/mockContext.ts +86 -0
- package/src/utils/nameOf.ts +7 -0
- package/src/utils/object.spec.ts +27 -0
- package/src/utils/object.ts +17 -0
- package/src/utils/printers.spec.ts +103 -0
- package/src/utils/printers.ts +49 -0
- package/src/utils/validationMessages.ts +65 -0
- package/src/validators/BuiltInValidators.ts +64 -0
- package/src/validators/InternalParamWrappers.ts +14 -0
- package/src/validators/ParamWrappers.ts +22 -0
- package/src/validators/types.ts +35 -0
- package/tsconfig.build.json +15 -0
- package/tsconfig.json +29 -0
- package/vite.config.ts +16 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
import { Node, SyntaxKind, ts } from 'ts-morph'
|
|
3
|
+
|
|
4
|
+
import { ApiEndpointDocs } from '../../hooks/useApiEndpoint'
|
|
5
|
+
import { EndpointData } from '../types'
|
|
6
|
+
import {
|
|
7
|
+
findNodeImplementation,
|
|
8
|
+
getProperTypeShape,
|
|
9
|
+
getShapeOfValidatorLiteral,
|
|
10
|
+
getValidatorPropertyOptionality,
|
|
11
|
+
getValidatorPropertyShape,
|
|
12
|
+
getValidatorPropertyStringValue,
|
|
13
|
+
getValuesOfObjectLiteral,
|
|
14
|
+
} from './nodeParsers'
|
|
15
|
+
|
|
16
|
+
export const parseEndpoint = (node: Node<ts.Node>, sourceFilePath: string) => {
|
|
17
|
+
const parsedEndpointMethod = node
|
|
18
|
+
.getFirstDescendantByKind(SyntaxKind.PropertyAccessExpression)!
|
|
19
|
+
.getText()
|
|
20
|
+
.split('.')[1]
|
|
21
|
+
.toUpperCase()
|
|
22
|
+
|
|
23
|
+
const endpointMethod = parsedEndpointMethod === 'DEL' ? 'DELETE' : parsedEndpointMethod
|
|
24
|
+
|
|
25
|
+
const endpointText = node.getFirstDescendantByKind(SyntaxKind.StringLiteral)!.getText() ?? ''
|
|
26
|
+
const endpointPath = endpointText.substring(1, endpointText.length - 1)
|
|
27
|
+
|
|
28
|
+
const endpointData: EndpointData = {
|
|
29
|
+
method: endpointMethod as 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
|
|
30
|
+
path: endpointPath,
|
|
31
|
+
sourceFilePath,
|
|
32
|
+
requestPathParams: [],
|
|
33
|
+
requestQuery: [],
|
|
34
|
+
requestHeaders: [],
|
|
35
|
+
rawBody: undefined,
|
|
36
|
+
objectBody: [],
|
|
37
|
+
responses: [],
|
|
38
|
+
name: undefined,
|
|
39
|
+
summary: undefined,
|
|
40
|
+
description: undefined,
|
|
41
|
+
tags: undefined,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const warningData: {
|
|
45
|
+
segment: string
|
|
46
|
+
error: Error
|
|
47
|
+
}[] = []
|
|
48
|
+
|
|
49
|
+
// API documentation
|
|
50
|
+
try {
|
|
51
|
+
const entries = parseApiDocumentation(node)
|
|
52
|
+
entries.forEach((param) => {
|
|
53
|
+
endpointData[param.identifier] = param.value as string & string[]
|
|
54
|
+
})
|
|
55
|
+
} catch (err) {
|
|
56
|
+
warningData.push({
|
|
57
|
+
segment: 'api',
|
|
58
|
+
error: err as Error,
|
|
59
|
+
})
|
|
60
|
+
console.error('Error', err)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Request params
|
|
64
|
+
try {
|
|
65
|
+
endpointData.requestPathParams = parseRequestParams(node, endpointPath)
|
|
66
|
+
} catch (err) {
|
|
67
|
+
warningData.push({
|
|
68
|
+
segment: 'path',
|
|
69
|
+
error: err as Error,
|
|
70
|
+
})
|
|
71
|
+
console.error('Error', err)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Request query
|
|
75
|
+
try {
|
|
76
|
+
endpointData.requestQuery = parseRequestObjectInput(node, 'useQueryParams')
|
|
77
|
+
} catch (err) {
|
|
78
|
+
warningData.push({
|
|
79
|
+
segment: 'query',
|
|
80
|
+
error: err as Error,
|
|
81
|
+
})
|
|
82
|
+
console.error('Error', err)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Request headers
|
|
86
|
+
try {
|
|
87
|
+
endpointData.requestHeaders = parseRequestObjectInput(node, 'useHeaderParams')
|
|
88
|
+
} catch (err) {
|
|
89
|
+
warningData.push({
|
|
90
|
+
segment: 'headers',
|
|
91
|
+
error: err as Error,
|
|
92
|
+
})
|
|
93
|
+
console.error('Error', err)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Raw request body
|
|
97
|
+
try {
|
|
98
|
+
const parsedBody = parseRequestRawBody(node)
|
|
99
|
+
if (parsedBody) {
|
|
100
|
+
endpointData.rawBody = parsedBody
|
|
101
|
+
}
|
|
102
|
+
} catch (err) {
|
|
103
|
+
warningData.push({
|
|
104
|
+
segment: 'rawBody',
|
|
105
|
+
error: err as Error,
|
|
106
|
+
})
|
|
107
|
+
console.error('Error', err)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Object request body
|
|
111
|
+
try {
|
|
112
|
+
endpointData.objectBody = parseRequestObjectInput(node, 'useRequestBody')
|
|
113
|
+
} catch (err) {
|
|
114
|
+
warningData.push({
|
|
115
|
+
segment: 'objectBody',
|
|
116
|
+
error: err as Error,
|
|
117
|
+
})
|
|
118
|
+
console.error('Error', err)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Request response
|
|
122
|
+
try {
|
|
123
|
+
endpointData.responses = parseRequestResponse(node)
|
|
124
|
+
} catch (err) {
|
|
125
|
+
warningData.push({
|
|
126
|
+
segment: 'response',
|
|
127
|
+
error: err as Error,
|
|
128
|
+
})
|
|
129
|
+
console.error('Error', err)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return endpointData
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const getHookNode = (
|
|
136
|
+
endpointNode: Node<ts.Node>,
|
|
137
|
+
hookName:
|
|
138
|
+
| 'useApiEndpoint'
|
|
139
|
+
| 'usePathParams'
|
|
140
|
+
| 'useQueryParams'
|
|
141
|
+
| 'useHeaderParams'
|
|
142
|
+
| 'useRequestBody'
|
|
143
|
+
| 'useRequestRawBody'
|
|
144
|
+
) => {
|
|
145
|
+
const callExpressions = endpointNode.getDescendantsOfKind(SyntaxKind.CallExpression)
|
|
146
|
+
const matchingCallExpressions = callExpressions.filter((node) => {
|
|
147
|
+
return node.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === hookName
|
|
148
|
+
})
|
|
149
|
+
return matchingCallExpressions[0] ?? null
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const parseApiDocumentation = (node: Node<ts.Node>) => {
|
|
153
|
+
const hookNode = getHookNode(node, 'useApiEndpoint')
|
|
154
|
+
if (!hookNode) {
|
|
155
|
+
return []
|
|
156
|
+
}
|
|
157
|
+
const paramNode = hookNode.getFirstChildByKind(SyntaxKind.SyntaxList)!
|
|
158
|
+
const valueNode = findNodeImplementation(paramNode.getLastChild()!)
|
|
159
|
+
|
|
160
|
+
if (!valueNode.isKind(SyntaxKind.ObjectLiteralExpression)) {
|
|
161
|
+
throw new Error('Non-literal type used in useApiEndpoint')
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const objectLiteral = valueNode.asKind(SyntaxKind.ObjectLiteralExpression)!
|
|
165
|
+
|
|
166
|
+
const values = getValuesOfObjectLiteral(objectLiteral).filter((param) => param.value !== null)
|
|
167
|
+
return values as {
|
|
168
|
+
identifier: keyof ApiEndpointDocs
|
|
169
|
+
value: typeof values[number]['value']
|
|
170
|
+
}[]
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const parseRequestParams = (node: Node<ts.Node>, endpointPath: string): EndpointData['requestPathParams'] => {
|
|
174
|
+
const hookNode = getHookNode(node, 'usePathParams')
|
|
175
|
+
if (!hookNode) {
|
|
176
|
+
return []
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const paramNode = hookNode.getFirstChildByKind(SyntaxKind.SyntaxList)!
|
|
180
|
+
const valueNode = findNodeImplementation(paramNode.getLastChild()!)
|
|
181
|
+
|
|
182
|
+
if (!valueNode.isKind(SyntaxKind.ObjectLiteralExpression)) {
|
|
183
|
+
throw new Error('Non-literal type used in usePathParams')
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const declaredParams = endpointPath
|
|
187
|
+
.split('/')
|
|
188
|
+
.filter((segment) => segment.startsWith(':'))
|
|
189
|
+
.map((segment) => ({
|
|
190
|
+
name: segment.substring(1).replace(/\?/, ''),
|
|
191
|
+
optional: segment.includes('?'),
|
|
192
|
+
}))
|
|
193
|
+
|
|
194
|
+
const objectLiteral = valueNode.asKind(SyntaxKind.ObjectLiteralExpression)!
|
|
195
|
+
return getShapeOfValidatorLiteral(objectLiteral)
|
|
196
|
+
.filter((param) => param.shape !== null)
|
|
197
|
+
.map((param) => ({
|
|
198
|
+
identifier: param.identifier,
|
|
199
|
+
signature: param.shape as string,
|
|
200
|
+
optional: declaredParams.some((declared) => declared.name === param.identifier && declared.optional),
|
|
201
|
+
description: param.description,
|
|
202
|
+
errorMessage: param.errorMessage,
|
|
203
|
+
}))
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const parseRequestRawBody = (node: Node<ts.Node>): NonNullable<EndpointData['rawBody']> | null => {
|
|
207
|
+
const hookNode = getHookNode(node, 'useRequestRawBody')
|
|
208
|
+
if (!hookNode) {
|
|
209
|
+
return null
|
|
210
|
+
}
|
|
211
|
+
const paramNode = hookNode.getFirstChildByKind(SyntaxKind.SyntaxList)!
|
|
212
|
+
const valueNode = findNodeImplementation(paramNode.getLastChild()!)
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
signature: getValidatorPropertyShape(valueNode),
|
|
216
|
+
optional: getValidatorPropertyOptionality(valueNode),
|
|
217
|
+
description: getValidatorPropertyStringValue(valueNode, 'description'),
|
|
218
|
+
errorMessage: getValidatorPropertyStringValue(valueNode, 'errorMessage'),
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const parseRequestObjectInput = (
|
|
223
|
+
node: Node<ts.Node>,
|
|
224
|
+
nodeName: 'useQueryParams' | 'useHeaderParams' | 'useRequestBody'
|
|
225
|
+
): EndpointData['requestQuery'] | EndpointData['objectBody'] => {
|
|
226
|
+
const hookNode = getHookNode(node, nodeName)
|
|
227
|
+
if (!hookNode) {
|
|
228
|
+
return []
|
|
229
|
+
}
|
|
230
|
+
const paramNode = hookNode.getFirstChildByKind(SyntaxKind.SyntaxList)!
|
|
231
|
+
const valueNode = findNodeImplementation(paramNode.getLastChild()!)
|
|
232
|
+
|
|
233
|
+
if (!valueNode.isKind(SyntaxKind.ObjectLiteralExpression)) {
|
|
234
|
+
throw new Error(`Non-literal type used in ${nodeName}`)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const objectLiteral = valueNode.asKind(SyntaxKind.ObjectLiteralExpression)!
|
|
238
|
+
return getShapeOfValidatorLiteral(objectLiteral)
|
|
239
|
+
.filter((param) => param.shape !== null)
|
|
240
|
+
.map((param) => ({
|
|
241
|
+
identifier: param.identifier,
|
|
242
|
+
signature: param.shape as string,
|
|
243
|
+
optional: param.optional,
|
|
244
|
+
description: param.description,
|
|
245
|
+
errorMessage: param.errorMessage,
|
|
246
|
+
}))
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const parseRequestResponse = (node: Node<ts.Node>): EndpointData['responses'] => {
|
|
250
|
+
const implementationNode = node
|
|
251
|
+
.getFirstChildByKind(SyntaxKind.CallExpression)!
|
|
252
|
+
.getFirstChildByKind(SyntaxKind.SyntaxList)!
|
|
253
|
+
.getFirstChildByKind(SyntaxKind.ArrowFunction)!
|
|
254
|
+
const returnType = implementationNode.getReturnType()
|
|
255
|
+
|
|
256
|
+
const actualType = (() => {
|
|
257
|
+
if (returnType.getText().startsWith('Promise')) {
|
|
258
|
+
return returnType.getTypeArguments()[0]
|
|
259
|
+
}
|
|
260
|
+
return returnType
|
|
261
|
+
})()
|
|
262
|
+
|
|
263
|
+
const responseType = getProperTypeShape(actualType, node)
|
|
264
|
+
|
|
265
|
+
// TODO: Add support for response descriptions and errors
|
|
266
|
+
if (typeof responseType === 'string') {
|
|
267
|
+
return [
|
|
268
|
+
{
|
|
269
|
+
status: responseType === 'void' || responseType === 'null' ? 204 : 200,
|
|
270
|
+
signature: responseType,
|
|
271
|
+
description: '',
|
|
272
|
+
errorMessage: '',
|
|
273
|
+
},
|
|
274
|
+
]
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (responseType[0].role === 'union') {
|
|
278
|
+
if (typeof responseType[0].shape === 'string') {
|
|
279
|
+
return [
|
|
280
|
+
{
|
|
281
|
+
status: responseType[0].shape === 'void' || responseType[0].shape === 'null' ? 204 : 200,
|
|
282
|
+
signature: responseType[0].shape,
|
|
283
|
+
description: '',
|
|
284
|
+
errorMessage: '',
|
|
285
|
+
},
|
|
286
|
+
]
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return responseType[0].shape.map((unionEntry) => ({
|
|
290
|
+
status: unionEntry.shape === 'void' || unionEntry.shape === 'null' ? 204 : 200,
|
|
291
|
+
signature: unionEntry.shape,
|
|
292
|
+
description: '',
|
|
293
|
+
errorMessage: '',
|
|
294
|
+
}))
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return [
|
|
298
|
+
{
|
|
299
|
+
status: 200,
|
|
300
|
+
signature: responseType,
|
|
301
|
+
description: '',
|
|
302
|
+
errorMessage: '',
|
|
303
|
+
},
|
|
304
|
+
]
|
|
305
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
import { Node, SyntaxKind, ts } from 'ts-morph'
|
|
3
|
+
|
|
4
|
+
import { ExposedModelData } from '../types'
|
|
5
|
+
import { getProperTypeShape } from './nodeParsers'
|
|
6
|
+
import { ShapeOfProperty } from './types'
|
|
7
|
+
|
|
8
|
+
export const parseExposedModel = (node: Node<ts.Node>): ExposedModelData => {
|
|
9
|
+
if (node.isKind(SyntaxKind.TypeReference) || node.isKind(SyntaxKind.TypeQuery)) {
|
|
10
|
+
const identifierNode = node.getFirstChildByKind(SyntaxKind.Identifier)!
|
|
11
|
+
const modelName = identifierNode.getText()
|
|
12
|
+
const modelShape = getProperTypeShape(node.getType(), node, [])
|
|
13
|
+
return {
|
|
14
|
+
name: modelName,
|
|
15
|
+
shape: modelShape,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
name: '',
|
|
21
|
+
shape: '',
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const parseNamedExposedModels = (node: Node<ts.Node>): ExposedModelData[] => {
|
|
26
|
+
if (node.isKind(SyntaxKind.TypeLiteral)) {
|
|
27
|
+
const shape = getProperTypeShape(node.getType(), node, []) as ShapeOfProperty[]
|
|
28
|
+
return shape.map((property) => ({
|
|
29
|
+
name: property.identifier,
|
|
30
|
+
shape: property.shape,
|
|
31
|
+
}))
|
|
32
|
+
}
|
|
33
|
+
return []
|
|
34
|
+
}
|