ts-typed-api 0.1.9 → 0.1.11
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/client-only.d.ts +2 -0
- package/dist/definition.js +1 -22
- package/dist/index.d.ts +2 -2
- package/dist/object-handlers.d.ts +3 -2
- package/package.json +1 -1
- package/src/client-only.ts +30 -1
- package/src/definition.ts +1 -26
- package/src/index.ts +2 -2
- package/src/object-handlers.ts +8 -6
package/dist/client-only.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { ApiClient, FetchHttpClientAdapter } from './client';
|
|
2
2
|
export { CreateApiDefinition, CreateResponses } from './definition';
|
|
3
3
|
export { z as ZodSchema } from 'zod';
|
|
4
|
+
export type { ApiDefinitionSchema, RouteSchema, UnifiedError, InferDataFromUnifiedResponse, ApiClientParams, ApiClientQuery, ApiClientBody, ApiResponse, ApiBody, ApiParams, ApiQuery, FileType, HttpSuccessStatusCode, HttpClientErrorStatusCode, HttpServerErrorStatusCode, AllowedInputStatusCode, AllowedResponseStatusCode } from './definition';
|
|
5
|
+
export type { HttpRequestOptions, HttpResponse, HttpClientAdapter, ApiCallResult, CallApiOptions } from './client';
|
package/dist/definition.js
CHANGED
|
@@ -48,32 +48,11 @@ exports.HttpServerErrorCodes = [500];
|
|
|
48
48
|
function makeSchemaStrict(schema) {
|
|
49
49
|
// Check if the schema has a .strict() method (ZodObject does)
|
|
50
50
|
if ('strict' in schema && typeof schema.strict === 'function') {
|
|
51
|
-
|
|
52
|
-
return makeStrict(schema);
|
|
53
|
-
// return schema.strict();
|
|
51
|
+
return schema.strict();
|
|
54
52
|
}
|
|
55
53
|
// For other schema types, return as-is
|
|
56
54
|
return schema;
|
|
57
55
|
}
|
|
58
|
-
function makeStrict(schema) {
|
|
59
|
-
if (schema instanceof zod_1.z.ZodObject) {
|
|
60
|
-
const newShape = {};
|
|
61
|
-
for (const [key, subSchema] of Object.entries(schema.shape)) {
|
|
62
|
-
if (subSchema instanceof zod_1.z.ZodArray) {
|
|
63
|
-
const strictItemSchema = makeStrict(subSchema.element);
|
|
64
|
-
newShape[key] = zod_1.z.array(strictItemSchema);
|
|
65
|
-
}
|
|
66
|
-
else if (subSchema instanceof zod_1.z.ZodObject) {
|
|
67
|
-
newShape[key] = makeStrict(subSchema);
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
newShape[key] = subSchema;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return zod_1.z.object(newShape).strict();
|
|
74
|
-
}
|
|
75
|
-
return schema;
|
|
76
|
-
}
|
|
77
56
|
// Helper function to create response schemas with unified structure and default 422 error
|
|
78
57
|
// Schemas input is now constrained to use AllowedInputStatusCode as keys.
|
|
79
58
|
function CreateResponses(schemas) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { ApiClient, FetchHttpClientAdapter } from './client';
|
|
2
|
-
export { CreateApiDefinition, CreateResponses } from './definition';
|
|
3
|
-
export { RegisterHandlers, EndpointMiddleware, UniversalEndpointMiddleware, SimpleMiddleware } from './object-handlers';
|
|
2
|
+
export { CreateApiDefinition, CreateResponses, ApiDefinitionSchema } from './definition';
|
|
3
|
+
export { RegisterHandlers, EndpointMiddleware, UniversalEndpointMiddleware, SimpleMiddleware, EndpointInfo } from './object-handlers';
|
|
4
4
|
export { File as UploadedFile } from './router';
|
|
5
5
|
export { z as ZodSchema } from 'zod';
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import express from "express";
|
|
2
2
|
import { ApiDefinitionSchema } from "./definition";
|
|
3
3
|
import { TypedRequest, TypedResponse } from "./router";
|
|
4
|
-
export type
|
|
4
|
+
export type EndpointInfo<TDef extends ApiDefinitionSchema = ApiDefinitionSchema> = {
|
|
5
5
|
[TDomain in keyof TDef['endpoints']]: {
|
|
6
6
|
domain: TDomain;
|
|
7
7
|
routeKey: keyof TDef['endpoints'][TDomain];
|
|
8
8
|
};
|
|
9
|
-
}[keyof TDef['endpoints']]
|
|
9
|
+
}[keyof TDef['endpoints']];
|
|
10
|
+
export type EndpointMiddleware<TDef extends ApiDefinitionSchema = ApiDefinitionSchema> = (req: express.Request, res: express.Response, next: express.NextFunction, endpointInfo: EndpointInfo<TDef>) => void | Promise<void>;
|
|
10
11
|
export type SimpleMiddleware = (req: express.Request, res: express.Response, next: express.NextFunction) => void | Promise<void>;
|
|
11
12
|
export type UniversalEndpointMiddleware = (req: express.Request, res: express.Response, next: express.NextFunction, endpointInfo: {
|
|
12
13
|
domain: string;
|
package/package.json
CHANGED
package/src/client-only.ts
CHANGED
|
@@ -1,4 +1,33 @@
|
|
|
1
1
|
// Client-only exports - no server dependencies
|
|
2
2
|
export { ApiClient, FetchHttpClientAdapter } from './client';
|
|
3
3
|
export { CreateApiDefinition, CreateResponses } from './definition';
|
|
4
|
-
export { z as ZodSchema } from 'zod';
|
|
4
|
+
export { z as ZodSchema } from 'zod';
|
|
5
|
+
|
|
6
|
+
// Re-export types that are safe for client use
|
|
7
|
+
export type {
|
|
8
|
+
ApiDefinitionSchema,
|
|
9
|
+
RouteSchema,
|
|
10
|
+
UnifiedError,
|
|
11
|
+
InferDataFromUnifiedResponse,
|
|
12
|
+
ApiClientParams,
|
|
13
|
+
ApiClientQuery,
|
|
14
|
+
ApiClientBody,
|
|
15
|
+
ApiResponse,
|
|
16
|
+
ApiBody,
|
|
17
|
+
ApiParams,
|
|
18
|
+
ApiQuery,
|
|
19
|
+
FileType,
|
|
20
|
+
HttpSuccessStatusCode,
|
|
21
|
+
HttpClientErrorStatusCode,
|
|
22
|
+
HttpServerErrorStatusCode,
|
|
23
|
+
AllowedInputStatusCode,
|
|
24
|
+
AllowedResponseStatusCode
|
|
25
|
+
} from './definition';
|
|
26
|
+
|
|
27
|
+
export type {
|
|
28
|
+
HttpRequestOptions,
|
|
29
|
+
HttpResponse,
|
|
30
|
+
HttpClientAdapter,
|
|
31
|
+
ApiCallResult,
|
|
32
|
+
CallApiOptions
|
|
33
|
+
} from './client';
|
package/src/definition.ts
CHANGED
|
@@ -78,37 +78,12 @@ type CreateResponsesReturnType<InputSchemas extends Partial<Record<AllowedInputS
|
|
|
78
78
|
function makeSchemaStrict(schema: ZodTypeAny): ZodTypeAny {
|
|
79
79
|
// Check if the schema has a .strict() method (ZodObject does)
|
|
80
80
|
if ('strict' in schema && typeof schema.strict === 'function') {
|
|
81
|
-
|
|
82
|
-
return makeStrict(schema)
|
|
83
|
-
// return schema.strict();
|
|
81
|
+
return schema.strict();
|
|
84
82
|
}
|
|
85
83
|
// For other schema types, return as-is
|
|
86
84
|
return schema;
|
|
87
85
|
}
|
|
88
86
|
|
|
89
|
-
function makeStrict<TSchema extends z.ZodType<any, any>>(
|
|
90
|
-
schema: TSchema extends z.ZodObject<any, any> ? TSchema : never
|
|
91
|
-
): TSchema {
|
|
92
|
-
if (schema instanceof z.ZodObject) {
|
|
93
|
-
const newShape: { [key: string]: z.ZodType<any, any> } = {}
|
|
94
|
-
|
|
95
|
-
for (const [key, subSchema] of Object.entries(schema.shape)) {
|
|
96
|
-
if (subSchema instanceof z.ZodArray) {
|
|
97
|
-
const strictItemSchema = makeStrict(subSchema.element)
|
|
98
|
-
newShape[key] = z.array(strictItemSchema)
|
|
99
|
-
} else if (subSchema instanceof z.ZodObject) {
|
|
100
|
-
newShape[key] = makeStrict(subSchema)
|
|
101
|
-
} else {
|
|
102
|
-
newShape[key] = subSchema as any
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return z.object(newShape).strict() as unknown as TSchema
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return schema
|
|
110
|
-
}
|
|
111
|
-
|
|
112
87
|
// Helper function to create response schemas with unified structure and default 422 error
|
|
113
88
|
// Schemas input is now constrained to use AllowedInputStatusCode as keys.
|
|
114
89
|
export function CreateResponses<TInputMap extends Partial<Record<AllowedInputStatusCode, InputSchemaOrMarker>>>(
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { ApiClient, FetchHttpClientAdapter } from './client';
|
|
2
|
-
export { CreateApiDefinition, CreateResponses } from './definition';
|
|
3
|
-
export { RegisterHandlers, EndpointMiddleware, UniversalEndpointMiddleware, SimpleMiddleware } from './object-handlers';
|
|
2
|
+
export { CreateApiDefinition, CreateResponses, ApiDefinitionSchema } from './definition';
|
|
3
|
+
export { RegisterHandlers, EndpointMiddleware, UniversalEndpointMiddleware, SimpleMiddleware, EndpointInfo } from './object-handlers';
|
|
4
4
|
export { File as UploadedFile } from './router';
|
|
5
5
|
export { z as ZodSchema } from 'zod';
|
package/src/object-handlers.ts
CHANGED
|
@@ -3,17 +3,19 @@ import { ApiDefinitionSchema } from "./definition";
|
|
|
3
3
|
import { registerRouteHandlers, SpecificRouteHandler } from "./handler";
|
|
4
4
|
import { TypedRequest, TypedResponse } from "./router";
|
|
5
5
|
|
|
6
|
+
export type EndpointInfo<TDef extends ApiDefinitionSchema = ApiDefinitionSchema> = {
|
|
7
|
+
[TDomain in keyof TDef['endpoints']]: {
|
|
8
|
+
domain: TDomain;
|
|
9
|
+
routeKey: keyof TDef['endpoints'][TDomain];
|
|
10
|
+
}
|
|
11
|
+
}[keyof TDef['endpoints']]
|
|
12
|
+
|
|
6
13
|
// Type for middleware function that receives endpoint information
|
|
7
14
|
export type EndpointMiddleware<TDef extends ApiDefinitionSchema = ApiDefinitionSchema> = (
|
|
8
15
|
req: express.Request,
|
|
9
16
|
res: express.Response,
|
|
10
17
|
next: express.NextFunction,
|
|
11
|
-
endpointInfo:
|
|
12
|
-
[TDomain in keyof TDef['endpoints']]: {
|
|
13
|
-
domain: TDomain;
|
|
14
|
-
routeKey: keyof TDef['endpoints'][TDomain];
|
|
15
|
-
}
|
|
16
|
-
}[keyof TDef['endpoints']]
|
|
18
|
+
endpointInfo: EndpointInfo<TDef>
|
|
17
19
|
) => void | Promise<void>;
|
|
18
20
|
|
|
19
21
|
// Type for simple middleware that doesn't need endpoint information
|