@arrirpc/typebox-adapter 0.45.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/LICENSE +9 -0
- package/README.md +42 -0
- package/dist/index.cjs +66 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +64 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2024 Joshua Sosso
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Arri Typebox Adapter
|
|
2
|
+
|
|
3
|
+
Let's you reuse your [Typebox](https://github.com/sinclairzx81/typebox) schemas with Arri-RPC
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @arrirpc/typebox-adapter
|
|
9
|
+
|
|
10
|
+
pnpm install @arrirpc/typebox-adapter
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Simply wrap your typebox schemas with `typeboxAdapter()` to use them with arrirpc. Additionally metadata such as `$id` and `description` will be converted over.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// updateUser.rpc.ts
|
|
19
|
+
import { defineRpc } from "@arrirpc/server";
|
|
20
|
+
import { typeboxAdapter } from "@arrripc/typebox-adapter";
|
|
21
|
+
import { Type } from "typebox";
|
|
22
|
+
|
|
23
|
+
const User = Type.Object(
|
|
24
|
+
{
|
|
25
|
+
id: Type.String(),
|
|
26
|
+
name: Type.String(),
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
$id: "User",
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
export default defineRpc({
|
|
34
|
+
params: typeboxAdapter(User),
|
|
35
|
+
response: typeboxAdapter(User),
|
|
36
|
+
handler({ params }) {
|
|
37
|
+
// typebox keys are now available
|
|
38
|
+
console.log(params.id);
|
|
39
|
+
return params;
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const schema = require('@arrirpc/schema');
|
|
4
|
+
const typebox = require('@sinclair/typebox');
|
|
5
|
+
const compiler = require('@sinclair/typebox/compiler');
|
|
6
|
+
const value = require('@sinclair/typebox/value');
|
|
7
|
+
const jsonSchemaToJtd = require('json-schema-to-jtd');
|
|
8
|
+
|
|
9
|
+
function typeboxAdapter(input) {
|
|
10
|
+
const schema$1 = jsonSchemaToJtd.jsonSchemaToJtdSchema(input);
|
|
11
|
+
const compiled = compiler.TypeCompiler.Compile(input);
|
|
12
|
+
return {
|
|
13
|
+
...schema$1,
|
|
14
|
+
metadata: {
|
|
15
|
+
id: input.$id ?? input.title,
|
|
16
|
+
description: input.description,
|
|
17
|
+
[schema.SCHEMA_METADATA]: {
|
|
18
|
+
_isAdaptedSchema: true,
|
|
19
|
+
output: {},
|
|
20
|
+
optional: input[typebox.OptionalKind] === "Optional",
|
|
21
|
+
parse(val) {
|
|
22
|
+
if (typeof val === "string") {
|
|
23
|
+
const parsedVal = JSON.parse(val);
|
|
24
|
+
if (compiled.Check(parsedVal)) {
|
|
25
|
+
return parsedVal;
|
|
26
|
+
}
|
|
27
|
+
throw typeboxErrorsToArriError(
|
|
28
|
+
compiled.Errors(parsedVal)
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
if (compiled.Check(val)) {
|
|
32
|
+
return val;
|
|
33
|
+
}
|
|
34
|
+
throw typeboxErrorsToArriError(compiled.Errors(val));
|
|
35
|
+
},
|
|
36
|
+
coerce(val) {
|
|
37
|
+
return value.Value.Cast(input, val);
|
|
38
|
+
},
|
|
39
|
+
validate(val) {
|
|
40
|
+
return compiled.Check(val);
|
|
41
|
+
},
|
|
42
|
+
serialize(val) {
|
|
43
|
+
return compiled.Encode(val);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function typeboxErrorsToArriError(errs) {
|
|
50
|
+
const mappedErrs = [];
|
|
51
|
+
for (const err of errs) {
|
|
52
|
+
const obj = {
|
|
53
|
+
message: err.message,
|
|
54
|
+
instancePath: err.path,
|
|
55
|
+
schemaPath: "",
|
|
56
|
+
data: err.value
|
|
57
|
+
};
|
|
58
|
+
mappedErrs.push(obj);
|
|
59
|
+
}
|
|
60
|
+
return new schema.ValidationError({
|
|
61
|
+
message: "Error validating input",
|
|
62
|
+
errors: mappedErrs
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
exports.typeboxAdapter = typeboxAdapter;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AAdaptedObjectSchema, AAdaptedRecordSchema, AAdaptedSchema } from '@arrirpc/schema';
|
|
2
|
+
import { TSchema, TObject, Static, TRecord } from '@sinclair/typebox';
|
|
3
|
+
|
|
4
|
+
declare function typeboxAdapter<TInput extends TSchema>(input: TInput): TInput extends TObject ? AAdaptedObjectSchema<Static<TInput>> : TInput extends TRecord ? AAdaptedRecordSchema<Static<TInput> extends Record<string, any> ? Static<TInput>[string] : any> : AAdaptedSchema<Static<TInput>>;
|
|
5
|
+
|
|
6
|
+
export { typeboxAdapter };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AAdaptedObjectSchema, AAdaptedRecordSchema, AAdaptedSchema } from '@arrirpc/schema';
|
|
2
|
+
import { TSchema, TObject, Static, TRecord } from '@sinclair/typebox';
|
|
3
|
+
|
|
4
|
+
declare function typeboxAdapter<TInput extends TSchema>(input: TInput): TInput extends TObject ? AAdaptedObjectSchema<Static<TInput>> : TInput extends TRecord ? AAdaptedRecordSchema<Static<TInput> extends Record<string, any> ? Static<TInput>[string] : any> : AAdaptedSchema<Static<TInput>>;
|
|
5
|
+
|
|
6
|
+
export { typeboxAdapter };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AAdaptedObjectSchema, AAdaptedRecordSchema, AAdaptedSchema } from '@arrirpc/schema';
|
|
2
|
+
import { TSchema, TObject, Static, TRecord } from '@sinclair/typebox';
|
|
3
|
+
|
|
4
|
+
declare function typeboxAdapter<TInput extends TSchema>(input: TInput): TInput extends TObject ? AAdaptedObjectSchema<Static<TInput>> : TInput extends TRecord ? AAdaptedRecordSchema<Static<TInput> extends Record<string, any> ? Static<TInput>[string] : any> : AAdaptedSchema<Static<TInput>>;
|
|
5
|
+
|
|
6
|
+
export { typeboxAdapter };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { SCHEMA_METADATA, ValidationError } from '@arrirpc/schema';
|
|
2
|
+
import { OptionalKind } from '@sinclair/typebox';
|
|
3
|
+
import { TypeCompiler } from '@sinclair/typebox/compiler';
|
|
4
|
+
import { Value } from '@sinclair/typebox/value';
|
|
5
|
+
import { jsonSchemaToJtdSchema } from 'json-schema-to-jtd';
|
|
6
|
+
|
|
7
|
+
function typeboxAdapter(input) {
|
|
8
|
+
const schema = jsonSchemaToJtdSchema(input);
|
|
9
|
+
const compiled = TypeCompiler.Compile(input);
|
|
10
|
+
return {
|
|
11
|
+
...schema,
|
|
12
|
+
metadata: {
|
|
13
|
+
id: input.$id ?? input.title,
|
|
14
|
+
description: input.description,
|
|
15
|
+
[SCHEMA_METADATA]: {
|
|
16
|
+
_isAdaptedSchema: true,
|
|
17
|
+
output: {},
|
|
18
|
+
optional: input[OptionalKind] === "Optional",
|
|
19
|
+
parse(val) {
|
|
20
|
+
if (typeof val === "string") {
|
|
21
|
+
const parsedVal = JSON.parse(val);
|
|
22
|
+
if (compiled.Check(parsedVal)) {
|
|
23
|
+
return parsedVal;
|
|
24
|
+
}
|
|
25
|
+
throw typeboxErrorsToArriError(
|
|
26
|
+
compiled.Errors(parsedVal)
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
if (compiled.Check(val)) {
|
|
30
|
+
return val;
|
|
31
|
+
}
|
|
32
|
+
throw typeboxErrorsToArriError(compiled.Errors(val));
|
|
33
|
+
},
|
|
34
|
+
coerce(val) {
|
|
35
|
+
return Value.Cast(input, val);
|
|
36
|
+
},
|
|
37
|
+
validate(val) {
|
|
38
|
+
return compiled.Check(val);
|
|
39
|
+
},
|
|
40
|
+
serialize(val) {
|
|
41
|
+
return compiled.Encode(val);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function typeboxErrorsToArriError(errs) {
|
|
48
|
+
const mappedErrs = [];
|
|
49
|
+
for (const err of errs) {
|
|
50
|
+
const obj = {
|
|
51
|
+
message: err.message,
|
|
52
|
+
instancePath: err.path,
|
|
53
|
+
schemaPath: "",
|
|
54
|
+
data: err.value
|
|
55
|
+
};
|
|
56
|
+
mappedErrs.push(obj);
|
|
57
|
+
}
|
|
58
|
+
return new ValidationError({
|
|
59
|
+
message: "Error validating input",
|
|
60
|
+
errors: mappedErrs
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { typeboxAdapter };
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arrirpc/typebox-adapter",
|
|
3
|
+
"version": "0.45.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": {
|
|
13
|
+
"name": "joshmossas",
|
|
14
|
+
"url": "https://github.com/joshmossas"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/modiimedia/arri/issues"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/modiimedia/arri.git",
|
|
22
|
+
"directory": "tooling/schema-typebox-adapter"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@arrirpc/schema": "0.45.0",
|
|
26
|
+
"json-schema-to-jtd": "0.45.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@arrirpc/server": "0.45.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@sinclair/typebox": "^0.32"
|
|
33
|
+
}
|
|
34
|
+
}
|