@orpc/contract 0.0.0-next.ee0aeaf → 0.0.0-next.f99e554

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 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
- return item instanceof ContractProcedure;
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
- pushTag(...tags) {
44
+ unshiftTag(...tags) {
39
45
  return new _DecoratedContractProcedure({
40
46
  ...this["~orpc"],
41
47
  route: {
42
48
  ...this["~orpc"].route,
43
- tags: [...this["~orpc"].route?.tags ?? [], ...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
- const item = router[key];
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
  }
@@ -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
@@ -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
- pushTag(...tags: string[]): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
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
  }
@@ -5,7 +5,57 @@ export interface RouteOptions {
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?: 'compact' | 'detailed';
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?: 'compact' | 'detailed';
9
59
  }
10
60
  export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema> {
11
61
  route?: RouteOptions;
@@ -1,12 +1,12 @@
1
1
  import type { ANY_CONTRACT_PROCEDURE, ContractProcedure } from './procedure';
2
2
  import type { SchemaInput, SchemaOutput } from './types';
3
- export interface ContractRouter {
4
- [k: string]: ANY_CONTRACT_PROCEDURE | ContractRouter;
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 InferContractRouterOutputs<T extends ContractRouter> = {
10
- [K in keyof T]: T[K] extends ContractProcedure<any, infer UOutputSchema> ? SchemaOutput<UOutputSchema> : T[K] extends ContractRouter ? InferContractRouterOutputs<T[K]> : never;
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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/contract",
3
3
  "type": "module",
4
- "version": "0.0.0-next.ee0aeaf",
4
+ "version": "0.0.0-next.f99e554",
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.ee0aeaf"
33
+ "@orpc/shared": "0.0.0-next.f99e554"
34
34
  },
35
35
  "devDependencies": {
36
36
  "arktype": "2.0.0-rc.26",