@orpc/contract 0.0.0-next.4555a17 → 0.0.0-next.5204560
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.js +50 -16
- package/dist/src/builder.d.ts +2 -2
- package/dist/src/config.d.ts +31 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/procedure-decorated.d.ts +1 -1
- package/dist/src/procedure.d.ts +52 -2
- package/dist/src/router.d.ts +7 -7
- package/dist/src/types.d.ts +2 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -3,11 +3,17 @@ var ContractProcedure = class {
|
|
|
3
3
|
"~type" = "ContractProcedure";
|
|
4
4
|
"~orpc";
|
|
5
5
|
constructor(def) {
|
|
6
|
+
if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
|
|
7
|
+
throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
|
|
8
|
+
}
|
|
6
9
|
this["~orpc"] = def;
|
|
7
10
|
}
|
|
8
11
|
};
|
|
9
12
|
function isContractProcedure(item) {
|
|
10
|
-
|
|
13
|
+
if (item instanceof ContractProcedure) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && "~type" in item && item["~type"] === "ContractProcedure" && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "InputSchema" in item["~orpc"] && "OutputSchema" in item["~orpc"];
|
|
11
17
|
}
|
|
12
18
|
|
|
13
19
|
// src/procedure-decorated.ts
|
|
@@ -35,12 +41,15 @@ var DecoratedContractProcedure = class _DecoratedContractProcedure extends Contr
|
|
|
35
41
|
} : void 0
|
|
36
42
|
});
|
|
37
43
|
}
|
|
38
|
-
|
|
44
|
+
unshiftTag(...tags) {
|
|
39
45
|
return new _DecoratedContractProcedure({
|
|
40
46
|
...this["~orpc"],
|
|
41
47
|
route: {
|
|
42
48
|
...this["~orpc"].route,
|
|
43
|
-
tags: [
|
|
49
|
+
tags: [
|
|
50
|
+
...tags,
|
|
51
|
+
...this["~orpc"].route?.tags?.filter((tag) => !tags.includes(tag)) ?? []
|
|
52
|
+
]
|
|
44
53
|
}
|
|
45
54
|
});
|
|
46
55
|
}
|
|
@@ -80,21 +89,19 @@ var ContractRouterBuilder = class _ContractRouterBuilder {
|
|
|
80
89
|
});
|
|
81
90
|
}
|
|
82
91
|
router(router) {
|
|
92
|
+
if (isContractProcedure(router)) {
|
|
93
|
+
let decorated = DecoratedContractProcedure.decorate(router);
|
|
94
|
+
if (this["~orpc"].tags) {
|
|
95
|
+
decorated = decorated.unshiftTag(...this["~orpc"].tags);
|
|
96
|
+
}
|
|
97
|
+
if (this["~orpc"].prefix) {
|
|
98
|
+
decorated = decorated.prefix(this["~orpc"].prefix);
|
|
99
|
+
}
|
|
100
|
+
return decorated;
|
|
101
|
+
}
|
|
83
102
|
const adapted = {};
|
|
84
103
|
for (const key in router) {
|
|
85
|
-
|
|
86
|
-
if (isContractProcedure(item)) {
|
|
87
|
-
let decorated = DecoratedContractProcedure.decorate(item);
|
|
88
|
-
if (this["~orpc"].tags) {
|
|
89
|
-
decorated = decorated.pushTag(...this["~orpc"].tags);
|
|
90
|
-
}
|
|
91
|
-
if (this["~orpc"].prefix) {
|
|
92
|
-
decorated = decorated.prefix(this["~orpc"].prefix);
|
|
93
|
-
}
|
|
94
|
-
adapted[key] = decorated;
|
|
95
|
-
} else {
|
|
96
|
-
adapted[key] = this.router(item);
|
|
97
|
-
}
|
|
104
|
+
adapted[key] = this.router(router[key]);
|
|
98
105
|
}
|
|
99
106
|
return adapted;
|
|
100
107
|
}
|
|
@@ -138,6 +145,31 @@ var ContractBuilder = class {
|
|
|
138
145
|
}
|
|
139
146
|
};
|
|
140
147
|
|
|
148
|
+
// src/config.ts
|
|
149
|
+
var DEFAULT_CONFIG = {
|
|
150
|
+
defaultMethod: "POST",
|
|
151
|
+
defaultSuccessStatus: 200,
|
|
152
|
+
defaultInputStructure: "compact",
|
|
153
|
+
defaultOutputStructure: "compact"
|
|
154
|
+
};
|
|
155
|
+
var GLOBAL_CONFIG_REF = { value: DEFAULT_CONFIG };
|
|
156
|
+
function configGlobal(config) {
|
|
157
|
+
if (config.defaultSuccessStatus !== void 0 && (config.defaultSuccessStatus < 200 || config.defaultSuccessStatus > 299)) {
|
|
158
|
+
throw new Error("[configGlobal] The defaultSuccessStatus must be between 200 and 299");
|
|
159
|
+
}
|
|
160
|
+
GLOBAL_CONFIG_REF.value = config;
|
|
161
|
+
}
|
|
162
|
+
function fallbackToGlobalConfig(key, value) {
|
|
163
|
+
if (value === void 0) {
|
|
164
|
+
const fallback = GLOBAL_CONFIG_REF.value[key];
|
|
165
|
+
if (fallback === void 0) {
|
|
166
|
+
return DEFAULT_CONFIG[key];
|
|
167
|
+
}
|
|
168
|
+
return fallback;
|
|
169
|
+
}
|
|
170
|
+
return value;
|
|
171
|
+
}
|
|
172
|
+
|
|
141
173
|
// src/index.ts
|
|
142
174
|
var oc = new ContractBuilder();
|
|
143
175
|
export {
|
|
@@ -145,6 +177,8 @@ export {
|
|
|
145
177
|
ContractProcedure,
|
|
146
178
|
ContractRouterBuilder,
|
|
147
179
|
DecoratedContractProcedure,
|
|
180
|
+
configGlobal,
|
|
181
|
+
fallbackToGlobalConfig,
|
|
148
182
|
isContractProcedure,
|
|
149
183
|
oc
|
|
150
184
|
};
|
package/dist/src/builder.d.ts
CHANGED
|
@@ -7,8 +7,8 @@ export declare class ContractBuilder {
|
|
|
7
7
|
prefix(prefix: HTTPPath): ContractRouterBuilder;
|
|
8
8
|
tag(...tags: string[]): ContractRouterBuilder;
|
|
9
9
|
route(route: RouteOptions): DecoratedContractProcedure<undefined, undefined>;
|
|
10
|
-
input<U extends Schema>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, undefined>;
|
|
11
|
-
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<undefined, U>;
|
|
10
|
+
input<U extends Schema = undefined>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, undefined>;
|
|
11
|
+
output<U extends Schema = undefined>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<undefined, U>;
|
|
12
12
|
router<T extends ContractRouter>(router: T): T;
|
|
13
13
|
}
|
|
14
14
|
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { HTTPMethod, InputStructure } from './types';
|
|
2
|
+
export interface ORPCConfig {
|
|
3
|
+
/**
|
|
4
|
+
* @default 'POST'
|
|
5
|
+
*/
|
|
6
|
+
defaultMethod?: HTTPMethod;
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @default 200
|
|
10
|
+
*/
|
|
11
|
+
defaultSuccessStatus?: number;
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @default 'compact'
|
|
15
|
+
*/
|
|
16
|
+
defaultInputStructure?: InputStructure;
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @default 'compact'
|
|
20
|
+
*/
|
|
21
|
+
defaultOutputStructure?: InputStructure;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Set the global configuration, this configuration can effect entire project
|
|
25
|
+
*/
|
|
26
|
+
export declare function configGlobal(config: ORPCConfig): void;
|
|
27
|
+
/**
|
|
28
|
+
* Fallback the value to the global config if it is undefined
|
|
29
|
+
*/
|
|
30
|
+
export declare function fallbackToGlobalConfig<T extends keyof ORPCConfig>(key: T, value: ORPCConfig[T]): Exclude<ORPCConfig[T], undefined>;
|
|
31
|
+
//# sourceMappingURL=config.d.ts.map
|
package/dist/src/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare class DecoratedContractProcedure<TInputSchema extends Schema, TOu
|
|
|
5
5
|
static decorate<UInputSchema extends Schema = undefined, UOutputSchema extends Schema = undefined>(procedure: ContractProcedure<UInputSchema, UOutputSchema>): DecoratedContractProcedure<UInputSchema, UOutputSchema>;
|
|
6
6
|
route(route: RouteOptions): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
7
7
|
prefix(prefix: HTTPPath): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
8
|
-
|
|
8
|
+
unshiftTag(...tags: string[]): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
9
9
|
input<U extends Schema = undefined>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, TOutputSchema>;
|
|
10
10
|
output<U extends Schema = undefined>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<TInputSchema, U>;
|
|
11
11
|
}
|
package/dist/src/procedure.d.ts
CHANGED
|
@@ -1,11 +1,61 @@
|
|
|
1
|
-
import type { HTTPMethod, HTTPPath, Schema, SchemaOutput } from './types';
|
|
1
|
+
import type { HTTPMethod, HTTPPath, InputStructure, OutputStructure, Schema, SchemaOutput } from './types';
|
|
2
2
|
export interface RouteOptions {
|
|
3
3
|
method?: HTTPMethod;
|
|
4
4
|
path?: HTTPPath;
|
|
5
5
|
summary?: string;
|
|
6
6
|
description?: string;
|
|
7
7
|
deprecated?: boolean;
|
|
8
|
-
tags?: string[];
|
|
8
|
+
tags?: readonly string[];
|
|
9
|
+
/**
|
|
10
|
+
* The status code of the response when the procedure is successful.
|
|
11
|
+
*
|
|
12
|
+
* @default 200
|
|
13
|
+
*/
|
|
14
|
+
successStatus?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
|
|
17
|
+
*
|
|
18
|
+
* @option 'compact'
|
|
19
|
+
* Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
|
|
20
|
+
*
|
|
21
|
+
* @option 'detailed'
|
|
22
|
+
* Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
|
|
23
|
+
*
|
|
24
|
+
* Example:
|
|
25
|
+
* ```ts
|
|
26
|
+
* const input = {
|
|
27
|
+
* params: { id: 1 },
|
|
28
|
+
* query: { search: 'hello' },
|
|
29
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
30
|
+
* body: { name: 'John' },
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @default 'compact'
|
|
35
|
+
*/
|
|
36
|
+
inputStructure?: InputStructure;
|
|
37
|
+
/**
|
|
38
|
+
* Determines how the response should be structured based on the output.
|
|
39
|
+
*
|
|
40
|
+
* @option 'compact'
|
|
41
|
+
* Includes only the body data, encoded directly in the response.
|
|
42
|
+
*
|
|
43
|
+
* @option 'detailed'
|
|
44
|
+
* Separates the output into `headers` and `body` fields.
|
|
45
|
+
* - `headers`: Custom headers to merge with the response headers.
|
|
46
|
+
* - `body`: The response data.
|
|
47
|
+
*
|
|
48
|
+
* Example:
|
|
49
|
+
* ```ts
|
|
50
|
+
* const output = {
|
|
51
|
+
* headers: { 'x-custom-header': 'value' },
|
|
52
|
+
* body: { message: 'Hello, world!' },
|
|
53
|
+
* };
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @default 'compact'
|
|
57
|
+
*/
|
|
58
|
+
outputStructure?: OutputStructure;
|
|
9
59
|
}
|
|
10
60
|
export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema> {
|
|
11
61
|
route?: RouteOptions;
|
package/dist/src/router.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { ANY_CONTRACT_PROCEDURE, ContractProcedure } from './procedure';
|
|
2
2
|
import type { SchemaInput, SchemaOutput } from './types';
|
|
3
|
-
export
|
|
4
|
-
[k: string]:
|
|
5
|
-
}
|
|
6
|
-
export type InferContractRouterInputs<T extends ContractRouter> = {
|
|
7
|
-
[K in keyof T]: T[K] extends ContractProcedure<infer UInputSchema, any> ? SchemaInput<UInputSchema> : T[K] extends ContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
3
|
+
export type ContractRouter = ANY_CONTRACT_PROCEDURE | {
|
|
4
|
+
[k: string]: ContractRouter;
|
|
8
5
|
};
|
|
9
|
-
export type
|
|
10
|
-
[K in keyof T]: T[K] extends
|
|
6
|
+
export type InferContractRouterInputs<T extends ContractRouter> = T extends ContractProcedure<infer UInputSchema, any> ? SchemaInput<UInputSchema> : {
|
|
7
|
+
[K in keyof T]: T[K] extends ContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
8
|
+
};
|
|
9
|
+
export type InferContractRouterOutputs<T extends ContractRouter> = T extends ContractProcedure<any, infer UOutputSchema> ? SchemaOutput<UOutputSchema> : {
|
|
10
|
+
[K in keyof T]: T[K] extends ContractRouter ? InferContractRouterOutputs<T[K]> : never;
|
|
11
11
|
};
|
|
12
12
|
//# sourceMappingURL=router.d.ts.map
|
package/dist/src/types.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
2
|
export type HTTPPath = `/${string}`;
|
|
3
3
|
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
4
|
+
export type InputStructure = 'compact' | 'detailed';
|
|
5
|
+
export type OutputStructure = 'compact' | 'detailed';
|
|
4
6
|
export type Schema = StandardSchemaV1 | undefined;
|
|
5
7
|
export type SchemaInput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : TFallback;
|
|
6
8
|
export type SchemaOutput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TFallback;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/contract",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.5204560",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@standard-schema/spec": "1.0.0-beta.4",
|
|
33
|
-
"@orpc/shared": "0.0.0-next.
|
|
33
|
+
"@orpc/shared": "0.0.0-next.5204560"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"arktype": "2.0.0-rc.26",
|