typed-bridge 2.0.14 → 2.1.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/dist/bridge/index.js +28 -18
- package/dist/demo/bridge/index.d.ts +185 -15
- package/dist/demo/bridge/index.js +13 -1
- package/dist/demo/bridge/order/index.d.ts +39 -0
- package/dist/demo/bridge/order/index.js +126 -0
- package/dist/demo/bridge/order/types.d.ts +130 -0
- package/dist/demo/bridge/order/types.js +104 -0
- package/dist/demo/bridge/product/index.d.ts +16 -0
- package/dist/demo/bridge/product/index.js +67 -0
- package/dist/demo/bridge/product/types.d.ts +24 -0
- package/dist/demo/bridge/product/types.js +27 -0
- package/dist/demo/bridge/user/index.d.ts +16 -5
- package/dist/demo/bridge/user/index.js +27 -9
- package/dist/demo/bridge/user/types.d.ts +21 -25
- package/dist/demo/bridge/user/types.js +20 -10
- package/dist/demo/index.d.ts +1 -1
- package/dist/demo/index.js +14 -3
- package/dist/demo/middleware.d.ts +1 -0
- package/dist/demo/middleware.js +58 -0
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.js +5 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -2
- package/dist/scripts/buildTypeBridge.js +6 -2
- package/dist/scripts/cli.js +1 -1
- package/dist/scripts/typedBridgeCleaner.js +103 -9
- package/package.json +11 -12
- package/readme.md +168 -101
- package/test/bridge.ts +219 -42
- package/test/index.ts +233 -20
- package/context.md +0 -174
- package/dist/server/index.d.ts +0 -4
- package/dist/server/index.js +0 -17
- package/tmp/add-middleware.md +0 -17
- package/tmp/bridge.ts +0 -40
- package/tmp/configuration.md +0 -46
- package/tmp/context-setup.md +0 -38
- package/tmp/request-validation.md +0 -24
package/dist/bridge/index.js
CHANGED
|
@@ -27,7 +27,8 @@ const createBridge = (bridge, port, path = '/bridge') => {
|
|
|
27
27
|
app.use(express_1.default.json());
|
|
28
28
|
app.use(express_1.default.urlencoded({ extended: true }));
|
|
29
29
|
// Typed bridge middleware
|
|
30
|
-
let requestId =
|
|
30
|
+
let requestId = 0;
|
|
31
|
+
let activeRequests = 0;
|
|
31
32
|
app.use((req, res, next) => {
|
|
32
33
|
const _req = req;
|
|
33
34
|
const xForwardedFor = req.headers['x-forwarded-for'];
|
|
@@ -36,16 +37,17 @@ const createBridge = (bridge, port, path = '/bridge') => {
|
|
|
36
37
|
: (xForwardedFor || '').split(', ')[0] || req.socket.remoteAddress || '';
|
|
37
38
|
if (ip === '::1')
|
|
38
39
|
ip = '127.0.0.1';
|
|
40
|
+
// Set typed bridge header
|
|
41
|
+
res.setHeader('X-Powered-By', 'typed-bridge');
|
|
42
|
+
requestId++;
|
|
43
|
+
activeRequests++;
|
|
39
44
|
// Bind data
|
|
40
45
|
_req.bind = {
|
|
41
|
-
id:
|
|
46
|
+
id: requestId,
|
|
42
47
|
ts: Date.now(),
|
|
43
48
|
args: {},
|
|
44
49
|
ip
|
|
45
50
|
};
|
|
46
|
-
// Set typed bridge header
|
|
47
|
-
res.setHeader('X-Powered-By', 'typed-bridge');
|
|
48
|
-
requestId++;
|
|
49
51
|
// Log request
|
|
50
52
|
if (__1.tbConfig.logs.request) {
|
|
51
53
|
console.log(chalk_1.default.blueBright(`REQ | ${new Date().toISOString()} | ${requestId} :: ${req.method} | ${req.path} | ${ip}`));
|
|
@@ -53,6 +55,7 @@ const createBridge = (bridge, port, path = '/bridge') => {
|
|
|
53
55
|
// Log response
|
|
54
56
|
const startTime = Date.now();
|
|
55
57
|
res.on('finish', () => {
|
|
58
|
+
activeRequests--;
|
|
56
59
|
const log = `RES | ${new Date().toISOString()} | ${requestId} :: ${res.statusCode} | ${Date.now() - startTime}ms`;
|
|
57
60
|
if (__1.tbConfig.logs.response)
|
|
58
61
|
console.log(res.statusCode < 400 ? chalk_1.default.green(log) : chalk_1.default.red(log));
|
|
@@ -72,20 +75,26 @@ const createBridge = (bridge, port, path = '/bridge') => {
|
|
|
72
75
|
app.use((req, res, next) => {
|
|
73
76
|
setTimeout(next, __1.tbConfig.responseDelay);
|
|
74
77
|
});
|
|
75
|
-
app.use(path, bridgeHandler(bridge));
|
|
76
78
|
// Server health
|
|
77
79
|
app.get(path_1.default.join(path, 'health'), (req, res) => {
|
|
78
|
-
res.status(200).json({
|
|
80
|
+
res.status(200).json({
|
|
81
|
+
status: 'OK',
|
|
82
|
+
activeRequests: activeRequests - 1,
|
|
83
|
+
totalRequests: requestId,
|
|
84
|
+
timestamp: new Date().toISOString()
|
|
85
|
+
});
|
|
79
86
|
});
|
|
87
|
+
app.use(path, bridgeHandler(bridge));
|
|
80
88
|
const server = app.listen(port, () => (0, helpers_1.printStartLogs)(port));
|
|
81
89
|
let shuttingDown = false;
|
|
82
90
|
const shutdown = () => {
|
|
83
91
|
if (shuttingDown)
|
|
84
92
|
return;
|
|
85
93
|
shuttingDown = true;
|
|
86
|
-
server.close()
|
|
87
|
-
|
|
88
|
-
|
|
94
|
+
server.close(() => {
|
|
95
|
+
(0, helpers_1.printStopLogs)();
|
|
96
|
+
shutdownCallback();
|
|
97
|
+
});
|
|
89
98
|
};
|
|
90
99
|
process.on('SIGINT', () => shutdown());
|
|
91
100
|
process.on('SIGTERM', () => shutdown());
|
|
@@ -108,14 +117,15 @@ const bridgeHandler = (bridge) => async (req, res) => {
|
|
|
108
117
|
return res.status(404).json({ error });
|
|
109
118
|
}
|
|
110
119
|
context = {};
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
120
|
+
const matchingMiddlewares = middlewares
|
|
121
|
+
.filter(m => (0, helpers_1.matchesPattern)(path, m.pattern))
|
|
122
|
+
.sort((a, b) => (0, helpers_1.getPatternSpecificity)(a.pattern) - (0, helpers_1.getPatternSpecificity)(b.pattern));
|
|
123
|
+
for (const middleware of matchingMiddlewares) {
|
|
124
|
+
const result = await middleware.handler(req, res);
|
|
125
|
+
if (result?.next === false)
|
|
126
|
+
return;
|
|
127
|
+
if (result?.context)
|
|
128
|
+
context = { ...context, ...result.context };
|
|
119
129
|
}
|
|
120
130
|
res.json((await serverFunction(args, context)) || {});
|
|
121
131
|
}
|
|
@@ -1,28 +1,198 @@
|
|
|
1
1
|
import * as user from './user';
|
|
2
|
+
import * as product from './product';
|
|
3
|
+
import * as order from './order';
|
|
2
4
|
declare const _default: {
|
|
3
|
-
'user.fetch': (args: import("zod").
|
|
5
|
+
'user.fetch': (args: import("zod").infer<import("zod").ZodObject<{
|
|
4
6
|
id: import("zod").ZodNumber;
|
|
5
|
-
},
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}>>, context: {
|
|
10
|
-
id: number;
|
|
11
|
-
}) => Promise<import("zod").TypeOf<import("zod").ZodObject<{
|
|
7
|
+
}, import("zod/v4/core").$strip>>, context: {
|
|
8
|
+
requestedAt: number;
|
|
9
|
+
userId: number;
|
|
10
|
+
}) => Promise<import("zod").infer<import("zod").ZodObject<{
|
|
12
11
|
id: import("zod").ZodNumber;
|
|
13
12
|
name: import("zod").ZodString;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
email: import("zod").ZodString;
|
|
14
|
+
}, import("zod/v4/core").$strip>>>;
|
|
15
|
+
'user.create': (args: import("zod").infer<import("zod").ZodObject<{
|
|
16
|
+
name: import("zod").ZodString;
|
|
17
|
+
email: import("zod").ZodString;
|
|
18
|
+
}, import("zod/v4/core").$strip>>, context: {
|
|
19
|
+
requestedAt: number;
|
|
20
|
+
userId: number;
|
|
21
|
+
}) => Promise<import("zod").infer<import("zod").ZodObject<{
|
|
22
|
+
id: import("zod").ZodNumber;
|
|
23
|
+
name: import("zod").ZodString;
|
|
24
|
+
email: import("zod").ZodString;
|
|
25
|
+
createdAt: import("zod").ZodDate;
|
|
26
|
+
}, import("zod/v4/core").$strip>>>;
|
|
21
27
|
'user.update': (args: {
|
|
22
28
|
id: number;
|
|
23
29
|
name?: string;
|
|
24
30
|
email?: string;
|
|
31
|
+
}, context: {
|
|
32
|
+
requestedAt: number;
|
|
33
|
+
userId: number;
|
|
25
34
|
}) => Promise<user.User>;
|
|
35
|
+
'user.remove': (args: {
|
|
36
|
+
id: number;
|
|
37
|
+
}, context: {
|
|
38
|
+
requestedAt: number;
|
|
39
|
+
userId: number;
|
|
40
|
+
} & {
|
|
41
|
+
isAdmin: boolean;
|
|
42
|
+
}) => Promise<{
|
|
43
|
+
success: boolean;
|
|
44
|
+
}>;
|
|
26
45
|
'user.fetchAll': () => Promise<user.User[]>;
|
|
46
|
+
'product.fetch': (args: import("zod").infer<import("zod").ZodObject<{
|
|
47
|
+
id: import("zod").ZodNumber;
|
|
48
|
+
}, import("zod/v4/core").$strip>>, context: {
|
|
49
|
+
requestedAt: number;
|
|
50
|
+
userId: number;
|
|
51
|
+
}) => Promise<import("zod").infer<import("zod").ZodObject<{
|
|
52
|
+
id: import("zod").ZodNumber;
|
|
53
|
+
name: import("zod").ZodString;
|
|
54
|
+
price: import("zod").ZodNumber;
|
|
55
|
+
createdAt: import("zod").ZodDate;
|
|
56
|
+
}, import("zod/v4/core").$strip>>>;
|
|
57
|
+
'product.create': (args: import("zod").infer<import("zod").ZodObject<{
|
|
58
|
+
name: import("zod").ZodString;
|
|
59
|
+
price: import("zod").ZodNumber;
|
|
60
|
+
}, import("zod/v4/core").$strip>>, context: {
|
|
61
|
+
requestedAt: number;
|
|
62
|
+
userId: number;
|
|
63
|
+
}) => Promise<import("zod").infer<import("zod").ZodObject<{
|
|
64
|
+
id: import("zod").ZodNumber;
|
|
65
|
+
name: import("zod").ZodString;
|
|
66
|
+
price: import("zod").ZodNumber;
|
|
67
|
+
createdAt: import("zod").ZodDate;
|
|
68
|
+
}, import("zod/v4/core").$strip>>>;
|
|
69
|
+
'product.list': () => Promise<product.Product[]>;
|
|
70
|
+
'order.create': (args: import("zod").infer<import("zod").ZodObject<{
|
|
71
|
+
customerId: import("zod").ZodNumber;
|
|
72
|
+
items: import("zod").ZodArray<import("zod").ZodObject<{
|
|
73
|
+
productId: import("zod").ZodNumber;
|
|
74
|
+
quantity: import("zod").ZodNumber;
|
|
75
|
+
price: import("zod").ZodNumber;
|
|
76
|
+
notes: import("zod").ZodOptional<import("zod").ZodString>;
|
|
77
|
+
discount: import("zod").ZodNullable<import("zod").ZodNumber>;
|
|
78
|
+
}, import("zod/v4/core").$strip>>;
|
|
79
|
+
shippingAddress: import("zod").ZodObject<{
|
|
80
|
+
street: import("zod").ZodString;
|
|
81
|
+
city: import("zod").ZodString;
|
|
82
|
+
state: import("zod").ZodString;
|
|
83
|
+
zip: import("zod").ZodString;
|
|
84
|
+
country: import("zod").ZodDefault<import("zod").ZodString>;
|
|
85
|
+
}, import("zod/v4/core").$strip>;
|
|
86
|
+
billingAddress: import("zod").ZodNullable<import("zod").ZodObject<{
|
|
87
|
+
street: import("zod").ZodString;
|
|
88
|
+
city: import("zod").ZodString;
|
|
89
|
+
state: import("zod").ZodString;
|
|
90
|
+
zip: import("zod").ZodString;
|
|
91
|
+
country: import("zod").ZodDefault<import("zod").ZodString>;
|
|
92
|
+
}, import("zod/v4/core").$strip>>;
|
|
93
|
+
couponCode: import("zod").ZodOptional<import("zod").ZodNullable<import("zod").ZodString>>;
|
|
94
|
+
scheduledDate: import("zod").ZodOptional<import("zod").ZodDate>;
|
|
95
|
+
isGift: import("zod").ZodBoolean;
|
|
96
|
+
giftMessage: import("zod").ZodNullable<import("zod").ZodString>;
|
|
97
|
+
}, import("zod/v4/core").$strip>>, context: {
|
|
98
|
+
requestedAt: number;
|
|
99
|
+
userId: number;
|
|
100
|
+
}) => Promise<import("zod").infer<import("zod").ZodObject<{
|
|
101
|
+
id: import("zod").ZodNumber;
|
|
102
|
+
status: import("zod").ZodString;
|
|
103
|
+
total: import("zod").ZodNumber;
|
|
104
|
+
items: import("zod").ZodArray<import("zod").ZodObject<{
|
|
105
|
+
productId: import("zod").ZodNumber;
|
|
106
|
+
quantity: import("zod").ZodNumber;
|
|
107
|
+
price: import("zod").ZodNumber;
|
|
108
|
+
notes: import("zod").ZodOptional<import("zod").ZodString>;
|
|
109
|
+
discount: import("zod").ZodNullable<import("zod").ZodNumber>;
|
|
110
|
+
}, import("zod/v4/core").$strip>>;
|
|
111
|
+
createdAt: import("zod").ZodDate;
|
|
112
|
+
}, import("zod/v4/core").$strip>>>;
|
|
113
|
+
'order.fetch': (args: import("zod").infer<import("zod").ZodObject<{
|
|
114
|
+
id: import("zod").ZodNumber;
|
|
115
|
+
}, import("zod/v4/core").$strip>>, context: {
|
|
116
|
+
requestedAt: number;
|
|
117
|
+
userId: number;
|
|
118
|
+
}) => Promise<import("zod").infer<import("zod").ZodObject<{
|
|
119
|
+
id: import("zod").ZodNumber;
|
|
120
|
+
customerId: import("zod").ZodNumber;
|
|
121
|
+
status: import("zod").ZodString;
|
|
122
|
+
total: import("zod").ZodNumber;
|
|
123
|
+
items: import("zod").ZodArray<import("zod").ZodObject<{
|
|
124
|
+
productId: import("zod").ZodNumber;
|
|
125
|
+
quantity: import("zod").ZodNumber;
|
|
126
|
+
price: import("zod").ZodNumber;
|
|
127
|
+
notes: import("zod").ZodOptional<import("zod").ZodString>;
|
|
128
|
+
discount: import("zod").ZodNullable<import("zod").ZodNumber>;
|
|
129
|
+
}, import("zod/v4/core").$strip>>;
|
|
130
|
+
shippingAddress: import("zod").ZodObject<{
|
|
131
|
+
street: import("zod").ZodString;
|
|
132
|
+
city: import("zod").ZodString;
|
|
133
|
+
state: import("zod").ZodString;
|
|
134
|
+
zip: import("zod").ZodString;
|
|
135
|
+
country: import("zod").ZodDefault<import("zod").ZodString>;
|
|
136
|
+
}, import("zod/v4/core").$strip>;
|
|
137
|
+
billingAddress: import("zod").ZodNullable<import("zod").ZodObject<{
|
|
138
|
+
street: import("zod").ZodString;
|
|
139
|
+
city: import("zod").ZodString;
|
|
140
|
+
state: import("zod").ZodString;
|
|
141
|
+
zip: import("zod").ZodString;
|
|
142
|
+
country: import("zod").ZodDefault<import("zod").ZodString>;
|
|
143
|
+
}, import("zod/v4/core").$strip>>;
|
|
144
|
+
isGift: import("zod").ZodBoolean;
|
|
145
|
+
giftMessage: import("zod").ZodNullable<import("zod").ZodString>;
|
|
146
|
+
createdAt: import("zod").ZodDate;
|
|
147
|
+
updatedAt: import("zod").ZodNullable<import("zod").ZodDate>;
|
|
148
|
+
}, import("zod/v4/core").$strip>>>;
|
|
149
|
+
'order.update': (args: import("zod").infer<import("zod").ZodObject<{
|
|
150
|
+
id: import("zod").ZodNumber;
|
|
151
|
+
status: import("zod").ZodOptional<import("zod").ZodString>;
|
|
152
|
+
shippingAddress: import("zod").ZodOptional<import("zod").ZodObject<{
|
|
153
|
+
street: import("zod").ZodString;
|
|
154
|
+
city: import("zod").ZodString;
|
|
155
|
+
state: import("zod").ZodString;
|
|
156
|
+
zip: import("zod").ZodString;
|
|
157
|
+
country: import("zod").ZodDefault<import("zod").ZodString>;
|
|
158
|
+
}, import("zod/v4/core").$strip>>;
|
|
159
|
+
giftMessage: import("zod").ZodOptional<import("zod").ZodNullable<import("zod").ZodString>>;
|
|
160
|
+
}, import("zod/v4/core").$strip>>, context: {
|
|
161
|
+
requestedAt: number;
|
|
162
|
+
userId: number;
|
|
163
|
+
}) => Promise<import("zod").infer<import("zod").ZodObject<{
|
|
164
|
+
id: import("zod").ZodNumber;
|
|
165
|
+
status: import("zod").ZodString;
|
|
166
|
+
updatedAt: import("zod").ZodDate;
|
|
167
|
+
}, import("zod/v4/core").$strip>>>;
|
|
168
|
+
'order.list': () => Promise<order.Order[]>;
|
|
169
|
+
'order.primitives': (args: import("zod").infer<import("zod").ZodObject<{
|
|
170
|
+
key: import("zod").ZodString;
|
|
171
|
+
}, import("zod/v4/core").$strip>>) => Promise<import("zod").infer<import("zod").ZodObject<{
|
|
172
|
+
str: import("zod").ZodString;
|
|
173
|
+
num: import("zod").ZodNumber;
|
|
174
|
+
bool: import("zod").ZodBoolean;
|
|
175
|
+
date: import("zod").ZodDate;
|
|
176
|
+
nul: import("zod").ZodNull;
|
|
177
|
+
undef: import("zod").ZodUndefined;
|
|
178
|
+
unk: import("zod").ZodUnknown;
|
|
179
|
+
whatever: import("zod").ZodAny;
|
|
180
|
+
optStr: import("zod").ZodOptional<import("zod").ZodString>;
|
|
181
|
+
nullStr: import("zod").ZodNullable<import("zod").ZodString>;
|
|
182
|
+
defStr: import("zod").ZodDefault<import("zod").ZodString>;
|
|
183
|
+
optNullStr: import("zod").ZodOptional<import("zod").ZodNullable<import("zod").ZodString>>;
|
|
184
|
+
nullOptStr: import("zod").ZodNullable<import("zod").ZodOptional<import("zod").ZodString>>;
|
|
185
|
+
tags: import("zod").ZodArray<import("zod").ZodString>;
|
|
186
|
+
scores: import("zod").ZodNullable<import("zod").ZodArray<import("zod").ZodNumber>>;
|
|
187
|
+
optDates: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodDate>>;
|
|
188
|
+
nested: import("zod").ZodObject<{
|
|
189
|
+
a: import("zod").ZodNumber;
|
|
190
|
+
b: import("zod").ZodOptional<import("zod").ZodString>;
|
|
191
|
+
c: import("zod").ZodObject<{
|
|
192
|
+
d: import("zod").ZodBoolean;
|
|
193
|
+
e: import("zod").ZodNullable<import("zod").ZodArray<import("zod").ZodString>>;
|
|
194
|
+
}, import("zod/v4/core").$strip>;
|
|
195
|
+
}, import("zod/v4/core").$strip>;
|
|
196
|
+
}, import("zod/v4/core").$strip>>>;
|
|
27
197
|
};
|
|
28
198
|
export default _default;
|
|
@@ -34,8 +34,20 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
const user = __importStar(require("./user"));
|
|
37
|
+
const product = __importStar(require("./product"));
|
|
38
|
+
const order = __importStar(require("./order"));
|
|
37
39
|
exports.default = {
|
|
38
40
|
'user.fetch': user.fetch,
|
|
41
|
+
'user.create': user.create,
|
|
39
42
|
'user.update': user.update,
|
|
40
|
-
'user.
|
|
43
|
+
'user.remove': user.remove,
|
|
44
|
+
'user.fetchAll': user.fetchAll,
|
|
45
|
+
'product.fetch': product.fetch,
|
|
46
|
+
'product.create': product.create,
|
|
47
|
+
'product.list': product.list,
|
|
48
|
+
'order.create': order.create,
|
|
49
|
+
'order.fetch': order.fetch,
|
|
50
|
+
'order.update': order.update,
|
|
51
|
+
'order.list': order.list,
|
|
52
|
+
'order.primitives': order.primitives
|
|
41
53
|
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { $z } from '../../..';
|
|
2
|
+
import * as types from './types';
|
|
3
|
+
export interface OrderItem {
|
|
4
|
+
productId: number;
|
|
5
|
+
quantity: number;
|
|
6
|
+
price: number;
|
|
7
|
+
notes?: string;
|
|
8
|
+
discount: number | null;
|
|
9
|
+
}
|
|
10
|
+
export interface Address {
|
|
11
|
+
street: string;
|
|
12
|
+
city: string;
|
|
13
|
+
state: string;
|
|
14
|
+
zip: string;
|
|
15
|
+
country: string;
|
|
16
|
+
}
|
|
17
|
+
export interface Order {
|
|
18
|
+
id: number;
|
|
19
|
+
customerId: number;
|
|
20
|
+
status: string;
|
|
21
|
+
total: number;
|
|
22
|
+
items: OrderItem[];
|
|
23
|
+
shippingAddress: Address;
|
|
24
|
+
billingAddress: Address | null;
|
|
25
|
+
isGift: boolean;
|
|
26
|
+
giftMessage: string | null;
|
|
27
|
+
createdAt: Date;
|
|
28
|
+
updatedAt: Date | null;
|
|
29
|
+
}
|
|
30
|
+
type Context = {
|
|
31
|
+
requestedAt: number;
|
|
32
|
+
userId: number;
|
|
33
|
+
};
|
|
34
|
+
export declare const create: (args: $z.infer<typeof types.create.args>, context: Context) => Promise<$z.infer<typeof types.create.res>>;
|
|
35
|
+
export declare const fetch: (args: $z.infer<typeof types.fetch.args>, context: Context) => Promise<$z.infer<typeof types.fetch.res>>;
|
|
36
|
+
export declare const update: (args: $z.infer<typeof types.update.args>, context: Context) => Promise<$z.infer<typeof types.update.res>>;
|
|
37
|
+
export declare const list: () => Promise<Order[]>;
|
|
38
|
+
export declare const primitives: (args: $z.infer<typeof types.primitives.args>) => Promise<$z.infer<typeof types.primitives.res>>;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.primitives = exports.list = exports.update = exports.fetch = exports.create = void 0;
|
|
37
|
+
const types = __importStar(require("./types"));
|
|
38
|
+
let nextId = 1;
|
|
39
|
+
const orders = [];
|
|
40
|
+
const create = async (args, context) => {
|
|
41
|
+
args = types.create.args.parse(args);
|
|
42
|
+
const items = args.items.map(item => ({
|
|
43
|
+
...item,
|
|
44
|
+
price: item.price,
|
|
45
|
+
discount: item.discount
|
|
46
|
+
}));
|
|
47
|
+
const order = {
|
|
48
|
+
id: nextId++,
|
|
49
|
+
customerId: args.customerId,
|
|
50
|
+
status: 'pending',
|
|
51
|
+
total: items.reduce((sum, i) => sum + i.price * i.quantity, 0),
|
|
52
|
+
items,
|
|
53
|
+
shippingAddress: { ...args.shippingAddress, country: args.shippingAddress.country ?? 'US' },
|
|
54
|
+
billingAddress: args.billingAddress ? { ...args.billingAddress, country: args.billingAddress.country ?? 'US' } : null,
|
|
55
|
+
isGift: args.isGift,
|
|
56
|
+
giftMessage: args.giftMessage,
|
|
57
|
+
createdAt: new Date(),
|
|
58
|
+
updatedAt: null
|
|
59
|
+
};
|
|
60
|
+
orders.push(order);
|
|
61
|
+
return {
|
|
62
|
+
id: order.id,
|
|
63
|
+
status: order.status,
|
|
64
|
+
total: order.total,
|
|
65
|
+
items: order.items,
|
|
66
|
+
createdAt: order.createdAt
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
exports.create = create;
|
|
70
|
+
const fetch = async (args, context) => {
|
|
71
|
+
args = types.fetch.args.parse(args);
|
|
72
|
+
const order = orders.find(o => o.id === args.id);
|
|
73
|
+
if (!order)
|
|
74
|
+
throw new Error(`Order with ID ${args.id} not found`);
|
|
75
|
+
return order;
|
|
76
|
+
};
|
|
77
|
+
exports.fetch = fetch;
|
|
78
|
+
const update = async (args, context) => {
|
|
79
|
+
args = types.update.args.parse(args);
|
|
80
|
+
const order = orders.find(o => o.id === args.id);
|
|
81
|
+
if (!order)
|
|
82
|
+
throw new Error(`Order with ID ${args.id} not found`);
|
|
83
|
+
if (args.status)
|
|
84
|
+
order.status = args.status;
|
|
85
|
+
if (args.shippingAddress)
|
|
86
|
+
order.shippingAddress = { ...args.shippingAddress, country: args.shippingAddress.country ?? 'US' };
|
|
87
|
+
if (args.giftMessage !== undefined)
|
|
88
|
+
order.giftMessage = args.giftMessage ?? null;
|
|
89
|
+
order.updatedAt = new Date();
|
|
90
|
+
return { id: order.id, status: order.status, updatedAt: order.updatedAt };
|
|
91
|
+
};
|
|
92
|
+
exports.update = update;
|
|
93
|
+
const list = async () => {
|
|
94
|
+
return orders;
|
|
95
|
+
};
|
|
96
|
+
exports.list = list;
|
|
97
|
+
const primitives = async (args) => {
|
|
98
|
+
args = types.primitives.args.parse(args);
|
|
99
|
+
return {
|
|
100
|
+
str: 'hello',
|
|
101
|
+
num: 42,
|
|
102
|
+
bool: true,
|
|
103
|
+
date: new Date(),
|
|
104
|
+
nul: null,
|
|
105
|
+
undef: undefined,
|
|
106
|
+
unk: { anything: true },
|
|
107
|
+
whatever: 'literally anything',
|
|
108
|
+
optStr: 'present',
|
|
109
|
+
nullStr: null,
|
|
110
|
+
defStr: 'default value',
|
|
111
|
+
optNullStr: null,
|
|
112
|
+
nullOptStr: undefined,
|
|
113
|
+
tags: ['a', 'b', 'c'],
|
|
114
|
+
scores: [1, 2, 3],
|
|
115
|
+
optDates: [new Date()],
|
|
116
|
+
nested: {
|
|
117
|
+
a: 1,
|
|
118
|
+
b: 'nested',
|
|
119
|
+
c: {
|
|
120
|
+
d: true,
|
|
121
|
+
e: ['deep']
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
exports.primitives = primitives;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { $z } from '../../..';
|
|
2
|
+
export declare const create: {
|
|
3
|
+
args: $z.ZodObject<{
|
|
4
|
+
customerId: $z.ZodNumber;
|
|
5
|
+
items: $z.ZodArray<$z.ZodObject<{
|
|
6
|
+
productId: $z.ZodNumber;
|
|
7
|
+
quantity: $z.ZodNumber;
|
|
8
|
+
price: $z.ZodNumber;
|
|
9
|
+
notes: $z.ZodOptional<$z.ZodString>;
|
|
10
|
+
discount: $z.ZodNullable<$z.ZodNumber>;
|
|
11
|
+
}, $z.core.$strip>>;
|
|
12
|
+
shippingAddress: $z.ZodObject<{
|
|
13
|
+
street: $z.ZodString;
|
|
14
|
+
city: $z.ZodString;
|
|
15
|
+
state: $z.ZodString;
|
|
16
|
+
zip: $z.ZodString;
|
|
17
|
+
country: $z.ZodDefault<$z.ZodString>;
|
|
18
|
+
}, $z.core.$strip>;
|
|
19
|
+
billingAddress: $z.ZodNullable<$z.ZodObject<{
|
|
20
|
+
street: $z.ZodString;
|
|
21
|
+
city: $z.ZodString;
|
|
22
|
+
state: $z.ZodString;
|
|
23
|
+
zip: $z.ZodString;
|
|
24
|
+
country: $z.ZodDefault<$z.ZodString>;
|
|
25
|
+
}, $z.core.$strip>>;
|
|
26
|
+
couponCode: $z.ZodOptional<$z.ZodNullable<$z.ZodString>>;
|
|
27
|
+
scheduledDate: $z.ZodOptional<$z.ZodDate>;
|
|
28
|
+
isGift: $z.ZodBoolean;
|
|
29
|
+
giftMessage: $z.ZodNullable<$z.ZodString>;
|
|
30
|
+
}, $z.core.$strip>;
|
|
31
|
+
res: $z.ZodObject<{
|
|
32
|
+
id: $z.ZodNumber;
|
|
33
|
+
status: $z.ZodString;
|
|
34
|
+
total: $z.ZodNumber;
|
|
35
|
+
items: $z.ZodArray<$z.ZodObject<{
|
|
36
|
+
productId: $z.ZodNumber;
|
|
37
|
+
quantity: $z.ZodNumber;
|
|
38
|
+
price: $z.ZodNumber;
|
|
39
|
+
notes: $z.ZodOptional<$z.ZodString>;
|
|
40
|
+
discount: $z.ZodNullable<$z.ZodNumber>;
|
|
41
|
+
}, $z.core.$strip>>;
|
|
42
|
+
createdAt: $z.ZodDate;
|
|
43
|
+
}, $z.core.$strip>;
|
|
44
|
+
};
|
|
45
|
+
export declare const fetch: {
|
|
46
|
+
args: $z.ZodObject<{
|
|
47
|
+
id: $z.ZodNumber;
|
|
48
|
+
}, $z.core.$strip>;
|
|
49
|
+
res: $z.ZodObject<{
|
|
50
|
+
id: $z.ZodNumber;
|
|
51
|
+
customerId: $z.ZodNumber;
|
|
52
|
+
status: $z.ZodString;
|
|
53
|
+
total: $z.ZodNumber;
|
|
54
|
+
items: $z.ZodArray<$z.ZodObject<{
|
|
55
|
+
productId: $z.ZodNumber;
|
|
56
|
+
quantity: $z.ZodNumber;
|
|
57
|
+
price: $z.ZodNumber;
|
|
58
|
+
notes: $z.ZodOptional<$z.ZodString>;
|
|
59
|
+
discount: $z.ZodNullable<$z.ZodNumber>;
|
|
60
|
+
}, $z.core.$strip>>;
|
|
61
|
+
shippingAddress: $z.ZodObject<{
|
|
62
|
+
street: $z.ZodString;
|
|
63
|
+
city: $z.ZodString;
|
|
64
|
+
state: $z.ZodString;
|
|
65
|
+
zip: $z.ZodString;
|
|
66
|
+
country: $z.ZodDefault<$z.ZodString>;
|
|
67
|
+
}, $z.core.$strip>;
|
|
68
|
+
billingAddress: $z.ZodNullable<$z.ZodObject<{
|
|
69
|
+
street: $z.ZodString;
|
|
70
|
+
city: $z.ZodString;
|
|
71
|
+
state: $z.ZodString;
|
|
72
|
+
zip: $z.ZodString;
|
|
73
|
+
country: $z.ZodDefault<$z.ZodString>;
|
|
74
|
+
}, $z.core.$strip>>;
|
|
75
|
+
isGift: $z.ZodBoolean;
|
|
76
|
+
giftMessage: $z.ZodNullable<$z.ZodString>;
|
|
77
|
+
createdAt: $z.ZodDate;
|
|
78
|
+
updatedAt: $z.ZodNullable<$z.ZodDate>;
|
|
79
|
+
}, $z.core.$strip>;
|
|
80
|
+
};
|
|
81
|
+
export declare const update: {
|
|
82
|
+
args: $z.ZodObject<{
|
|
83
|
+
id: $z.ZodNumber;
|
|
84
|
+
status: $z.ZodOptional<$z.ZodString>;
|
|
85
|
+
shippingAddress: $z.ZodOptional<$z.ZodObject<{
|
|
86
|
+
street: $z.ZodString;
|
|
87
|
+
city: $z.ZodString;
|
|
88
|
+
state: $z.ZodString;
|
|
89
|
+
zip: $z.ZodString;
|
|
90
|
+
country: $z.ZodDefault<$z.ZodString>;
|
|
91
|
+
}, $z.core.$strip>>;
|
|
92
|
+
giftMessage: $z.ZodOptional<$z.ZodNullable<$z.ZodString>>;
|
|
93
|
+
}, $z.core.$strip>;
|
|
94
|
+
res: $z.ZodObject<{
|
|
95
|
+
id: $z.ZodNumber;
|
|
96
|
+
status: $z.ZodString;
|
|
97
|
+
updatedAt: $z.ZodDate;
|
|
98
|
+
}, $z.core.$strip>;
|
|
99
|
+
};
|
|
100
|
+
export declare const primitives: {
|
|
101
|
+
args: $z.ZodObject<{
|
|
102
|
+
key: $z.ZodString;
|
|
103
|
+
}, $z.core.$strip>;
|
|
104
|
+
res: $z.ZodObject<{
|
|
105
|
+
str: $z.ZodString;
|
|
106
|
+
num: $z.ZodNumber;
|
|
107
|
+
bool: $z.ZodBoolean;
|
|
108
|
+
date: $z.ZodDate;
|
|
109
|
+
nul: $z.ZodNull;
|
|
110
|
+
undef: $z.ZodUndefined;
|
|
111
|
+
unk: $z.ZodUnknown;
|
|
112
|
+
whatever: $z.ZodAny;
|
|
113
|
+
optStr: $z.ZodOptional<$z.ZodString>;
|
|
114
|
+
nullStr: $z.ZodNullable<$z.ZodString>;
|
|
115
|
+
defStr: $z.ZodDefault<$z.ZodString>;
|
|
116
|
+
optNullStr: $z.ZodOptional<$z.ZodNullable<$z.ZodString>>;
|
|
117
|
+
nullOptStr: $z.ZodNullable<$z.ZodOptional<$z.ZodString>>;
|
|
118
|
+
tags: $z.ZodArray<$z.ZodString>;
|
|
119
|
+
scores: $z.ZodNullable<$z.ZodArray<$z.ZodNumber>>;
|
|
120
|
+
optDates: $z.ZodOptional<$z.ZodArray<$z.ZodDate>>;
|
|
121
|
+
nested: $z.ZodObject<{
|
|
122
|
+
a: $z.ZodNumber;
|
|
123
|
+
b: $z.ZodOptional<$z.ZodString>;
|
|
124
|
+
c: $z.ZodObject<{
|
|
125
|
+
d: $z.ZodBoolean;
|
|
126
|
+
e: $z.ZodNullable<$z.ZodArray<$z.ZodString>>;
|
|
127
|
+
}, $z.core.$strip>;
|
|
128
|
+
}, $z.core.$strip>;
|
|
129
|
+
}, $z.core.$strip>;
|
|
130
|
+
};
|