@archtx/procedures 1.0.1
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/README.md +283 -0
- package/dist/exports.d.ts +6 -0
- package/dist/exports.js +7 -0
- package/dist/exports.js.map +1 -0
- package/dist/src/errors.d.ts +22 -0
- package/dist/src/errors.js +50 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/index.d.ts +56 -0
- package/dist/src/index.js +86 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/index.test.d.ts +1 -0
- package/dist/src/index.test.js +307 -0
- package/dist/src/index.test.js.map +1 -0
- package/dist/src/procedure-codes.d.ts +26 -0
- package/dist/src/procedure-codes.js +28 -0
- package/dist/src/procedure-codes.js.map +1 -0
- package/dist/src/schema/compute-schema.d.ts +23 -0
- package/dist/src/schema/compute-schema.js +28 -0
- package/dist/src/schema/compute-schema.js.map +1 -0
- package/dist/src/schema/compute-schema.test.d.ts +1 -0
- package/dist/src/schema/compute-schema.test.js +107 -0
- package/dist/src/schema/compute-schema.test.js.map +1 -0
- package/dist/src/schema/extract-json-schema.d.ts +2 -0
- package/dist/src/schema/extract-json-schema.js +12 -0
- package/dist/src/schema/extract-json-schema.js.map +1 -0
- package/dist/src/schema/extract-json-schema.test.d.ts +1 -0
- package/dist/src/schema/extract-json-schema.test.js +23 -0
- package/dist/src/schema/extract-json-schema.test.js.map +1 -0
- package/dist/src/schema/parser.d.ts +21 -0
- package/dist/src/schema/parser.js +57 -0
- package/dist/src/schema/parser.js.map +1 -0
- package/dist/src/schema/parser.test.d.ts +1 -0
- package/dist/src/schema/parser.test.js +71 -0
- package/dist/src/schema/parser.test.js.map +1 -0
- package/dist/src/schema/resolve-schema-lib.d.ts +12 -0
- package/dist/src/schema/resolve-schema-lib.js +7 -0
- package/dist/src/schema/resolve-schema-lib.js.map +1 -0
- package/dist/src/schema/resolve-schema-lib.test.d.ts +1 -0
- package/dist/src/schema/resolve-schema-lib.test.js +17 -0
- package/dist/src/schema/resolve-schema-lib.test.js.map +1 -0
- package/dist/src/schema/types.d.ts +4 -0
- package/dist/src/schema/types.js +2 -0
- package/dist/src/schema/types.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# @archtx/procedures
|
|
2
|
+
|
|
3
|
+
A **TypeScript** library for generating type-safe Procedure Calls with built-in schema validation using Suretype or TypeBox.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Create type-safe Procedures
|
|
8
|
+
- Built-in schema validation using Suretype/TypeBox
|
|
9
|
+
- Runtime validation of arguments
|
|
10
|
+
- Generated JSON-Schema for Procedures
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @archtx/procedures
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Basic Example
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { Procedures } from '@archtx/procedures'
|
|
24
|
+
import { v } from 'suretype'
|
|
25
|
+
|
|
26
|
+
const { Create } = Procedures()
|
|
27
|
+
|
|
28
|
+
// Register a query Procedure
|
|
29
|
+
const { getUserInfo, getUserInfoSchema } = Create(
|
|
30
|
+
'getUserInfo',
|
|
31
|
+
{
|
|
32
|
+
schema: {
|
|
33
|
+
args: v.object({ userId: v.string().required() }),
|
|
34
|
+
data: v.object({
|
|
35
|
+
name: v.string().required(),
|
|
36
|
+
email: v.string(),
|
|
37
|
+
}),
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
async (ctx, args) => {
|
|
41
|
+
// Handle the Procedure call
|
|
42
|
+
return {
|
|
43
|
+
name: 'John Doe',
|
|
44
|
+
email: 'john@example.com',
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
// Call the Procedure
|
|
50
|
+
const userInfo = await getUserInfo({ userId: '123' })
|
|
51
|
+
|
|
52
|
+
// view the JSON-schema for the Procedure
|
|
53
|
+
console.log(getUserInfoSchema.args, getUserInfoSchema.data)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### With Custom Context
|
|
57
|
+
|
|
58
|
+
You can pass a `type` to the `Procedures<Type>()` invokation to require custom context in all procedure calls. The
|
|
59
|
+
`hook` function allows you to add local context to the Procedure call. This is useful for adding additional context
|
|
60
|
+
to the Procedure call, such as authentication tokens or user information.
|
|
61
|
+
|
|
62
|
+
The context type will be typed in the Procedure handler first argument `ctx`.
|
|
63
|
+
|
|
64
|
+
When your procedure has a `hook` the return value of the `hook` function will be passed to the Procedure handler by
|
|
65
|
+
extending the `ctx` object. This allows you to pass additional context to the Procedure handler.
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
interface CustomContext {
|
|
69
|
+
authToken: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const { Create } = Procedures<CustomContext>({
|
|
73
|
+
// Procedure handler registration callback
|
|
74
|
+
onCreate: ({ handler, name }) => {
|
|
75
|
+
httpRouter.post(`/Procedure/${name}`, async (req) => {
|
|
76
|
+
return handler({ authToken: req.headers.authorization }, req.body)
|
|
77
|
+
})
|
|
78
|
+
},
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const { CheckIsAuthenticated } = Create(
|
|
82
|
+
'CheckIsAuthenticated',
|
|
83
|
+
{
|
|
84
|
+
hook: async (ctx, args) => {
|
|
85
|
+
if (validateAuthToken(ctx.authToken)) {
|
|
86
|
+
const user = await getUserFromToken(ctx.authToken)
|
|
87
|
+
|
|
88
|
+
return { user }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
throw new Error('Invalid auth token')
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
schema: {
|
|
95
|
+
data: v.string(),
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
async (ctx, args) => {
|
|
99
|
+
// Access context in handler with type-safety
|
|
100
|
+
console.log(ctx.user)
|
|
101
|
+
return 'User authentication is valid'
|
|
102
|
+
},
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
// Call the Procedure
|
|
106
|
+
CheckIsAuthenticated({ authToken: 'valid-token' }, {}) // Returns 'User authentication is valid'
|
|
107
|
+
CheckIsAuthenticated({ authToken: 'no-token' }, {}) // Throws 'ProcedureContextError: Invalid auth token'
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Retrieving Registered Procedures
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const { Create, getProcedures } = Procedures()
|
|
114
|
+
|
|
115
|
+
// Later in your code
|
|
116
|
+
const registeredProcedures = getProcedures()
|
|
117
|
+
const userInfoProcedure = registeredProcedures.get('getUserInfo')
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Generated JSON-Schema
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
const { Create, getProcedures } = Procedures()
|
|
124
|
+
|
|
125
|
+
const { getUserInfo, getUserInfoSchema } = Create(
|
|
126
|
+
'getUserInfo',
|
|
127
|
+
{
|
|
128
|
+
schema: {
|
|
129
|
+
args: v.object({ userId: v.string().required() }),
|
|
130
|
+
data: v.object({
|
|
131
|
+
name: v.string().required(),
|
|
132
|
+
email: v.string(),
|
|
133
|
+
}),
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
async (ctx, args) => {
|
|
137
|
+
// Handle the Procedure call
|
|
138
|
+
return {
|
|
139
|
+
name: 'John Doe',
|
|
140
|
+
email: '',
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
console.log(getUserInfoSchema) // { args: JSON-schema; data: JSON-schema }
|
|
146
|
+
// or
|
|
147
|
+
console.log(getProcedures().get('getUserInfo').config.schema) // { args: JSON-schema; data: JSON-schema }
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Procedure Errors
|
|
151
|
+
|
|
152
|
+
The context provided to each procedure has a built-in `error` method that can be used to throw errors with a specific
|
|
153
|
+
error code and message. This is useful for handling errors in a consistent way across all procedures.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
import { Procedures, ProcedureCodes } from '@archtx/procedures'
|
|
157
|
+
|
|
158
|
+
const { Create } = Procedures()
|
|
159
|
+
|
|
160
|
+
const { getUserInfo } = Create(
|
|
161
|
+
'getUserInfo',
|
|
162
|
+
{
|
|
163
|
+
//...
|
|
164
|
+
},
|
|
165
|
+
async (ctx, args) => {
|
|
166
|
+
if (!isSomeConditionPassing(args)) {
|
|
167
|
+
throw ctx.error(
|
|
168
|
+
ProcedureCodes.PRECONDITION_FAILED,
|
|
169
|
+
'Some condition failed',
|
|
170
|
+
{
|
|
171
|
+
// extra (optional) meta data for the error
|
|
172
|
+
},
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
name: 'John Doe',
|
|
177
|
+
email: '',
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
The lib exports a `ProcedureError` that can be used:
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import { ProcedureError } from '@archtx/procedures'
|
|
187
|
+
import { processError } from '@vitest/utils/error'
|
|
188
|
+
|
|
189
|
+
const procedureError = new ProcedureError(500, 'Internal server error', {
|
|
190
|
+
// this 3rd argument is optional
|
|
191
|
+
extraInfo: '...',
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
processError.procedureName // string
|
|
195
|
+
processError.code // number
|
|
196
|
+
processError.message // string
|
|
197
|
+
processError.meta // object
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
/**
|
|
203
|
+
* A list of common codes borrowed from HTTP status codes.
|
|
204
|
+
*/
|
|
205
|
+
enum ProcedureCodes {
|
|
206
|
+
OK = 200,
|
|
207
|
+
CREATED = 201,
|
|
208
|
+
ACCEPTED = 202,
|
|
209
|
+
NO_CONTENT = 204,
|
|
210
|
+
RESET_CONTENT = 205,
|
|
211
|
+
PARTIAL_CONTENT = 206,
|
|
212
|
+
BAD_REQUEST = 400,
|
|
213
|
+
UNAUTHORIZED = 401,
|
|
214
|
+
FORBIDDEN = 403,
|
|
215
|
+
NOT_FOUND = 404,
|
|
216
|
+
NOT_ACCEPTABLE = 406,
|
|
217
|
+
PROXY_AUTHENTICATION_REQUIRED = 407,
|
|
218
|
+
TIMEOUT = 408,
|
|
219
|
+
CONFLICT = 409,
|
|
220
|
+
PRECONDITION_FAILED = 412,
|
|
221
|
+
VALIDATION_ERROR = 422,
|
|
222
|
+
TOO_MANY_REQUESTS = 429,
|
|
223
|
+
INTERNAL_ERROR = 500,
|
|
224
|
+
HANDLER_ERROR = 500,
|
|
225
|
+
NOT_IMPLEMENTED = 501,
|
|
226
|
+
SERVICE_UNAVAILABLE = 503,
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## API Reference
|
|
231
|
+
|
|
232
|
+
### `Procedures(options?)`
|
|
233
|
+
|
|
234
|
+
Creates a new Procedure generator instance.
|
|
235
|
+
|
|
236
|
+
#### Options
|
|
237
|
+
|
|
238
|
+
- `onCreate`: Callback function called when a new Procedure is registered
|
|
239
|
+
- `handler`: The Procedure handler function
|
|
240
|
+
- `config`: Configuration object containing schema and local Procedure context handler
|
|
241
|
+
- `name`: Name of the Procedure procedure
|
|
242
|
+
|
|
243
|
+
### `Create`
|
|
244
|
+
|
|
245
|
+
Object containing methods to register new Procedures.
|
|
246
|
+
|
|
247
|
+
#### Methods
|
|
248
|
+
|
|
249
|
+
- `query(name, options, handler)`: Register a query Procedure
|
|
250
|
+
- `mutation(name, options, handler)`: Register a mutation Procedure
|
|
251
|
+
|
|
252
|
+
> Note: `query` and `mutation` methods are identical, except for the type of Procedure registered.
|
|
253
|
+
> These Procedure "type"'s are used in client implementation to differentiate between query and mutation Procedures.
|
|
254
|
+
> This helps clients support caching calls appropriately if using a cache layer. For example, you can cache
|
|
255
|
+
> query Procedures but should not cache mutation Procedures.
|
|
256
|
+
|
|
257
|
+
#### Options
|
|
258
|
+
|
|
259
|
+
- `schema`:
|
|
260
|
+
- `args`: Suretype/TypeBox validation schema for arguments
|
|
261
|
+
- `data`: Suretype/TypeBox validation schema for return data
|
|
262
|
+
- `context`: Function that returns additional context local to the Procedure call
|
|
263
|
+
|
|
264
|
+
### Error Handling
|
|
265
|
+
|
|
266
|
+
The library includes built-in validation error handling through the `ProcedureValidationError` class.
|
|
267
|
+
|
|
268
|
+
If the Procedure `config.args` schema exists, the `args` are validated before the Procedure handler is called.
|
|
269
|
+
If the validation fails, an `ProcedureValidationError` is thrown with the validation error message.
|
|
270
|
+
|
|
271
|
+
## Acknowledgements
|
|
272
|
+
|
|
273
|
+
- `@sinclair/typebox` - For providing a great library for generating JSON schema with TypeScript.
|
|
274
|
+
- `@suretype/suretype` - For providing a great library for runtime type validation.
|
|
275
|
+
- `@typeschema` - For providing inspiration for the Procedure schema resolver between Suretype or TypeBox.
|
|
276
|
+
|
|
277
|
+
## License
|
|
278
|
+
|
|
279
|
+
MIT
|
|
280
|
+
|
|
281
|
+
## Contributing
|
|
282
|
+
|
|
283
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
package/dist/exports.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './src/index';
|
|
2
|
+
export * from './src/errors';
|
|
3
|
+
export * from './src/procedure-codes';
|
|
4
|
+
export * from './src/schema/extract-json-schema';
|
|
5
|
+
export * from './src/schema/parser';
|
|
6
|
+
export * from './src/schema/resolve-schema-lib';
|
|
7
|
+
//# sourceMappingURL=exports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exports.js","sourceRoot":"","sources":["../exports.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,uBAAuB,CAAA;AACrC,cAAc,kCAAkC,CAAA;AAChD,cAAc,qBAAqB,CAAA;AACnC,cAAc,iCAAiC,CAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { TSchemaValidationError } from './schema/parser';
|
|
2
|
+
import { ProcedureCodes } from './procedure-codes';
|
|
3
|
+
export declare class ProcedureError extends Error {
|
|
4
|
+
readonly procedureName: string;
|
|
5
|
+
readonly code: ProcedureCodes & number;
|
|
6
|
+
readonly message: string;
|
|
7
|
+
readonly meta?: object | undefined;
|
|
8
|
+
constructor(procedureName: string, code: ProcedureCodes & number, message: string, meta?: object | undefined);
|
|
9
|
+
}
|
|
10
|
+
export declare class ProcedureHookError extends ProcedureError {
|
|
11
|
+
readonly procedureName: string;
|
|
12
|
+
constructor(procedureName: string, message: string);
|
|
13
|
+
}
|
|
14
|
+
export declare class ProcedureValidationError extends ProcedureError {
|
|
15
|
+
readonly procedureName: string;
|
|
16
|
+
readonly errors?: TSchemaValidationError[] | undefined;
|
|
17
|
+
constructor(procedureName: string, message: string, errors?: TSchemaValidationError[] | undefined);
|
|
18
|
+
}
|
|
19
|
+
export declare class ProcedureRegistrationError extends Error {
|
|
20
|
+
readonly procedureName: string;
|
|
21
|
+
constructor(procedureName: string, message: string);
|
|
22
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ProcedureCodes } from './procedure-codes';
|
|
2
|
+
export class ProcedureError extends Error {
|
|
3
|
+
procedureName;
|
|
4
|
+
code;
|
|
5
|
+
message;
|
|
6
|
+
meta;
|
|
7
|
+
constructor(procedureName, code, message, meta) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.procedureName = procedureName;
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.message = message;
|
|
12
|
+
this.meta = meta;
|
|
13
|
+
this.name = 'ProcedureError';
|
|
14
|
+
// https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/
|
|
15
|
+
Object.setPrototypeOf(this, ProcedureError.prototype);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export class ProcedureHookError extends ProcedureError {
|
|
19
|
+
procedureName;
|
|
20
|
+
constructor(procedureName, message) {
|
|
21
|
+
super(procedureName, ProcedureCodes.PRECONDITION_FAILED, message);
|
|
22
|
+
this.procedureName = procedureName;
|
|
23
|
+
this.name = 'ProcedureHookError';
|
|
24
|
+
// https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/
|
|
25
|
+
Object.setPrototypeOf(this, ProcedureHookError.prototype);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export class ProcedureValidationError extends ProcedureError {
|
|
29
|
+
procedureName;
|
|
30
|
+
errors;
|
|
31
|
+
constructor(procedureName, message, errors) {
|
|
32
|
+
super(procedureName, ProcedureCodes.VALIDATION_ERROR, message);
|
|
33
|
+
this.procedureName = procedureName;
|
|
34
|
+
this.errors = errors;
|
|
35
|
+
this.name = 'ProcedureValidationError';
|
|
36
|
+
// https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/
|
|
37
|
+
Object.setPrototypeOf(this, ProcedureValidationError.prototype);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export class ProcedureRegistrationError extends Error {
|
|
41
|
+
procedureName;
|
|
42
|
+
constructor(procedureName, message) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.procedureName = procedureName;
|
|
45
|
+
this.name = 'ProcedureRegistrationError';
|
|
46
|
+
// https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/
|
|
47
|
+
Object.setPrototypeOf(this, ProcedureRegistrationError.prototype);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAElD,MAAM,OAAO,cAAe,SAAQ,KAAK;IAE5B;IACA;IACA;IACA;IAJX,YACW,aAAqB,EACrB,IAA6B,EAC7B,OAAe,EACf,IAAa;QAEtB,KAAK,CAAC,OAAO,CAAC,CAAA;QALL,kBAAa,GAAb,aAAa,CAAQ;QACrB,SAAI,GAAJ,IAAI,CAAyB;QAC7B,YAAO,GAAP,OAAO,CAAQ;QACf,SAAI,GAAJ,IAAI,CAAS;QAGtB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAA;QAE5B,mMAAmM;QACnM,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,cAAc,CAAC,SAAS,CAAC,CAAA;IACvD,CAAC;CACF;AAED,MAAM,OAAO,kBAAmB,SAAQ,cAAc;IAEzC;IADX,YACW,aAAqB,EAC9B,OAAe;QAEf,KAAK,CAAC,aAAa,EAAE,cAAc,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAA;QAHxD,kBAAa,GAAb,aAAa,CAAQ;QAI9B,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAA;QAEhC,mMAAmM;QACnM,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,kBAAkB,CAAC,SAAS,CAAC,CAAA;IAC3D,CAAC;CACF;AAED,MAAM,OAAO,wBAAyB,SAAQ,cAAc;IAE/C;IAEA;IAHX,YACW,aAAqB,EAC9B,OAAe,EACN,MAAiC;QAE1C,KAAK,CAAC,aAAa,EAAE,cAAc,CAAC,gBAAgB,EAAE,OAAO,CAAC,CAAA;QAJrD,kBAAa,GAAb,aAAa,CAAQ;QAErB,WAAM,GAAN,MAAM,CAA2B;QAG1C,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAA;QAEtC,mMAAmM;QACnM,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,wBAAwB,CAAC,SAAS,CAAC,CAAA;IACjE,CAAC;CACF;AAED,MAAM,OAAO,0BAA2B,SAAQ,KAAK;IAC9B;IAArB,YAAqB,aAAqB,EAAE,OAAe;QACzD,KAAK,CAAC,OAAO,CAAC,CAAA;QADK,kBAAa,GAAb,aAAa,CAAQ;QAExC,IAAI,CAAC,IAAI,GAAG,4BAA4B,CAAA;QAExC,mMAAmM;QACnM,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,0BAA0B,CAAC,SAAS,CAAC,CAAA;IACnE,CAAC;CACF"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { ProcedureError } from './errors';
|
|
2
|
+
import { ProcedureCodes } from './procedure-codes';
|
|
3
|
+
import { TJSONSchema, TSchemaLib } from './schema/types';
|
|
4
|
+
export type TNoContextProvided = unknown;
|
|
5
|
+
export type TLocalContext = {
|
|
6
|
+
error: (code: ProcedureCodes & number, message: string, meta?: object) => ProcedureError;
|
|
7
|
+
};
|
|
8
|
+
export declare function Procedures<TContext = TNoContextProvided>(
|
|
9
|
+
/**
|
|
10
|
+
* Optionally provided builder to register Procedures
|
|
11
|
+
*/
|
|
12
|
+
builder?: {
|
|
13
|
+
onCreate?: (config: {
|
|
14
|
+
name: string;
|
|
15
|
+
config: {
|
|
16
|
+
hook?: (ctx: any, args?: any) => any;
|
|
17
|
+
schema?: {
|
|
18
|
+
args?: TJSONSchema;
|
|
19
|
+
data?: TJSONSchema;
|
|
20
|
+
};
|
|
21
|
+
validation?: {
|
|
22
|
+
args?: (args: any) => {
|
|
23
|
+
errors?: any[];
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
handler: (ctx: any, args?: any) => Promise<any>;
|
|
28
|
+
}) => void;
|
|
29
|
+
}): {
|
|
30
|
+
getProcedures: () => Map<string, {
|
|
31
|
+
name: string;
|
|
32
|
+
config: {
|
|
33
|
+
hook?: (ctx: any, args?: any) => any;
|
|
34
|
+
schema?: {
|
|
35
|
+
args?: TJSONSchema;
|
|
36
|
+
data?: TJSONSchema;
|
|
37
|
+
};
|
|
38
|
+
validation?: {
|
|
39
|
+
args?: (args: any) => {
|
|
40
|
+
errors?: any[];
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
handler: (ctx: TContext, args: any) => Promise<any>;
|
|
45
|
+
}>;
|
|
46
|
+
Create: <TName extends string, TArgs, TData, TLocalHook>(name: TName, config: {
|
|
47
|
+
hook?: (ctx: TContext & TLocalContext, args: TSchemaLib<TArgs>) => Promise<TLocalHook>;
|
|
48
|
+
schema?: {
|
|
49
|
+
args?: TArgs;
|
|
50
|
+
data?: TData;
|
|
51
|
+
};
|
|
52
|
+
}, handler: (ctx: TContext & TLocalContext & TLocalHook, args: TSchemaLib<TArgs>) => Promise<TSchemaLib<TData>>) => { [K in TName | `${TName}Schema`]: K extends TName ? (ctx: TContext, args: TSchemaLib<TArgs>) => Promise<TSchemaLib<TData>> : {
|
|
53
|
+
args?: TSchemaLib<TArgs> extends unknown ? undefined : TJSONSchema;
|
|
54
|
+
data?: TSchemaLib<TData> extends unknown ? undefined : TJSONSchema;
|
|
55
|
+
}; };
|
|
56
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ProcedureHookError, ProcedureError, ProcedureValidationError, } from './errors';
|
|
2
|
+
import { ProcedureCodes } from './procedure-codes';
|
|
3
|
+
import { computeSchema } from './schema/compute-schema';
|
|
4
|
+
export function Procedures(
|
|
5
|
+
/**
|
|
6
|
+
* Optionally provided builder to register Procedures
|
|
7
|
+
*/
|
|
8
|
+
builder) {
|
|
9
|
+
const procedures = new Map();
|
|
10
|
+
function Create(name, config, handler) {
|
|
11
|
+
const { jsonSchema, validations } = computeSchema(name, config.schema);
|
|
12
|
+
const registeredProcedure = {
|
|
13
|
+
name,
|
|
14
|
+
config: {
|
|
15
|
+
ctx: config.hook,
|
|
16
|
+
schema: jsonSchema,
|
|
17
|
+
validation: {
|
|
18
|
+
args: validations.args,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
handler: async (ctx, args) => {
|
|
22
|
+
try {
|
|
23
|
+
if (validations?.args) {
|
|
24
|
+
const { errors } = validations.args(args);
|
|
25
|
+
if (errors) {
|
|
26
|
+
throw new ProcedureValidationError(name, `Validation error for ${name} - ${errors.map((e) => e.message).join(', ')}`, errors);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const localCtx = {
|
|
30
|
+
error: (code, message, meta) => {
|
|
31
|
+
return new ProcedureError(name, code, message, meta);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
let computedLocalHook = {};
|
|
35
|
+
if (config.hook) {
|
|
36
|
+
try {
|
|
37
|
+
computedLocalHook = (await config.hook({ ...localCtx, ...ctx }, args));
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error instanceof ProcedureError) {
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
const err = new ProcedureHookError(name, `Error in hook for ${name} - ${error?.message}`);
|
|
44
|
+
err.stack = error.stack;
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return handler({
|
|
49
|
+
...ctx,
|
|
50
|
+
...localCtx,
|
|
51
|
+
...computedLocalHook,
|
|
52
|
+
}, args);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
if (error instanceof ProcedureHookError) {
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
else if (error instanceof ProcedureError) {
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
const err = new ProcedureError(name, ProcedureCodes.HANDLER_ERROR, `Error in handler for ${name} - ${error?.message}`);
|
|
63
|
+
err.stack = error.stack;
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
procedures.set(name, registeredProcedure);
|
|
70
|
+
if (builder?.onCreate) {
|
|
71
|
+
builder.onCreate(registeredProcedure);
|
|
72
|
+
}
|
|
73
|
+
// return for testing so that the rpc can be called/tested individually or internally
|
|
74
|
+
return {
|
|
75
|
+
[name]: registeredProcedure.handler,
|
|
76
|
+
[`${name}Schema`]: jsonSchema,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
getProcedures: () => {
|
|
81
|
+
return procedures;
|
|
82
|
+
},
|
|
83
|
+
Create,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,wBAAwB,GACzB,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAavD,MAAM,UAAU,UAAU;AACxB;;GAEG;AACH,OAgBC;IAED,MAAM,UAAU,GAiBZ,IAAI,GAAG,EAAE,CAAA;IAEb,SAAS,MAAM,CACb,IAAW,EACX,MASC,EACD,OAG+B;QAS/B,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,aAAa,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;QAEtE,MAAM,mBAAmB,GAAG;YAC1B,IAAI;YACJ,MAAM,EAAE;gBACN,GAAG,EAAE,MAAM,CAAC,IAAI;gBAChB,MAAM,EAAE,UAAU;gBAClB,UAAU,EAAE;oBACV,IAAI,EAAE,WAAW,CAAC,IAAI;iBACvB;aACF;YAED,OAAO,EAAE,KAAK,EAAE,GAAa,EAAE,IAAuB,EAAE,EAAE;gBACxD,IAAI,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