@openmrp/sdk-mcp 0.19.3

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.
Files changed (135) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +103 -0
  3. package/auth.d.mts +6 -0
  4. package/auth.d.mts.map +1 -0
  5. package/auth.d.ts +6 -0
  6. package/auth.d.ts.map +1 -0
  7. package/auth.js +37 -0
  8. package/auth.js.map +1 -0
  9. package/auth.mjs +32 -0
  10. package/auth.mjs.map +1 -0
  11. package/code-tool-paths.cjs +8 -0
  12. package/code-tool-paths.cjs.map +1 -0
  13. package/code-tool-paths.d.cts +2 -0
  14. package/code-tool-paths.d.cts.map +1 -0
  15. package/code-tool-types.d.mts +14 -0
  16. package/code-tool-types.d.mts.map +1 -0
  17. package/code-tool-types.d.ts +14 -0
  18. package/code-tool-types.d.ts.map +1 -0
  19. package/code-tool-types.js +4 -0
  20. package/code-tool-types.js.map +1 -0
  21. package/code-tool-types.mjs +3 -0
  22. package/code-tool-types.mjs.map +1 -0
  23. package/code-tool-worker.d.mts +5 -0
  24. package/code-tool-worker.d.mts.map +1 -0
  25. package/code-tool-worker.d.ts +5 -0
  26. package/code-tool-worker.d.ts.map +1 -0
  27. package/code-tool-worker.js +636 -0
  28. package/code-tool-worker.js.map +1 -0
  29. package/code-tool-worker.mjs +598 -0
  30. package/code-tool-worker.mjs.map +1 -0
  31. package/code-tool.d.mts +21 -0
  32. package/code-tool.d.mts.map +1 -0
  33. package/code-tool.d.ts +21 -0
  34. package/code-tool.d.ts.map +1 -0
  35. package/code-tool.js +287 -0
  36. package/code-tool.js.map +1 -0
  37. package/code-tool.mjs +251 -0
  38. package/code-tool.mjs.map +1 -0
  39. package/http.d.mts +12 -0
  40. package/http.d.mts.map +1 -0
  41. package/http.d.ts +12 -0
  42. package/http.d.ts.map +1 -0
  43. package/http.js +190 -0
  44. package/http.js.map +1 -0
  45. package/http.mjs +182 -0
  46. package/http.mjs.map +1 -0
  47. package/index.d.mts +3 -0
  48. package/index.d.mts.map +1 -0
  49. package/index.d.ts +3 -0
  50. package/index.d.ts.map +1 -0
  51. package/index.js +60 -0
  52. package/index.js.map +1 -0
  53. package/index.mjs +58 -0
  54. package/index.mjs.map +1 -0
  55. package/instructions.d.mts +5 -0
  56. package/instructions.d.mts.map +1 -0
  57. package/instructions.d.ts +5 -0
  58. package/instructions.d.ts.map +1 -0
  59. package/instructions.js +47 -0
  60. package/instructions.js.map +1 -0
  61. package/instructions.mjs +41 -0
  62. package/instructions.mjs.map +1 -0
  63. package/logger.d.mts +7 -0
  64. package/logger.d.mts.map +1 -0
  65. package/logger.d.ts +7 -0
  66. package/logger.d.ts.map +1 -0
  67. package/logger.js +29 -0
  68. package/logger.js.map +1 -0
  69. package/logger.mjs +22 -0
  70. package/logger.mjs.map +1 -0
  71. package/methods.d.mts +10 -0
  72. package/methods.d.mts.map +1 -0
  73. package/methods.d.ts +10 -0
  74. package/methods.d.ts.map +1 -0
  75. package/methods.js +2285 -0
  76. package/methods.js.map +1 -0
  77. package/methods.mjs +2281 -0
  78. package/methods.mjs.map +1 -0
  79. package/options.d.mts +20 -0
  80. package/options.d.mts.map +1 -0
  81. package/options.d.ts +20 -0
  82. package/options.d.ts.map +1 -0
  83. package/options.js +121 -0
  84. package/options.js.map +1 -0
  85. package/options.mjs +114 -0
  86. package/options.mjs.map +1 -0
  87. package/package.json +216 -0
  88. package/server.d.mts +38 -0
  89. package/server.d.mts.map +1 -0
  90. package/server.d.ts +38 -0
  91. package/server.d.ts.map +1 -0
  92. package/server.js +162 -0
  93. package/server.js.map +1 -0
  94. package/server.mjs +152 -0
  95. package/server.mjs.map +1 -0
  96. package/src/auth.ts +42 -0
  97. package/src/code-tool-paths.cts +5 -0
  98. package/src/code-tool-types.ts +17 -0
  99. package/src/code-tool-worker.ts +649 -0
  100. package/src/code-tool.ts +316 -0
  101. package/src/http.ts +227 -0
  102. package/src/index.ts +67 -0
  103. package/src/instructions.ts +59 -0
  104. package/src/logger.ts +28 -0
  105. package/src/methods.ts +2306 -0
  106. package/src/options.ts +159 -0
  107. package/src/server.ts +202 -0
  108. package/src/stdio.ts +17 -0
  109. package/src/tsconfig.json +11 -0
  110. package/src/types.ts +126 -0
  111. package/src/util.ts +25 -0
  112. package/stdio.d.mts +3 -0
  113. package/stdio.d.mts.map +1 -0
  114. package/stdio.d.ts +3 -0
  115. package/stdio.d.ts.map +1 -0
  116. package/stdio.js +18 -0
  117. package/stdio.js.map +1 -0
  118. package/stdio.mjs +14 -0
  119. package/stdio.mjs.map +1 -0
  120. package/types.d.mts +65 -0
  121. package/types.d.mts.map +1 -0
  122. package/types.d.ts +65 -0
  123. package/types.d.ts.map +1 -0
  124. package/types.js +58 -0
  125. package/types.js.map +1 -0
  126. package/types.mjs +53 -0
  127. package/types.mjs.map +1 -0
  128. package/util.d.mts +4 -0
  129. package/util.d.mts.map +1 -0
  130. package/util.d.ts +4 -0
  131. package/util.d.ts.map +1 -0
  132. package/util.js +30 -0
  133. package/util.js.map +1 -0
  134. package/util.mjs +24 -0
  135. package/util.mjs.map +1 -0
