@declaro/core 2.0.0-y.0 → 2.1.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.
Files changed (266) hide show
  1. package/{LICENSE → LICENSE.md} +1 -1
  2. package/README.md +203 -0
  3. package/dist/browser/index.js +28 -0
  4. package/dist/browser/index.js.map +133 -0
  5. package/dist/browser/scope/index.js +3 -0
  6. package/dist/browser/scope/index.js.map +9 -0
  7. package/dist/bun/index.js +19011 -0
  8. package/dist/bun/index.js.map +132 -0
  9. package/dist/bun/scope/index.js +4 -0
  10. package/dist/bun/scope/index.js.map +9 -0
  11. package/dist/node/index.cjs +19039 -0
  12. package/dist/node/index.cjs.map +132 -0
  13. package/dist/node/index.js +19010 -0
  14. package/dist/node/index.js.map +132 -0
  15. package/dist/node/scope/index.cjs +69 -0
  16. package/dist/node/scope/index.cjs.map +9 -0
  17. package/dist/node/scope/index.js +3 -0
  18. package/dist/node/scope/index.js.map +9 -0
  19. package/dist/ts/app/app-context.d.ts +9 -0
  20. package/dist/ts/app/app-context.d.ts.map +1 -0
  21. package/dist/ts/app/app-lifecycle.d.ts +6 -0
  22. package/dist/ts/app/app-lifecycle.d.ts.map +1 -0
  23. package/dist/ts/app/app.d.ts +24 -0
  24. package/dist/ts/app/app.d.ts.map +1 -0
  25. package/dist/{app → ts/app}/index.d.ts +1 -0
  26. package/dist/ts/app/index.d.ts.map +1 -0
  27. package/dist/ts/application/create-request-context.d.ts +4 -0
  28. package/dist/ts/application/create-request-context.d.ts.map +1 -0
  29. package/dist/ts/application/create-request-context.test.d.ts +2 -0
  30. package/dist/ts/application/create-request-context.test.d.ts.map +1 -0
  31. package/dist/ts/application/use-declaro.d.ts +3 -0
  32. package/dist/ts/application/use-declaro.d.ts.map +1 -0
  33. package/dist/{auth → ts/auth}/permission-validator.d.ts +1 -0
  34. package/dist/ts/auth/permission-validator.d.ts.map +1 -0
  35. package/dist/ts/auth/permission-validator.test.d.ts +2 -0
  36. package/dist/ts/auth/permission-validator.test.d.ts.map +1 -0
  37. package/dist/ts/context/async-context.d.ts +54 -0
  38. package/dist/ts/context/async-context.d.ts.map +1 -0
  39. package/dist/ts/context/async-context.test.d.ts +2 -0
  40. package/dist/ts/context/async-context.test.d.ts.map +1 -0
  41. package/dist/{context → ts/context}/context-consumer.d.ts +4 -0
  42. package/dist/ts/context/context-consumer.d.ts.map +1 -0
  43. package/dist/ts/context/context.circular-deps.test.d.ts +2 -0
  44. package/dist/ts/context/context.circular-deps.test.d.ts.map +1 -0
  45. package/dist/ts/context/context.d.ts +452 -0
  46. package/dist/ts/context/context.d.ts.map +1 -0
  47. package/dist/ts/context/context.test.d.ts +2 -0
  48. package/dist/ts/context/context.test.d.ts.map +1 -0
  49. package/dist/ts/context/legacy-context.test.d.ts +2 -0
  50. package/dist/ts/context/legacy-context.test.d.ts.map +1 -0
  51. package/dist/{context → ts/context}/validators.d.ts +2 -1
  52. package/dist/ts/context/validators.d.ts.map +1 -0
  53. package/dist/ts/dataflow/index.d.ts +2 -0
  54. package/dist/ts/dataflow/index.d.ts.map +1 -0
  55. package/dist/ts/dataflow/objects.d.ts +7 -0
  56. package/dist/ts/dataflow/objects.d.ts.map +1 -0
  57. package/dist/ts/dataflow/objects.test.d.ts +2 -0
  58. package/dist/ts/dataflow/objects.test.d.ts.map +1 -0
  59. package/dist/{errors → ts/errors}/errors.d.ts +16 -3
  60. package/dist/ts/errors/errors.d.ts.map +1 -0
  61. package/dist/ts/events/event-manager.d.ts +19 -0
  62. package/dist/ts/events/event-manager.d.ts.map +1 -0
  63. package/dist/ts/events/event-manager.spec.d.ts +2 -0
  64. package/dist/ts/events/event-manager.spec.d.ts.map +1 -0
  65. package/dist/ts/events/index.d.ts +2 -0
  66. package/dist/ts/events/index.d.ts.map +1 -0
  67. package/dist/ts/http/headers.d.ts +21 -0
  68. package/dist/ts/http/headers.d.ts.map +1 -0
  69. package/dist/ts/http/headers.spec.d.ts +2 -0
  70. package/dist/ts/http/headers.spec.d.ts.map +1 -0
  71. package/dist/ts/http/request-context.d.ts +17 -0
  72. package/dist/ts/http/request-context.d.ts.map +1 -0
  73. package/dist/ts/http/request-context.spec.d.ts +2 -0
  74. package/dist/ts/http/request-context.spec.d.ts.map +1 -0
  75. package/dist/ts/http/request.d.ts +31 -0
  76. package/dist/ts/http/request.d.ts.map +1 -0
  77. package/dist/ts/http/request.spec.d.ts +2 -0
  78. package/dist/ts/http/request.spec.d.ts.map +1 -0
  79. package/dist/{http → ts/http}/url.d.ts +5 -4
  80. package/dist/ts/http/url.d.ts.map +1 -0
  81. package/dist/ts/http/url.spec.d.ts +2 -0
  82. package/dist/ts/http/url.spec.d.ts.map +1 -0
  83. package/dist/ts/index.d.ts +47 -0
  84. package/dist/ts/index.d.ts.map +1 -0
  85. package/dist/{pipelines → ts/pipelines}/index.d.ts +1 -0
  86. package/dist/ts/pipelines/index.d.ts.map +1 -0
  87. package/dist/{pipelines → ts/pipelines}/pipeline-action.d.ts +1 -0
  88. package/dist/ts/pipelines/pipeline-action.d.ts.map +1 -0
  89. package/dist/ts/pipelines/pipeline-action.test.d.ts +2 -0
  90. package/dist/ts/pipelines/pipeline-action.test.d.ts.map +1 -0
  91. package/dist/{pipelines → ts/pipelines}/pipeline.d.ts +3 -2
  92. package/dist/ts/pipelines/pipeline.d.ts.map +1 -0
  93. package/dist/ts/pipelines/pipeline.test.d.ts +2 -0
  94. package/dist/ts/pipelines/pipeline.test.d.ts.map +1 -0
  95. package/dist/ts/schema/json-schema.d.ts +12 -0
  96. package/dist/ts/schema/json-schema.d.ts.map +1 -0
  97. package/dist/ts/schema/labels.d.ts +14 -0
  98. package/dist/ts/schema/labels.d.ts.map +1 -0
  99. package/dist/ts/schema/model-schema.d.ts +75 -0
  100. package/dist/ts/schema/model-schema.d.ts.map +1 -0
  101. package/dist/ts/schema/model-schema.test.d.ts +2 -0
  102. package/dist/ts/schema/model-schema.test.d.ts.map +1 -0
  103. package/dist/ts/schema/model.d.ts +35 -0
  104. package/dist/ts/schema/model.d.ts.map +1 -0
  105. package/dist/ts/schema/schema-mixin.d.ts +24 -0
  106. package/dist/ts/schema/schema-mixin.d.ts.map +1 -0
  107. package/dist/ts/schema/test/mock-model.d.ts +8 -0
  108. package/dist/ts/schema/test/mock-model.d.ts.map +1 -0
  109. package/dist/ts/scope/index.d.ts +34 -0
  110. package/dist/ts/scope/index.d.ts.map +1 -0
  111. package/dist/ts/shared/utils/action-descriptor.d.ts +28 -0
  112. package/dist/ts/shared/utils/action-descriptor.d.ts.map +1 -0
  113. package/dist/ts/shared/utils/action-descriptor.test.d.ts +2 -0
  114. package/dist/ts/shared/utils/action-descriptor.test.d.ts.map +1 -0
  115. package/dist/ts/shared/utils/schema-utils.d.ts +3 -0
  116. package/dist/ts/shared/utils/schema-utils.d.ts.map +1 -0
  117. package/dist/ts/shared/utils/schema-utils.test.d.ts +2 -0
  118. package/dist/ts/shared/utils/schema-utils.test.d.ts.map +1 -0
  119. package/dist/ts/shims/async-local-storage.d.ts +36 -0
  120. package/dist/ts/shims/async-local-storage.d.ts.map +1 -0
  121. package/dist/ts/shims/async-local-storage.test.d.ts +2 -0
  122. package/dist/ts/shims/async-local-storage.test.d.ts.map +1 -0
  123. package/dist/{timing.d.ts → ts/timing.d.ts} +1 -0
  124. package/dist/ts/timing.d.ts.map +1 -0
  125. package/dist/{typescript → ts/typescript}/arrays.d.ts +1 -0
  126. package/dist/ts/typescript/arrays.d.ts.map +1 -0
  127. package/dist/{typescript → ts/typescript}/baseModel.d.ts +1 -0
  128. package/dist/ts/typescript/baseModel.d.ts.map +1 -0
  129. package/dist/{typescript → ts/typescript}/classes.d.ts +1 -0
  130. package/dist/ts/typescript/classes.d.ts.map +1 -0
  131. package/dist/{typescript → ts/typescript}/constant-manipulation/snake-case.d.ts +1 -0
  132. package/dist/ts/typescript/constant-manipulation/snake-case.d.ts.map +1 -0
  133. package/dist/{typescript → ts/typescript}/errors.d.ts +1 -0
  134. package/dist/ts/typescript/errors.d.ts.map +1 -0
  135. package/dist/ts/typescript/fetch.d.ts +3 -0
  136. package/dist/ts/typescript/fetch.d.ts.map +1 -0
  137. package/dist/{typescript → ts/typescript}/generics.d.ts +1 -0
  138. package/dist/ts/typescript/generics.d.ts.map +1 -0
  139. package/dist/{typescript → ts/typescript}/index.d.ts +1 -0
  140. package/dist/ts/typescript/index.d.ts.map +1 -0
  141. package/dist/ts/typescript/objects.d.ts +26 -0
  142. package/dist/ts/typescript/objects.d.ts.map +1 -0
  143. package/dist/{typescript → ts/typescript}/promises.d.ts +1 -0
  144. package/dist/ts/typescript/promises.d.ts.map +1 -0
  145. package/dist/{validation → ts/validation}/index.d.ts +1 -0
  146. package/dist/ts/validation/index.d.ts.map +1 -0
  147. package/dist/{validation → ts/validation}/validation.d.ts +1 -0
  148. package/dist/ts/validation/validation.d.ts.map +1 -0
  149. package/dist/{validation → ts/validation}/validator.d.ts +1 -0
  150. package/dist/ts/validation/validator.d.ts.map +1 -0
  151. package/dist/ts/validation/validator.test.d.ts +2 -0
  152. package/dist/ts/validation/validator.test.d.ts.map +1 -0
  153. package/package.json +46 -13
  154. package/src/app/app-context.ts +4 -5
  155. package/src/app/app-lifecycle.ts +4 -3
  156. package/src/app/app.ts +7 -5
  157. package/src/application/create-request-context.test.ts +345 -0
  158. package/src/application/create-request-context.ts +19 -0
  159. package/src/application/use-declaro.ts +27 -0
  160. package/src/auth/permission-validator.test.ts +238 -2
  161. package/src/auth/permission-validator.ts +3 -3
  162. package/src/context/async-context.test.ts +348 -0
  163. package/src/context/async-context.ts +129 -0
  164. package/src/context/context-consumer.ts +4 -4
  165. package/src/context/context.circular-deps.test.ts +1047 -0
  166. package/src/context/context.test.ts +420 -3
  167. package/src/context/context.ts +590 -87
  168. package/src/context/legacy-context.test.ts +9 -9
  169. package/src/dataflow/objects.test.ts +7 -7
  170. package/src/dataflow/objects.ts +10 -9
  171. package/src/errors/errors.ts +19 -3
  172. package/src/events/event-manager.spec.ts +129 -0
  173. package/src/events/event-manager.ts +25 -14
  174. package/src/http/headers.ts +17 -2
  175. package/src/http/request-context.ts +24 -15
  176. package/src/http/request.ts +27 -6
  177. package/src/http/url.ts +3 -3
  178. package/src/index.ts +34 -3
  179. package/src/pipelines/pipeline.test.ts +11 -9
  180. package/src/schema/json-schema.ts +16 -0
  181. package/src/schema/labels.ts +23 -23
  182. package/src/schema/model-schema.test.ts +282 -0
  183. package/src/schema/model-schema.ts +197 -0
  184. package/src/schema/model.ts +143 -0
  185. package/src/schema/schema-mixin.ts +51 -0
  186. package/src/schema/test/mock-model.ts +19 -0
  187. package/src/scope/index.ts +33 -0
  188. package/src/shared/utils/action-descriptor.test.ts +182 -0
  189. package/src/shared/utils/action-descriptor.ts +102 -0
  190. package/src/shared/utils/schema-utils.test.ts +33 -0
  191. package/src/shared/utils/schema-utils.ts +17 -0
  192. package/src/shims/async-local-storage.test.ts +258 -0
  193. package/src/shims/async-local-storage.ts +82 -0
  194. package/src/typescript/objects.ts +32 -1
  195. package/src/validation/validator.test.ts +12 -20
  196. package/dist/app/app-context.d.ts +0 -8
  197. package/dist/app/app-lifecycle.d.ts +0 -4
  198. package/dist/app/app.d.ts +0 -22
  199. package/dist/auth/permission-validator.test.d.ts +0 -1
  200. package/dist/context/context.d.ts +0 -161
  201. package/dist/context/context.test.d.ts +0 -1
  202. package/dist/context/legacy-context.test.d.ts +0 -1
  203. package/dist/dataflow/index.d.ts +0 -1
  204. package/dist/dataflow/objects.d.ts +0 -5
  205. package/dist/dataflow/objects.test.d.ts +0 -1
  206. package/dist/events/event-manager.d.ts +0 -16
  207. package/dist/events/event-manager.spec.d.ts +0 -1
  208. package/dist/events/index.d.ts +0 -1
  209. package/dist/helpers/index.d.ts +0 -1
  210. package/dist/helpers/ucfirst.d.ts +0 -1
  211. package/dist/http/headers.d.ts +0 -4
  212. package/dist/http/headers.spec.d.ts +0 -1
  213. package/dist/http/request-context.d.ts +0 -12
  214. package/dist/http/request-context.spec.d.ts +0 -1
  215. package/dist/http/request.d.ts +0 -8
  216. package/dist/http/request.spec.d.ts +0 -1
  217. package/dist/http/url.spec.d.ts +0 -1
  218. package/dist/index.d.ts +0 -19
  219. package/dist/pipelines/pipeline-action.test.d.ts +0 -1
  220. package/dist/pipelines/pipeline.test.d.ts +0 -1
  221. package/dist/pkg.cjs +0 -30
  222. package/dist/pkg.mjs +0 -56612
  223. package/dist/schema/application.d.ts +0 -83
  224. package/dist/schema/application.test.d.ts +0 -1
  225. package/dist/schema/define-model.d.ts +0 -10
  226. package/dist/schema/define-model.test.d.ts +0 -1
  227. package/dist/schema/formats.d.ts +0 -10
  228. package/dist/schema/index.d.ts +0 -10
  229. package/dist/schema/labels.d.ts +0 -13
  230. package/dist/schema/labels.test.d.ts +0 -1
  231. package/dist/schema/module.d.ts +0 -7
  232. package/dist/schema/module.test.d.ts +0 -1
  233. package/dist/schema/properties.d.ts +0 -19
  234. package/dist/schema/response.d.ts +0 -31
  235. package/dist/schema/response.test.d.ts +0 -1
  236. package/dist/schema/supported-types.d.ts +0 -12
  237. package/dist/schema/supported-types.test.d.ts +0 -1
  238. package/dist/schema/transform-model.d.ts +0 -4
  239. package/dist/schema/transform-model.test.d.ts +0 -1
  240. package/dist/schema/types.d.ts +0 -95
  241. package/dist/schema/types.test.d.ts +0 -1
  242. package/dist/typescript/fetch.d.ts +0 -2
  243. package/dist/typescript/objects.d.ts +0 -12
  244. package/dist/validation/validator.test.d.ts +0 -1
  245. package/src/helpers/index.ts +0 -1
  246. package/src/helpers/ucfirst.ts +0 -3
  247. package/src/schema/application.test.ts +0 -286
  248. package/src/schema/application.ts +0 -150
  249. package/src/schema/define-model.test.ts +0 -81
  250. package/src/schema/define-model.ts +0 -50
  251. package/src/schema/formats.ts +0 -23
  252. package/src/schema/index.ts +0 -10
  253. package/src/schema/labels.test.ts +0 -60
  254. package/src/schema/module.test.ts +0 -39
  255. package/src/schema/module.ts +0 -6
  256. package/src/schema/properties.ts +0 -40
  257. package/src/schema/response.test.ts +0 -101
  258. package/src/schema/response.ts +0 -93
  259. package/src/schema/supported-types.test.ts +0 -20
  260. package/src/schema/supported-types.ts +0 -15
  261. package/src/schema/transform-model.test.ts +0 -31
  262. package/src/schema/transform-model.ts +0 -24
  263. package/src/schema/types.test.ts +0 -28
  264. package/src/schema/types.ts +0 -163
  265. package/tsconfig.json +0 -11
  266. package/vite.config.ts +0 -24
