@xrpckit/schema 0.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/dist/index.d.ts +130 -0
- package/dist/index.js +45 -0
- package/package.json +40 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { ZodType, z } from 'zod';
|
|
2
|
+
|
|
3
|
+
interface EndpointDefinition {
|
|
4
|
+
type: 'query' | 'mutation';
|
|
5
|
+
input: ZodType;
|
|
6
|
+
output: ZodType;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Creates a query endpoint definition.
|
|
10
|
+
* Queries are typically used for read operations that don't modify server state.
|
|
11
|
+
*
|
|
12
|
+
* @param config - Configuration object containing input and output Zod schemas
|
|
13
|
+
* @param config.input - Zod schema for validating the input parameters
|
|
14
|
+
* @param config.output - Zod schema for validating the output response
|
|
15
|
+
* @returns An endpoint definition with type 'query'
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const getUser = query({
|
|
20
|
+
* input: z.object({ id: z.string() }),
|
|
21
|
+
* output: z.object({ id: z.string(), name: z.string() }),
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function query(config: {
|
|
26
|
+
input: ZodType;
|
|
27
|
+
output: ZodType;
|
|
28
|
+
}): EndpointDefinition;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a mutation endpoint definition.
|
|
31
|
+
* Mutations are typically used for write operations that modify server state.
|
|
32
|
+
*
|
|
33
|
+
* @param config - Configuration object containing input and output Zod schemas
|
|
34
|
+
* @param config.input - Zod schema for validating the input parameters
|
|
35
|
+
* @param config.output - Zod schema for validating the output response
|
|
36
|
+
* @returns An endpoint definition with type 'mutation'
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const createUser = mutation({
|
|
41
|
+
* input: z.object({ name: z.string(), email: z.string().email() }),
|
|
42
|
+
* output: z.object({ id: z.string(), name: z.string() }),
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function mutation(config: {
|
|
47
|
+
input: ZodType;
|
|
48
|
+
output: ZodType;
|
|
49
|
+
}): EndpointDefinition;
|
|
50
|
+
|
|
51
|
+
interface EndpointGroup {
|
|
52
|
+
[endpointName: string]: EndpointDefinition;
|
|
53
|
+
}
|
|
54
|
+
interface RouterDefinition {
|
|
55
|
+
[groupName: string]: EndpointGroup;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Get middleware associated with a router definition
|
|
59
|
+
* Used by parser to extract middleware metadata
|
|
60
|
+
*/
|
|
61
|
+
declare function getRouterMiddleware(router: RouterDefinition): Middleware[] | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Middleware function type for extending context
|
|
64
|
+
* Middleware receives the request and current context, and returns updated context
|
|
65
|
+
* Note: In generated code, middleware is implemented per-target (Go, TypeScript, etc.)
|
|
66
|
+
* This type is for documentation and type checking in the contract definition
|
|
67
|
+
*/
|
|
68
|
+
type Middleware<TContext = Record<string, unknown>> = (req: Request, context: TContext) => Promise<TContext | Response>;
|
|
69
|
+
/**
|
|
70
|
+
* Router configuration with optional middleware
|
|
71
|
+
*/
|
|
72
|
+
interface RouterConfig {
|
|
73
|
+
middleware?: Middleware[];
|
|
74
|
+
[groupName: string]: EndpointGroup | Middleware[] | undefined;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Creates an endpoint group containing one or more endpoints.
|
|
78
|
+
*
|
|
79
|
+
* @param endpoints - An object mapping endpoint names to their definitions (query or mutation)
|
|
80
|
+
* @returns The endpoint group with preserved types
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const greeting = createEndpoint({
|
|
85
|
+
* greet: query({
|
|
86
|
+
* input: z.object({ name: z.string() }),
|
|
87
|
+
* output: z.object({ message: z.string() }),
|
|
88
|
+
* }),
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare function createEndpoint<T extends EndpointGroup>(endpoints: T): T;
|
|
93
|
+
/**
|
|
94
|
+
* Creates a router with optional middleware support
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* // Without middleware
|
|
98
|
+
* const router = createRouter({
|
|
99
|
+
* greeting: createEndpoint({ ... })
|
|
100
|
+
* });
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* // With middleware
|
|
104
|
+
* const router = createRouter({
|
|
105
|
+
* middleware: [
|
|
106
|
+
* async (req, ctx) => ({ ...ctx, userId: extractUserId(req) })
|
|
107
|
+
* ],
|
|
108
|
+
* greeting: createEndpoint({ ... })
|
|
109
|
+
* });
|
|
110
|
+
*/
|
|
111
|
+
declare function createRouter<T extends RouterConfig | RouterDefinition>(config: T): T extends RouterConfig ? Omit<T, 'middleware'> : T;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Infer input type from an endpoint definition
|
|
115
|
+
* @example
|
|
116
|
+
* type CreateInput = InferInput<typeof router.todo.create>;
|
|
117
|
+
*/
|
|
118
|
+
type InferInput<T extends {
|
|
119
|
+
input: ZodType;
|
|
120
|
+
}> = z.infer<T['input']>;
|
|
121
|
+
/**
|
|
122
|
+
* Infer output type from an endpoint definition
|
|
123
|
+
* @example
|
|
124
|
+
* type CreateOutput = InferOutput<typeof router.todo.create>;
|
|
125
|
+
*/
|
|
126
|
+
type InferOutput<T extends {
|
|
127
|
+
output: ZodType;
|
|
128
|
+
}> = z.infer<T['output']>;
|
|
129
|
+
|
|
130
|
+
export { type EndpointDefinition, type EndpointGroup, type InferInput, type InferOutput, type Middleware, type RouterConfig, type RouterDefinition, createEndpoint, createRouter, getRouterMiddleware, mutation, query };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// src/router.ts
|
|
2
|
+
var routerMiddleware = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
function getRouterMiddleware(router) {
|
|
4
|
+
return routerMiddleware.get(router);
|
|
5
|
+
}
|
|
6
|
+
function isRouterConfig(value) {
|
|
7
|
+
return value && typeof value === "object" && "middleware" in value && Array.isArray(value.middleware);
|
|
8
|
+
}
|
|
9
|
+
function createEndpoint(endpoints) {
|
|
10
|
+
return endpoints;
|
|
11
|
+
}
|
|
12
|
+
function createRouter(config) {
|
|
13
|
+
if (isRouterConfig(config)) {
|
|
14
|
+
const { middleware, ...endpoints } = config;
|
|
15
|
+
const routerDef = endpoints;
|
|
16
|
+
if (middleware && middleware.length > 0) {
|
|
17
|
+
routerMiddleware.set(routerDef, middleware);
|
|
18
|
+
}
|
|
19
|
+
return routerDef;
|
|
20
|
+
}
|
|
21
|
+
return config;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/endpoint.ts
|
|
25
|
+
function query(config) {
|
|
26
|
+
return {
|
|
27
|
+
type: "query",
|
|
28
|
+
input: config.input,
|
|
29
|
+
output: config.output
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function mutation(config) {
|
|
33
|
+
return {
|
|
34
|
+
type: "mutation",
|
|
35
|
+
input: config.input,
|
|
36
|
+
output: config.output
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
createEndpoint,
|
|
41
|
+
createRouter,
|
|
42
|
+
getRouterMiddleware,
|
|
43
|
+
mutation,
|
|
44
|
+
query
|
|
45
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xrpckit/schema",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/mwesox/xrpc.git",
|
|
9
|
+
"directory": "packages/schema"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"bun": "./src/index.ts",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup src/index.ts --format esm --dts --clean"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"zod": "^4.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^22.0.0",
|
|
34
|
+
"tsup": "^8.0.0",
|
|
35
|
+
"typescript": "^5.0.0"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=18.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|