@@ -0,0 +1,649 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import path from 'node:path';
4
+ import util from 'node:util';
5
+ import Fuse from 'fuse.js';
6
+ import ts from 'typescript';
7
+ import { WorkerOutput } from './code-tool-types';
8
+ import { OpenMRP, ClientOptions } from '@openmrp/sdk';
9
+
10
+ async function tseval(code: string) {
11
+ return import('data:application/typescript;charset=utf-8;base64,' + Buffer.from(code).toString('base64'));
12
+ }
13
+
14
+ function getRunFunctionSource(code: string): {
15
+ type: 'declaration' | 'expression';
16
+ client: string | undefined;
17
+ code: string;
18
+ } | null {
19
+ const sourceFile = ts.createSourceFile('code.ts', code, ts.ScriptTarget.Latest, true);
20
+ const printer = ts.createPrinter();
21
+
22
+ for (const statement of sourceFile.statements) {
23
+ // Check for top-level function declarations
24
+ if (ts.isFunctionDeclaration(statement)) {
25
+ if (statement.name?.text === 'run') {
26
+ return {
27
+ type: 'declaration',
28
+ client: statement.parameters[0]?.name.getText(),
29
+ code: printer.printNode(ts.EmitHint.Unspecified, statement.body!, sourceFile),
30
+ };
31
+ }
32
+ }
33
+
34
+ // Check for variable declarations: const run = () => {} or const run = function() {}
35
+ if (ts.isVariableStatement(statement)) {
36
+ for (const declaration of statement.declarationList.declarations) {
37
+ if (
38
+ ts.isIdentifier(declaration.name) &&
39
+ declaration.name.text === 'run' &&
40
+ // Check if it's initialized with a function
41
+ declaration.initializer &&
42
+ (ts.isFunctionExpression(declaration.initializer) || ts.isArrowFunction(declaration.initializer))
43
+ ) {
44
+ return {
45
+ type: 'expression',
46
+ client: declaration.initializer.parameters[0]?.name.getText(),
47
+ code: printer.printNode(ts.EmitHint.Unspecified, declaration.initializer, sourceFile),
48
+ };
49
+ }
50
+ }
51
+ }
52
+ }
53
+
54
+ return null;
55
+ }
56
+
57
+ function getTSDiagnostics(code: string): string[] {
58
+ const functionSource = getRunFunctionSource(code)!;
59
+ const codeWithImport = [
60
+ 'import { OpenMRP } from "@openmrp/sdk";',
61
+ functionSource.type === 'declaration' ?
62
+ `async function run(${functionSource.client}: OpenMRP)`
63
+ : `const run: (${functionSource.client}: OpenMRP) => Promise<unknown> =`,
64
+ functionSource.code,
65
+ ].join('\n');
66
+ const sourcePath = path.resolve('code.ts');
67
+ const ast = ts.createSourceFile(sourcePath, codeWithImport, ts.ScriptTarget.Latest, true);
68
+ const options = ts.getDefaultCompilerOptions();
69
+ options.target = ts.ScriptTarget.Latest;
70
+ options.module = ts.ModuleKind.NodeNext;
71
+ options.moduleResolution = ts.ModuleResolutionKind.NodeNext;
72
+ const host = ts.createCompilerHost(options, true);
73
+ const newHost: typeof host = {
74
+ ...host,
75
+ getSourceFile: (...args) => {
76
+ if (path.resolve(args[0]) === sourcePath) {
77
+ return ast;
78
+ }
79
+ return host.getSourceFile(...args);
80
+ },
81
+ readFile: (...args) => {
82
+ if (path.resolve(args[0]) === sourcePath) {
83
+ return codeWithImport;
84
+ }
85
+ return host.readFile(...args);
86
+ },
87
+ fileExists: (...args) => {
88
+ if (path.resolve(args[0]) === sourcePath) {
89
+ return true;
90
+ }
91
+ return host.fileExists(...args);
92
+ },
93
+ };
94
+ const program = ts.createProgram({
95
+ options,
96
+ rootNames: [sourcePath],
97
+ host: newHost,
98
+ });
99
+ const diagnostics = ts.getPreEmitDiagnostics(program, ast);
100
+ return diagnostics.map((d) => {
101
+ const message = ts.flattenDiagnosticMessageText(d.messageText, '\n');
102
+ if (!d.file || !d.start) return `- ${message}`;
103
+ const { line: lineNumber } = ts.getLineAndCharacterOfPosition(d.file, d.start);
104
+ const line = codeWithImport.split('\n').at(lineNumber)?.trim();
105
+ return line ? `- ${message}\n ${line}` : `- ${message}`;
106
+ });
107
+ }
108
+
109
+ const fuse = new Fuse(
110
+ [
111
+ 'client.auth.apiKeys.create',
112
+ 'client.auth.apiKeys.delete',
113
+ 'client.auth.apiKeys.list',
114
+ 'client.auth.apiKeys.retrieve',
115
+ 'client.auth.apiKeys.actions.rotate',
116
+ 'client.core.retrieveSearch',
117
+ 'client.core.sandboxes.create',
118
+ 'client.core.sandboxes.delete',
119
+ 'client.core.sandboxes.list',
120
+ 'client.core.sandboxes.retrieve',
121
+ 'client.core.requestLogs.list',
122
+ 'client.core.requestLogs.retrieve',
123
+ 'client.core.auditEvents.list',
124
+ 'client.core.auditEvents.retrieve',
125
+ 'client.core.auditEvents.retrieveResourceTypes',
126
+ 'client.core.addresses.retrieveSuggestions',
127
+ 'client.core.addresses.actions.validate',
128
+ 'client.core.emailLogs.list',
129
+ 'client.core.emailLogs.retrieve',
130
+ 'client.core.jobs.cancel',
131
+ 'client.core.jobs.retrieve',
132
+ 'client.core.analytics.updateDeliveryPerformance',
133
+ 'client.core.analytics.updateOee',
134
+ 'client.core.analytics.updateOeeTrend',
135
+ 'client.core.analytics.updateScheduleAttainment',
136
+ 'client.core.actions.emailRecord',
137
+ 'client.catalog.units.create',
138
+ 'client.catalog.units.delete',
139
+ 'client.catalog.units.list',
140
+ 'client.catalog.units.retrieve',
141
+ 'client.catalog.units.update',
142
+ 'client.catalog.units.actions.bulkUpsert',
143
+ 'client.catalog.unitGroups.create',
144
+ 'client.catalog.unitGroups.delete',
145
+ 'client.catalog.unitGroups.list',
146
+ 'client.catalog.unitGroups.retrieve',
147
+ 'client.catalog.unitGroups.update',
148
+ 'client.catalog.unitGroups.units.create',
149
+ 'client.catalog.unitGroups.units.delete',
150
+ 'client.catalog.unitGroups.units.list',
151
+ 'client.catalog.unitGroups.units.retrieve',
152
+ 'client.catalog.unitGroups.units.update',
153
+ 'client.catalog.unitGroups.actions.bulkUpsert',
154
+ 'client.catalog.properties.create',
155
+ 'client.catalog.properties.delete',
156
+ 'client.catalog.properties.list',
157
+ 'client.catalog.properties.retrieve',
158
+ 'client.catalog.properties.update',
159
+ 'client.catalog.properties.attributes.create',
160
+ 'client.catalog.properties.attributes.delete',
161
+ 'client.catalog.properties.attributes.list',
162
+ 'client.catalog.properties.attributes.retrieve',
163
+ 'client.catalog.properties.attributes.update',
164
+ 'client.catalog.properties.actions.bulkUpsert',
165
+ 'client.catalog.items.changeCategory',
166
+ 'client.catalog.items.list',
167
+ 'client.catalog.items.retrieve',
168
+ 'client.catalog.items.retrieveLotDefault',
169
+ 'client.catalog.items.inventory.list',
170
+ 'client.catalog.items.inventory.update',
171
+ 'client.catalog.items.attributes.delete',
172
+ 'client.catalog.items.attributes.update',
173
+ 'client.catalog.items.actions.bulkReconcile',
174
+ 'client.catalog.itemCategories.changeUnitGroup',
175
+ 'client.catalog.itemCategories.create',
176
+ 'client.catalog.itemCategories.delete',
177
+ 'client.catalog.itemCategories.list',
178
+ 'client.catalog.itemCategories.retrieve',
179
+ 'client.catalog.itemCategories.update',
180
+ 'client.catalog.itemCategories.properties.delete',
181
+ 'client.catalog.itemCategories.properties.update',
182
+ 'client.catalog.itemCategories.actions.bulkUpsert',
183
+ 'client.catalog.materials.create',
184
+ 'client.catalog.materials.delete',
185
+ 'client.catalog.materials.list',
186
+ 'client.catalog.materials.retrieve',
187
+ 'client.catalog.materials.update',
188
+ 'client.catalog.materials.actions.bulkUpsert',
189
+ 'client.catalog.parts.create',
190
+ 'client.catalog.parts.delete',
191
+ 'client.catalog.parts.list',
192
+ 'client.catalog.parts.retrieve',
193
+ 'client.catalog.parts.update',
194
+ 'client.catalog.parts.actions.bulkUpsert',
195
+ 'client.catalog.productLines.create',
196
+ 'client.catalog.productLines.delete',
197
+ 'client.catalog.productLines.list',
198
+ 'client.catalog.productLines.retrieve',
199
+ 'client.catalog.productLines.update',
200
+ 'client.catalog.productLines.actions.bulkUpsert',
201
+ 'client.catalog.products.changeProductLine',
202
+ 'client.catalog.products.create',
203
+ 'client.catalog.products.delete',
204
+ 'client.catalog.products.list',
205
+ 'client.catalog.products.retrieve',
206
+ 'client.catalog.products.update',
207
+ 'client.catalog.products.actions.bulkUpsert',
208
+ 'client.ai.retrieveToolGroups',
209
+ 'client.ai.retrieveTools',
210
+ 'client.ai.agents.create',
211
+ 'client.ai.agents.delete',
212
+ 'client.ai.agents.list',
213
+ 'client.ai.agents.retrieve',
214
+ 'client.ai.agents.update',
215
+ 'client.ai.agents.updateStatus',
216
+ 'client.ai.runs.create',
217
+ 'client.ai.runs.list',
218
+ 'client.ai.runs.retrieve',
219
+ 'client.ai.runs.actions.cancel',
220
+ 'client.ai.runs.actions.continue',
221
+ 'client.ai.runs.actions.retry',
222
+ 'client.ai.memories.create',
223
+ 'client.ai.memories.delete',
224
+ 'client.ai.memories.list',
225
+ 'client.ai.memories.retrieve',
226
+ 'client.ai.memories.update',
227
+ 'client.messaging.retrieveContacts',
228
+ 'client.messaging.notifications.create',
229
+ 'client.messaging.notifications.list',
230
+ 'client.messaging.notifications.retrieve',
231
+ 'client.messaging.notifications.retrieveUnreadCount',
232
+ 'client.messaging.notifications.retrieveUnreadSummary',
233
+ 'client.messaging.notifications.actions.dismiss',
234
+ 'client.messaging.notifications.actions.markAllSeen',
235
+ 'client.messaging.notifications.actions.read',
236
+ 'client.messaging.notifications.actions.seen',
237
+ 'client.messaging.announcements.list',
238
+ 'client.messaging.announcements.retrieve',
239
+ 'client.messaging.announcements.actions.dismiss',
240
+ 'client.messaging.announcements.actions.read',
241
+ 'client.messaging.announcements.actions.seen',
242
+ 'client.messaging.conversations.create',
243
+ 'client.messaging.conversations.list',
244
+ 'client.messaging.conversations.retrieve',
245
+ 'client.messaging.conversations.update',
246
+ 'client.messaging.conversations.actions.archive',
247
+ 'client.messaging.conversations.actions.assign',
248
+ 'client.messaging.conversations.actions.hide',
249
+ 'client.messaging.conversations.actions.leave',
250
+ 'client.messaging.conversations.actions.mute',
251
+ 'client.messaging.conversations.actions.read',
252
+ 'client.messaging.conversations.actions.redact',
253
+ 'client.messaging.conversations.actions.report',
254
+ 'client.messaging.conversations.actions.setLegalHold',
255
+ 'client.messaging.conversations.actions.setStatus',
256
+ 'client.messaging.conversations.actions.unarchive',
257
+ 'client.messaging.conversations.actions.unhide',
258
+ 'client.messaging.conversations.actions.unmute',
259
+ 'client.messaging.conversations.links.create',
260
+ 'client.messaging.conversations.links.delete',
261
+ 'client.messaging.conversations.links.list',
262
+ 'client.messaging.conversations.messages.create',
263
+ 'client.messaging.conversations.messages.list',
264
+ 'client.messaging.conversations.participants.create',
265
+ 'client.messaging.conversations.participants.delete',
266
+ 'client.messaging.conversations.participants.actions.setRole',
267
+ 'client.messaging.conversations.attachments.actions.uploadURL',
268
+ 'client.messaging.messages.update',
269
+ 'client.messaging.messages.actions.approveSend',
270
+ 'client.messaging.messages.actions.cancel',
271
+ 'client.messaging.messages.actions.reject',
272
+ 'client.messaging.groups.create',
273
+ 'client.messaging.groups.delete',
274
+ 'client.messaging.groups.list',
275
+ 'client.messaging.groups.retrieve',
276
+ 'client.messaging.groups.update',
277
+ 'client.messaging.groups.members.create',
278
+ 'client.messaging.groups.members.delete',
279
+ 'client.messaging.blocks.create',
280
+ 'client.messaging.blocks.delete',
281
+ 'client.messaging.blocks.list',
282
+ 'client.messaging.preferences.list',
283
+ 'client.messaging.preferences.update',
284
+ 'client.messaging.emailDomains.create',
285
+ 'client.messaging.emailDomains.delete',
286
+ 'client.messaging.emailDomains.list',
287
+ 'client.messaging.emailDomains.retrieve',
288
+ 'client.messaging.emailDomains.actions.verify',
289
+ 'client.messaging.emailInboxes.create',
290
+ 'client.messaging.emailInboxes.delete',
291
+ 'client.messaging.emailInboxes.list',
292
+ 'client.messaging.emailInboxes.retrieve',
293
+ 'client.messaging.emailInboxes.update',
294
+ 'client.sales.accountGroups.create',
295
+ 'client.sales.accountGroups.delete',
296
+ 'client.sales.accountGroups.list',
297
+ 'client.sales.accountGroups.retrieve',
298
+ 'client.sales.accountGroups.update',
299
+ 'client.sales.accountPrices.create',
300
+ 'client.sales.accountPrices.delete',
301
+ 'client.sales.accountPrices.list',
302
+ 'client.sales.accountPrices.retrieve',
303
+ 'client.sales.accountPrices.update',
304
+ 'client.sales.accountPrices.actions.exportPriceList',
305
+ 'client.sales.addresses.create',
306
+ 'client.sales.addresses.delete',
307
+ 'client.sales.addresses.list',
308
+ 'client.sales.addresses.retrieve',
309
+ 'client.sales.addresses.update',
310
+ 'client.sales.accountStatuses.list',
311
+ 'client.sales.accountStatuses.retrieve',
312
+ 'client.sales.accountUsers.salesTargets.create',
313
+ 'client.sales.accountUsers.salesTargets.list',
314
+ 'client.sales.accountUsers.salesTargets.update',
315
+ 'client.sales.priorities.list',
316
+ 'client.sales.priorities.retrieve',
317
+ 'client.sales.customers.create',
318
+ 'client.sales.customers.delete',
319
+ 'client.sales.customers.list',
320
+ 'client.sales.customers.retrieve',
321
+ 'client.sales.customers.retrieveLeadTime',
322
+ 'client.sales.customers.update',
323
+ 'client.sales.customers.actions.merge',
324
+ 'client.sales.contacts.actions.findByEmail',
325
+ 'client.sales.orderDiscounts.create',
326
+ 'client.sales.orderDiscounts.delete',
327
+ 'client.sales.orderDiscounts.list',
328
+ 'client.sales.orderDiscounts.retrieve',
329
+ 'client.sales.orderDiscounts.update',
330
+ 'client.sales.orderDiscounts.actions.findByCode',
331
+ 'client.sales.salesOrders.checkout',
332
+ 'client.sales.salesOrders.create',
333
+ 'client.sales.salesOrders.delete',
334
+ 'client.sales.salesOrders.list',
335
+ 'client.sales.salesOrders.priceQuote',
336
+ 'client.sales.salesOrders.retrieve',
337
+ 'client.sales.salesOrders.retrieveStatuses',
338
+ 'client.sales.salesOrders.update',
339
+ 'client.sales.salesOrders.actions.bulkDelete',
340
+ 'client.sales.salesOrders.actions.close',
341
+ 'client.sales.salesOrders.actions.createProductionRun',
342
+ 'client.sales.salesOrders.actions.issue',
343
+ 'client.sales.salesOrders.actions.open',
344
+ 'client.sales.salesOrders.actions.quoteCommitment',
345
+ 'client.sales.salesOrders.actions.quoteFreight',
346
+ 'client.sales.salesOrders.actions.unissue',
347
+ 'client.sales.salesOrders.lines.create',
348
+ 'client.sales.salesOrders.lines.delete',
349
+ 'client.sales.salesOrders.lines.update',
350
+ 'client.sales.salesOrders.lines.actions.reorder',
351
+ 'client.sales.volumeDiscounts.create',
352
+ 'client.sales.volumeDiscounts.delete',
353
+ 'client.sales.volumeDiscounts.list',
354
+ 'client.sales.volumeDiscounts.retrieve',
355
+ 'client.sales.volumeDiscounts.update',
356
+ 'client.finance.retrieveAdjustmentTypes',
357
+ 'client.finance.retrieveTransactionMethods',
358
+ 'client.finance.retrieveTransactionTypes',
359
+ 'client.finance.paymentTerms.create',
360
+ 'client.finance.paymentTerms.delete',
361
+ 'client.finance.paymentTerms.list',
362
+ 'client.finance.paymentTerms.retrieve',
363
+ 'client.finance.paymentTerms.update',
364
+ 'client.operations.retrieveDemandOverrideTypes',
365
+ 'client.operations.retrieveMachineDowntimeReasons',
366
+ 'client.operations.retrieveMachineStatus',
367
+ 'client.operations.retrieveScheduleDeviationTypes',
368
+ 'client.operations.shippingTerms.create',
369
+ 'client.operations.shippingTerms.delete',
370
+ 'client.operations.shippingTerms.list',
371
+ 'client.operations.shippingTerms.retrieve',
372
+ 'client.operations.shippingTerms.update',
373
+ 'client.operations.carriers.create',
374
+ 'client.operations.carriers.delete',
375
+ 'client.operations.carriers.list',
376
+ 'client.operations.carriers.retrieve',
377
+ 'client.operations.carriers.update',
378
+ 'client.operations.carriers.serviceLevels.create',
379
+ 'client.operations.carriers.serviceLevels.delete',
380
+ 'client.operations.carriers.serviceLevels.list',
381
+ 'client.operations.carriers.serviceLevels.retrieve',
382
+ 'client.operations.carriers.serviceLevels.update',
383
+ 'client.operations.departments.create',
384
+ 'client.operations.departments.delete',
385
+ 'client.operations.departments.list',
386
+ 'client.operations.departments.retrieve',
387
+ 'client.operations.departments.update',
388
+ 'client.operations.machines.create',
389
+ 'client.operations.machines.delete',
390
+ 'client.operations.machines.list',
391
+ 'client.operations.machines.retrieve',
392
+ 'client.operations.machines.update',
393
+ 'client.operations.machineDowntimeEvents.create',
394
+ 'client.operations.machineDowntimeEvents.delete',
395
+ 'client.operations.machineDowntimeEvents.list',
396
+ 'client.operations.machineDowntimeEvents.retrieve',
397
+ 'client.operations.machineDowntimeEvents.update',
398
+ 'client.operations.demandOverrides.create',
399
+ 'client.operations.demandOverrides.delete',
400
+ 'client.operations.demandOverrides.list',
401
+ 'client.operations.demandOverrides.retrieve',
402
+ 'client.operations.demandOverrides.update',
403
+ 'client.operations.productionSchedules.create',
404
+ 'client.operations.productionSchedules.delete',
405
+ 'client.operations.productionSchedules.list',
406
+ 'client.operations.productionSchedules.retrieve',
407
+ 'client.operations.productionSchedules.retrieveAtRiskOrders',
408
+ 'client.operations.productionSchedules.retrieveCurrent',
409
+ 'client.operations.productionSchedules.retrieveDerivedLines',
410
+ 'client.operations.productionSchedules.retrieveDeviations',
411
+ 'client.operations.productionSchedules.retrieveFinishedPolicies',
412
+ 'client.operations.productionSchedules.retrieveFinishingLines',
413
+ 'client.operations.productionSchedules.retrieveItemPolicies',
414
+ 'client.operations.productionSchedules.retrieveWeekReleasePreview',
415
+ 'client.operations.productionSchedules.lines.create',
416
+ 'client.operations.productionSchedules.lines.delete',
417
+ 'client.operations.productionSchedules.lines.list',
418
+ 'client.operations.productionSchedules.lines.update',
419
+ 'client.operations.productionSchedules.actions.archive',
420
+ 'client.operations.productionSchedules.actions.preview',
421
+ 'client.operations.productionSchedules.actions.previewRegenerate',
422
+ 'client.operations.productionSchedules.actions.publish',
423
+ 'client.operations.productionSchedules.actions.regenerate',
424
+ 'client.operations.productionSchedules.actions.releaseWeek',
425
+ 'client.operations.productionScheduleSettings.list',
426
+ 'client.operations.productionScheduleSettings.update',
427
+ 'client.operations.productionScheduleSettings.resources.delete',
428
+ 'client.operations.productionScheduleSettings.resources.list',
429
+ 'client.operations.productionScheduleSettings.resources.update',
430
+ 'client.operations.productionScheduleSettings.items.delete',
431
+ 'client.operations.productionScheduleSettings.items.list',
432
+ 'client.operations.productionScheduleSettings.items.retrieve',
433
+ 'client.operations.productionScheduleSettings.items.update',
434
+ 'client.operations.fulfillmentRecommendations.list',
435
+ 'client.operations.fulfillmentRecommendations.actions.apply',
436
+ 'client.operations.operatingCalendars.create',
437
+ 'client.operations.operatingCalendars.delete',
438
+ 'client.operations.operatingCalendars.list',
439
+ 'client.operations.operatingCalendars.retrieve',
440
+ 'client.operations.operatingCalendars.update',
441
+ 'client.operations.operatingCalendars.closures.create',
442
+ 'client.operations.operatingCalendars.closures.delete',
443
+ 'client.operations.operatingCalendars.closures.list',
444
+ 'client.operations.locations.create',
445
+ 'client.operations.locations.delete',
446
+ 'client.operations.locations.list',
447
+ 'client.operations.locations.retrieve',
448
+ 'client.operations.locations.update',
449
+ 'client.operations.locations.actions.bulkUpsert',
450
+ 'client.operations.locationTypes.list',
451
+ 'client.operations.locationTypes.retrieve',
452
+ 'client.operations.shipments.actions.rateShop',
453
+ 'client.operations.scanningStations.create',
454
+ 'client.operations.scanningStations.delete',
455
+ 'client.operations.scanningStations.list',
456
+ 'client.operations.scanningStations.retrieve',
457
+ 'client.operations.scanningStations.update',
458
+ 'client.identity.retrievePermissionGroups',
459
+ 'client.identity.accountUsers.create',
460
+ 'client.identity.accountUsers.list',
461
+ 'client.identity.accountUsers.retrieve',
462
+ 'client.identity.accountUsers.update',
463
+ 'client.identity.accountUsers.actions.activate',
464
+ 'client.identity.accountUsers.actions.disable',
465
+ 'client.identity.accountUsers.actions.remove',
466
+ 'client.identity.accounts.updateFavicon',
467
+ 'client.identity.roles.create',
468
+ 'client.identity.roles.delete',
469
+ 'client.identity.roles.list',
470
+ 'client.identity.roles.retrieve',
471
+ 'client.identity.roles.update',
472
+ 'client.settings.portalDomains.create',
473
+ 'client.settings.portalDomains.delete',
474
+ 'client.settings.portalDomains.list',
475
+ 'client.settings.portalDomains.retrieve',
476
+ 'client.settings.portalDomains.actions.verify',
477
+ 'client.settings.integrations.create',
478
+ 'client.settings.integrations.delete',
479
+ 'client.settings.integrations.list',
480
+ 'client.settings.integrations.update',
481
+ ],
482
+ { threshold: 1, shouldSort: true },
483
+ );
484
+
485
+ function getMethodSuggestions(fullyQualifiedMethodName: string): string[] {
486
+ return fuse
487
+ .search(fullyQualifiedMethodName)
488
+ .map(({ item }) => item)
489
+ .slice(0, 5);
490
+ }
491
+
492
+ const proxyToObj = new WeakMap<any, any>();
493
+ const objToProxy = new WeakMap<any, any>();
494
+
495
+ type ClientProxyConfig = {
496
+ path: string[];
497
+ isBelievedBad?: boolean;
498
+ };
499
+
500
+ function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T {
501
+ let proxy: T = objToProxy.get(obj);
502
+
503
+ if (!proxy) {
504
+ proxy = new Proxy(obj, {
505
+ get(target, prop, receiver) {
506
+ const propPath = [...path, String(prop)];
507
+ const value = Reflect.get(target, prop, receiver);
508
+
509
+ if (isBelievedBad || (!(prop in target) && value === undefined)) {
510
+ // If we're accessing a path that doesn't exist, it will probably eventually error.
511
+ // Let's proxy it and mark it bad so that we can control the error message.
512
+ // We proxy an empty class so that an invocation or construction attempt is possible.
513
+ return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true });
514
+ }
515
+
516
+ if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
517
+ return makeSdkProxy(value, { path: propPath, isBelievedBad });
518
+ }
519
+
520
+ return value;
521
+ },
522
+
523
+ apply(target, thisArg, args) {
524
+ if (isBelievedBad || typeof target !== 'function') {
525
+ const fullyQualifiedMethodName = path.join('.');
526
+ const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
527
+ throw new Error(
528
+ `${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`,
529
+ );
530
+ }
531
+
532
+ return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
533
+ },
534
+
535
+ construct(target, args, newTarget) {
536
+ if (isBelievedBad || typeof target !== 'function') {
537
+ const fullyQualifiedMethodName = path.join('.');
538
+ const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
539
+ throw new Error(
540
+ `${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`,
541
+ );
542
+ }
543
+
544
+ return Reflect.construct(target, args, newTarget);
545
+ },
546
+ });
547
+
548
+ objToProxy.set(obj, proxy);
549
+ proxyToObj.set(proxy, obj);
550
+ }
551
+
552
+ return proxy;
553
+ }
554
+
555
+ function parseError(code: string, error: unknown): string | undefined {
556
+ if (!(error instanceof Error)) return;
557
+ const cause = error.cause instanceof Error ? `: ${error.cause.message}` : '';
558
+ const message = error.name ? `${error.name}: ${error.message}${cause}` : `${error.message}${cause}`;
559
+ try {
560
+ // Deno uses V8; the first "<anonymous>:LINE:COLUMN" is the top of stack.
561
+ const lineNumber = error.stack?.match(/<anonymous>:([0-9]+):[0-9]+/)?.[1];
562
+ // -1 for the zero-based indexing
563
+ const line =
564
+ lineNumber &&
565
+ code
566
+ .split('\n')
567
+ .at(parseInt(lineNumber, 10) - 1)
568
+ ?.trim();
569
+ return line ? `${message}\n at line ${lineNumber}\n ${line}` : message;
570
+ } catch {
571
+ return message;
572
+ }
573
+ }
574
+
575
+ const fetch = async (req: Request): Promise<Response> => {
576
+ const { opts, code } = (await req.json()) as { opts: ClientOptions; code: string };
577
+
578
+ const runFunctionSource = code ? getRunFunctionSource(code) : null;
579
+ if (!runFunctionSource) {
580
+ const message =
581
+ code ?
582
+ 'The code is missing a top-level `run` function.'
583
+ : 'The code argument is missing. Provide one containing a top-level `run` function.';
584
+ return Response.json(
585
+ {
586
+ is_error: true,
587
+ result: `${message} Write code within this template:\n\n\`\`\`\nasync function run(client) {\n // Fill this out\n}\n\`\`\``,
588
+ log_lines: [],
589
+ err_lines: [],
590
+ } satisfies WorkerOutput,
591
+ { status: 400, statusText: 'Code execution error' },
592
+ );
593
+ }
594
+
595
+ const diagnostics = getTSDiagnostics(code);
596
+ if (diagnostics.length > 0) {
597
+ return Response.json(
598
+ {
599
+ is_error: true,
600
+ result: `The code contains TypeScript diagnostics:\n${diagnostics.join('\n')}`,
601
+ log_lines: [],
602
+ err_lines: [],
603
+ } satisfies WorkerOutput,
604
+ { status: 400, statusText: 'Code execution error' },
605
+ );
606
+ }
607
+
608
+ const client = new OpenMRP({
609
+ ...opts,
610
+ });
611
+
612
+ const log_lines: string[] = [];
613
+ const err_lines: string[] = [];
614
+ const originalConsole = globalThis.console;
615
+ globalThis.console = {
616
+ ...originalConsole,
617
+ log: (...args: unknown[]) => {
618
+ log_lines.push(util.format(...args));
619
+ },
620
+ error: (...args: unknown[]) => {
621
+ err_lines.push(util.format(...args));
622
+ },
623
+ };
624
+ try {
625
+ let run_ = async (client: any) => {};
626
+ run_ = (await tseval(`${code}\nexport default run;`)).default;
627
+ const result = await run_(makeSdkProxy(client, { path: ['client'] }));
628
+ return Response.json({
629
+ is_error: false,
630
+ result,
631
+ log_lines,
632
+ err_lines,
633
+ } satisfies WorkerOutput);
634
+ } catch (e) {
635
+ return Response.json(
636
+ {
637
+ is_error: true,
638
+ result: parseError(code, e),
639
+ log_lines,
640
+ err_lines,
641
+ } satisfies WorkerOutput,
642
+ { status: 400, statusText: 'Code execution error' },
643
+ );
644
+ } finally {
645
+ globalThis.console = originalConsole;
646
+ }
647
+ };
648
+
649
+ export default { fetch };