@@ -1,150 +0,0 @@
1
- import { cloneDeep } from 'lodash-es'
2
- import type { Model } from './define-model'
3
- import { Module } from './module'
4
- import type { DeclaroSchema } from './types'
5
- import { Response } from './response'
6
-
7
- export type ModuleFactory = (mod: Module) => Module | undefined
8
-
9
- export class Application {
10
- private _info: DeclaroSchema.InfoObject
11
- private _modules: Map<string, Module>
12
- private _models: Map<string, Model>
13
- private _responses: Map<number, Response>
14
-
15
- constructor(info: DeclaroSchema.InfoObject) {
16
- this._info = info
17
- this._modules = new Map()
18
- this._models = new Map()
19
- this._responses = new Map()
20
- }
21
-
22
- /**
23
- * Get the application info (read-only). Application info can only be set in the constructor of the application.
24
- *
25
- * Note: This should be set by the application implementation. It would be dangerous for any one module to be able to change the application info for all of the others.
26
- */
27
- get info() {
28
- return cloneDeep(this._info)
29
- }
30
-
31
- /**
32
- * Define a module for the application.
33
- *
34
- * @param tag The tag info for the module, to be used in the OpenAPI schema
35
- * @param factory A convenience function to define the module, adding endpoints, etc.
36
- * @returns
37
- */
38
- defineModule(tag: DeclaroSchema.TagObject, factory?: ModuleFactory) {
39
- let mod = new Module(this, tag)
40
- const result = factory?.(mod)
41
-
42
- if (result instanceof Module) {
43
- mod = result
44
- } else if (!!result) {
45
- throw new Error('Module factory must return a Module instance or undefined')
46
- }
47
-
48
- this._modules.set(tag.name, mod)
49
-
50
- return mod
51
- }
52
-
53
- /**
54
- * Get a module by name.
55
- *
56
- * @param name The name of the module to get
57
- * @returns The `Module` instance for the given name
58
- */
59
- getModule(name: string): Module {
60
- return this._modules.get(name)
61
- }
62
-
63
- /**
64
- * Add models to the application.
65
- *
66
- * @param models The models to add to the application
67
- * @example app.addModel(User)
68
- * @example app.addModel(User, Post)
69
- * @example app.addModel(...myModels)
70
- * @returns The application instance
71
- */
72
- addModel(...models: Model[]) {
73
- models.forEach((model) => {
74
- this._models.set(model.name, model)
75
- })
76
-
77
- return this
78
- }
79
-
80
- /**
81
- * Get a model by name.
82
- *
83
- * @param name The name of the model to get
84
- * @returns The `Model` instance for the given name
85
- */
86
- getModel(name: string): Model {
87
- return this._models.get(name)
88
- }
89
-
90
- /**
91
- * Get all models.
92
- *
93
- * @returns All models in the application
94
- */
95
- getModels(): Model[] {
96
- return Array.from(this._models.values())
97
- }
98
-
99
- /**
100
- * Add a response to the application.
101
- *
102
- * @param code The HTTP status code for the response
103
- * @param response The response object
104
- * @returns The application instance
105
- */
106
- addResponse(...responses: Response[]) {
107
- responses.forEach((response) => {
108
- const existingResponse = this.getResponse(response.code)
109
-
110
- if (existingResponse) {
111
- existingResponse.merge(response)
112
- } else {
113
- this._responses.set(response.code, response)
114
- }
115
- })
116
- return this
117
- }
118
-
119
- /**
120
- * Get a response by status code.
121
- *
122
- * @param code The HTTP status code for the response
123
- * @returns The `Response` instance for the given status code
124
- */
125
- getResponse(code: number): Response {
126
- return this._responses.get(code)
127
- }
128
-
129
- /**
130
- * Get all responses. (read-only)
131
- *
132
- * @returns All responses in the application
133
- */
134
- get responses(): Map<number, Response> {
135
- return new Map(this._responses)
136
- }
137
-
138
- /**
139
- * Get all responses as a hash of keys and values.
140
- *
141
- * @returns A hash of keys and values representing all responses in the application
142
- */
143
- getResponseSchema(): DeclaroSchema.ResponsesObject {
144
- const responsesHash: { [key: string]: DeclaroSchema.ResponseObject } = {}
145
- this._responses.forEach((value, key) => {
146
- responsesHash[key] = value.schema
147
- })
148
- return responsesHash
149
- }
150
- }
@@ -1,81 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { defineModel, type ModelName, type ModelProperties } from '.'
3
- import { t } from './properties'
4
-
5
- describe('Model definition', async () => {
6
- it('should define a model', async () => {
7
- const movie = defineModel('Movie', {
8
- type: 'object',
9
- properties: {
10
- title: {
11
- type: 'string',
12
- labels: {
13
- singularEntityName: 'Title',
14
- pluralEntityName: 'Titles',
15
- },
16
- },
17
- year: {
18
- type: 'integer',
19
- format: 'int32',
20
- },
21
- },
22
- required: ['title'],
23
- labels: {
24
- singularEntityName: 'Movie',
25
- pluralEntityName: 'Movies',
26
- },
27
- })
28
-
29
- const name: ModelName<typeof movie> = movie.name
30
- const properties: ModelProperties<typeof movie> = movie.schema.properties
31
-
32
- expect(name).toBe('Movie')
33
- expect(properties.title.type).toBe('string')
34
-
35
- expect(movie.name).toBe('Movie')
36
- expect(movie.isModel).toBe(true)
37
- expect(movie.schema.type).toBe('object')
38
- expect(movie.schema.properties.title['type']).toBe('string')
39
- expect(movie.schema.properties.year['type']).toBe('integer')
40
- expect(movie.schema.properties.year['format']).toBe('int32')
41
- expect(movie.schema.required).toEqual(['title'])
42
- expect(movie.schema.labels).toBeTypeOf('object')
43
- expect(movie.schema.labels.pluralEntityName).toBe('Movies')
44
- expect(movie.schema.labels.singularEntityName).toBe('Movie')
45
-
46
- expect(movie.schema.properties.title.labels).toBeTypeOf('object')
47
- expect(movie.schema.properties.title.labels.pluralEntityName).toBe('Titles')
48
- expect(movie.schema.properties.title.labels.singularEntityName).toBe('Title')
49
- expect((movie.schema.properties.title.labels as any).pluralSlug).toBe(undefined) // Only include explicitly defined labels—leave the rest up to the framework
50
- })
51
-
52
- it('should define a model with nested properties', async () => {
53
- const movie = defineModel('Movie', {
54
- type: 'object',
55
- properties: {
56
- title: {
57
- type: 'string',
58
- labels: {
59
- singularEntityName: 'Title',
60
- pluralEntityName: 'Titles',
61
- },
62
- },
63
- year: {
64
- type: 'integer',
65
- format: 'int32',
66
- },
67
- meta: {
68
- type: 'object',
69
- properties: {
70
- rating: t.integer(),
71
- },
72
- },
73
- },
74
- required: ['title'],
75
- labels: {
76
- singularEntityName: 'Movie',
77
- pluralEntityName: 'Movies',
78
- },
79
- })
80
- })
81
- })
@@ -1,50 +0,0 @@
1
- import type { DeclaroSchema } from './types'
2
-
3
- export type Model<
4
- T extends DeclaroSchema.AnyObjectProperties = DeclaroSchema.AnyObjectProperties,
5
- N extends Readonly<string> = string,
6
- > = {
7
- name: N
8
- schema: DeclaroSchema.SchemaObject<T>
9
- isModel: true
10
- }
11
-
12
- type TraverseSchemaFn<T extends DeclaroSchema.AnyObjectProperties> = (
13
- name: string,
14
- property: DeclaroSchema.SchemaObject<any>,
15
- schema: DeclaroSchema.SchemaObject<T>,
16
- ) => DeclaroSchema.SchemaObject<any>
17
-
18
- export type ModelName<T extends Model<any, string>> = T['name']
19
- export type ModelProperties<T extends Model<any, string>> = T['schema']['properties']
20
-
21
- function traverseSchema<T extends DeclaroSchema.AnyObjectProperties>(
22
- schema: DeclaroSchema.SchemaObject<T>,
23
- fn: TraverseSchemaFn<T>,
24
- ) {
25
- for (const [key, value] of Object.entries(schema.properties ?? {})) {
26
- let properties: DeclaroSchema.AnyObjectProperties = schema.properties!
27
- properties[key] = fn(key, value, schema)
28
- }
29
- }
30
-
31
- export function initializeModel(schema: DeclaroSchema.SchemaObject<any>) {
32
- return schema
33
- }
34
-
35
- export function defineModel<T extends DeclaroSchema.AnyObjectProperties, N extends Readonly<string>>(
36
- name: N,
37
- doc: DeclaroSchema.SchemaObject<T>,
38
- ): Model<T, N> {
39
- traverseSchema(doc, (name, property, schema) => {
40
- return initializeModel({
41
- propertyName: name,
42
- ...property,
43
- })
44
- })
45
- return {
46
- name: name as Readonly<N>,
47
- schema: { ...doc },
48
- isModel: true,
49
- }
50
- }
@@ -1,23 +0,0 @@
1
- export enum RelationFormat {
2
- OneToOne = 'one-to-one',
3
- OneToMany = 'one-to-many',
4
- ManyToOne = 'many-to-one',
5
- ManyToMany = 'many-to-many',
6
- EmbeddedObject = 'embedded-object',
7
- EmbeddedArray = 'embedded-array',
8
- }
9
-
10
- export type RelationReferenceType = 'object' | 'array'
11
-
12
- export function getReferenceType(format: string): RelationReferenceType {
13
- switch (format) {
14
- case RelationFormat.OneToOne:
15
- case RelationFormat.ManyToOne:
16
- case RelationFormat.EmbeddedObject:
17
- return 'object'
18
- case RelationFormat.OneToMany:
19
- case RelationFormat.ManyToMany:
20
- case RelationFormat.EmbeddedArray:
21
- return 'array'
22
- }
23
- }
@@ -1,10 +0,0 @@
1
- export * from './define-model'
2
- export * from './supported-types'
3
- export * from './transform-model'
4
- export * from './labels'
5
- export * from './module'
6
- export * from './application'
7
- export * from './response'
8
- export * from './formats'
9
- export * from './properties'
10
- export * from './types'
@@ -1,60 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { getEntityLabels } from './labels'
3
-
4
- describe('Entity Labels', () => {
5
- it('Should be able to define labels for entities', () => {
6
- const labels = getEntityLabels('User')
7
-
8
- expect(labels.singularLabel).toBe('User')
9
- expect(labels.pluralLabel).toBe('Users')
10
- expect(labels.singularParameter).toBe('user')
11
- expect(labels.pluralParameter).toBe('users')
12
- expect(labels.singularSlug).toBe('user')
13
- expect(labels.pluralSlug).toBe('users')
14
- expect(labels.singularEntityName).toBe('User')
15
- expect(labels.pluralEntityName).toBe('Users')
16
- expect(labels.singularTableName).toBe('user')
17
- expect(labels.pluralTableName).toBe('users')
18
- })
19
-
20
- it('Should handle different types of entity names', () => {
21
- const twoWords = getEntityLabels('SomeEntity')
22
-
23
- expect(twoWords.singularLabel).toBe('Some Entity')
24
- expect(twoWords.pluralLabel).toBe('Some Entities')
25
- expect(twoWords.singularParameter).toBe('someEntity')
26
- expect(twoWords.pluralParameter).toBe('someEntities')
27
- expect(twoWords.singularSlug).toBe('some-entity')
28
- expect(twoWords.pluralSlug).toBe('some-entities')
29
- expect(twoWords.singularEntityName).toBe('SomeEntity')
30
- expect(twoWords.pluralEntityName).toBe('SomeEntities')
31
- expect(twoWords.singularTableName).toBe('some-entity')
32
- expect(twoWords.pluralTableName).toBe('some-entities')
33
-
34
- const threeWords = getEntityLabels('SomeOtherEntity')
35
-
36
- expect(threeWords.singularLabel).toBe('Some Other Entity')
37
- expect(threeWords.pluralLabel).toBe('Some Other Entities')
38
- expect(threeWords.singularParameter).toBe('someOtherEntity')
39
- expect(threeWords.pluralParameter).toBe('someOtherEntities')
40
- expect(threeWords.singularSlug).toBe('some-other-entity')
41
- expect(threeWords.pluralSlug).toBe('some-other-entities')
42
- expect(threeWords.singularEntityName).toBe('SomeOtherEntity')
43
- expect(threeWords.pluralEntityName).toBe('SomeOtherEntities')
44
- expect(threeWords.singularTableName).toBe('some-other-entity')
45
- expect(threeWords.pluralTableName).toBe('some-other-entities')
46
-
47
- const sentence = getEntityLabels('Some other entity')
48
-
49
- expect(sentence.singularLabel).toBe('Some Other Entity')
50
- expect(sentence.pluralLabel).toBe('Some Other Entities')
51
- expect(sentence.singularParameter).toBe('someOtherEntity')
52
- expect(sentence.pluralParameter).toBe('someOtherEntities')
53
- expect(sentence.singularSlug).toBe('some-other-entity')
54
- expect(sentence.pluralSlug).toBe('some-other-entities')
55
- expect(sentence.singularEntityName).toBe('SomeOtherEntity')
56
- expect(sentence.pluralEntityName).toBe('SomeOtherEntities')
57
- expect(sentence.singularTableName).toBe('some-other-entity')
58
- expect(sentence.pluralTableName).toBe('some-other-entities')
59
- })
60
- })
@@ -1,39 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { Module } from './module'
3
- import { Application } from './application'
4
-
5
- describe('Module definition', () => {
6
- it('should define a module', () => {
7
- const application = new Application({
8
- title: 'My API',
9
- version: '1.0.0',
10
- description: 'My API description',
11
- termsOfService: 'https://example.com/terms',
12
- contact: {
13
- name: 'API Support',
14
- url: 'https://example.com/support',
15
- email: 'test@test.com',
16
- },
17
- })
18
-
19
- const module = new Module(application, {
20
- name: 'Module',
21
-
22
- description: 'Module description',
23
- externalDocs: {
24
- description: 'External documentation',
25
- url: 'https://example.com/docs',
26
- },
27
- })
28
-
29
- expect(module.application).toBeInstanceOf(Application)
30
- expect(module.application.info.title).toBe('My API')
31
- expect(module.application.info.version).toBe('1.0.0')
32
- expect(module.application.info.description).toBe('My API description')
33
-
34
- expect(module.tag.name).toBe('Module')
35
- expect(module.tag.description).toBe('Module description')
36
- expect(module.tag.externalDocs.description).toBe('External documentation')
37
- expect(module.tag.externalDocs.url).toBe('https://example.com/docs')
38
- })
39
- })
@@ -1,6 +0,0 @@
1
- import type { Application } from './application'
2
- import type { DeclaroSchema } from './types'
3
-
4
- export class Module {
5
- constructor(public readonly application: Application, public readonly tag: DeclaroSchema.TagObject) {}
6
- }
@@ -1,40 +0,0 @@
1
- import { type DeclaroSchema } from './types'
2
-
3
- export const t = {
4
- string: <T extends DeclaroSchema.NonArraySchemaObject<any, any>>(property?: T) => {
5
- return {
6
- ...property,
7
- type: 'string' as const,
8
- }
9
- },
10
- number: <T extends DeclaroSchema.NonArraySchemaObject<any, any>>(property?: T) => {
11
- return {
12
- ...property,
13
- type: 'number' as const,
14
- }
15
- },
16
- boolean: <T extends DeclaroSchema.NonArraySchemaObject<any, any>>(property?: T) => {
17
- return {
18
- ...property,
19
- type: 'boolean' as const,
20
- }
21
- },
22
- integer: <T extends DeclaroSchema.NonArraySchemaObject<any, any>>(property?: T) => {
23
- return {
24
- ...property,
25
- type: 'integer' as const,
26
- }
27
- },
28
- object: <T extends DeclaroSchema.NonArraySchemaObject<any, any>>(property?: T) => {
29
- return {
30
- ...property,
31
- type: 'object' as const,
32
- }
33
- },
34
- array: <T extends DeclaroSchema.NonArraySchemaObject<any, any>>(property?: T) => {
35
- return {
36
- ...property,
37
- type: 'array' as const,
38
- }
39
- },
40
- }
@@ -1,101 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { Response } from './response'
3
- import type { DeclaroSchema } from './types'
4
-
5
- describe('Response schema', () => {
6
- it('should define a response schema', () => {
7
- const response = new Response(200, {
8
- description: 'Successful response',
9
- })
10
- .content('application/json', {
11
- schema: {
12
- type: 'object',
13
- properties: {
14
- message: {
15
- type: 'string',
16
- },
17
- },
18
- },
19
- })
20
- .content('application/xml', {
21
- schema: {
22
- type: 'object',
23
- properties: {
24
- message: {
25
- type: 'string',
26
- },
27
- },
28
- },
29
- })
30
- .content('application/json', {
31
- schema: {
32
- type: 'object',
33
- properties: {
34
- error: {
35
- type: 'string',
36
- },
37
- },
38
- },
39
- })
40
- .content('application/xml', {
41
- schema: {
42
- type: 'object',
43
- properties: {
44
- error: {
45
- type: 'string',
46
- },
47
- },
48
- },
49
- })
50
-
51
- expect(response.code).toBe(200)
52
- expect(response).toBeInstanceOf(Response)
53
-
54
- const jsonSchema = response.schema.content['application/json'].schema as DeclaroSchema.SchemaObject<any>
55
- expect(jsonSchema).toBeTypeOf('object')
56
- expect(jsonSchema.oneOf.length).toBe(2)
57
- expect((jsonSchema.oneOf[0] as any).properties.message.type).toBe('string')
58
- expect((jsonSchema.oneOf[1] as any).properties.error.type).toBe('string')
59
- })
60
-
61
- it('Should merge response schemas', () => {
62
- const response = new Response(200, {
63
- description: 'Successful response',
64
- }).content('application/json', {
65
- schema: {
66
- type: 'object',
67
- properties: {
68
- message: {
69
- type: 'string',
70
- },
71
- },
72
- },
73
- })
74
-
75
- const response2 = new Response(200, {
76
- description: 'Successful response',
77
- }).content('application/xml', {
78
- schema: {
79
- type: 'object',
80
- properties: {
81
- message: {
82
- type: 'string',
83
- },
84
- },
85
- },
86
- })
87
-
88
- response.merge(response2)
89
-
90
- expect(response.code).toBe(200)
91
- expect(response).toBeInstanceOf(Response)
92
-
93
- const jsonSchema = response.schema.content['application/json'].schema as DeclaroSchema.SchemaObject<any>
94
- expect(jsonSchema).toBeTypeOf('object')
95
- expect((jsonSchema as any).properties.message.type).toBe('string')
96
-
97
- const xmlSchema = response.schema.content['application/xml'].schema as DeclaroSchema.SchemaObject<any>
98
- expect(xmlSchema).toBeTypeOf('object')
99
- expect((xmlSchema as any).properties.message.type).toBe('string')
100
- })
101
- })
@@ -1,93 +0,0 @@
1
- import type { DeclaroSchema } from './types'
2
- import { merge, cloneDeep } from 'lodash-es'
3
-
4
- export type MediaRecord<T extends DeclaroSchema.AnyObjectProperties> = {
5
- contentType: string
6
- media: DeclaroSchema.MediaTypeObject<T>
7
- }
8
-
9
- export class Response {
10
- private _code: number
11
- private _response: DeclaroSchema.ResponseObject
12
- private _mediaTypes: MediaRecord<any>[]
13
-
14
- constructor(code: number, response: DeclaroSchema.ResponseObject) {
15
- this._mediaTypes = []
16
- const defaultDescription = code >= 200 && code < 300 ? 'Successful response' : 'Error response'
17
-
18
- this._code = code
19
- this._response = {
20
- ...response,
21
- description: response.description ?? defaultDescription,
22
- content: {},
23
- }
24
- }
25
-
26
- /**
27
- *
28
- * @param contentType The HTTP content type
29
- * @param media The openapi media schema defining the response type.
30
- * @returns An instance of the response object for chaining content types.
31
- */
32
- content(contentType: string, ...media: DeclaroSchema.MediaTypeObject<any>[]) {
33
- this._mediaTypes.push(...media.map((m) => ({ contentType, media: m })))
34
-
35
- return this
36
- }
37
-
38
- merge(response: Response) {
39
- merge(this._response, response._response)
40
- this._mediaTypes.push(...response._mediaTypes)
41
-
42
- return this
43
- }
44
-
45
- /**
46
- * The response code.
47
- *
48
- * Note: Read-only. This can only be initialized in the constructor.
49
- */
50
- get code() {
51
- return this._code
52
- }
53
-
54
- /**
55
- * The fully composed openapi response object.
56
- */
57
- get schema(): DeclaroSchema.ResponseObject {
58
- const contentTypes = this.getContentTypes()
59
-
60
- const response = cloneDeep(this._response)
61
-
62
- contentTypes.forEach((contentType) => {
63
- const media = this.getMediaForContentType(contentType)
64
-
65
- if (media.length > 1) {
66
- response.content[contentType] = {
67
- schema: {
68
- oneOf: media.map((m) => m.schema),
69
- },
70
- examples: media.reduce((acc, media) => {
71
- if (media.examples) {
72
- return { ...acc, ...media.examples }
73
- }
74
-
75
- return acc
76
- }, {}),
77
- }
78
- } else if (media.length === 1) {
79
- response.content[contentType] = media[0]
80
- }
81
- })
82
-
83
- return response
84
- }
85
-
86
- protected getContentTypes() {
87
- return [...new Set(this._mediaTypes.map(({ contentType }) => contentType))]
88
- }
89
-
90
- protected getMediaForContentType(contentType: string) {
91
- return this._mediaTypes.filter((media) => media.contentType === contentType).map((c) => c.media)
92
- }
93
- }
@@ -1,20 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { TypescriptMap } from './supported-types'
3
-
4
- describe('Supported types', async () => {
5
- it('should support scalar types', async () => {
6
- expect(TypescriptMap.string).toBe('string')
7
- expect(TypescriptMap.text).toBe('string')
8
-
9
- expect(TypescriptMap.integer).toBe('number')
10
- expect(TypescriptMap.number).toBe('number')
11
- expect(TypescriptMap.float).toBe('number')
12
- expect(TypescriptMap.double).toBe('number')
13
-
14
- expect(TypescriptMap.boolean).toBe('boolean')
15
-
16
- expect(TypescriptMap.date).toBe('Date')
17
- expect(TypescriptMap.datetime).toBe('Date')
18
- expect(TypescriptMap.time).toBe('Date')
19
- })
20
- })