two-stroke 5.4.0 → 5.6.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.
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from "node:fs";
4
+ import openapiTS from "openapi-typescript";
5
+ import prettier from "prettier";
6
+ import ts from "typescript";
7
+
8
+ const services = process.argv[2].split(",");
9
+
10
+ const UUID = ts.factory.createTypeReferenceNode(
11
+ ts.factory.createIdentifier("UUID"),
12
+ );
13
+ const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull());
14
+
15
+ await Promise.all(
16
+ services.map(async (service) => {
17
+ const output = await openapiTS(`${service}/doc`, {
18
+ transform(schemaObject) {
19
+ if ("format" in schemaObject && schemaObject.format === "uuid") {
20
+ return schemaObject.nullable
21
+ ? ts.factory.createUnionTypeNode([UUID, NULL])
22
+ : UUID;
23
+ }
24
+ },
25
+ });
26
+ const printer = ts.createPrinter({});
27
+ const resultFile = ts.createSourceFile(
28
+ `src/__definitions__/${service.replace("https://", "")}-definitions.ts`,
29
+ "",
30
+ ts.ScriptTarget.Latest,
31
+ );
32
+ const result = printer.printNode(
33
+ ts.EmitHint.Unspecified,
34
+ output[0],
35
+ resultFile,
36
+ );
37
+ fs.writeFileSync(
38
+ `src/__definitions__/${service.replace("https://", "")}-definitions.ts`,
39
+ await prettier.format(
40
+ `// Autogenerated by two-stroke
41
+
42
+ ${fs.readFileSync("src/__definitions__/type-definitions.ts", "utf-8")}
43
+
44
+ ${result}`,
45
+ { parser: "typescript", printWidth: 100 },
46
+ ),
47
+ );
48
+ }),
49
+ );
package/package.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "bin": {
3
+ "api-types": "./bin/api-types.mjs",
3
4
  "bulk": "./bin/bulk.mjs",
4
5
  "deploy": "./bin/deploy.mjs",
5
6
  "dev": "./bin/dev.mjs",
@@ -51,7 +52,7 @@
51
52
  "name": "two-stroke",
52
53
  "packageManager": "yarn@4.6.0",
53
54
  "type": "module",
54
- "version": "5.4.0",
55
+ "version": "5.6.0",
55
56
  "devDependencies": {
56
57
  "@types/eslint": "^9.6.1",
57
58
  "eslint": "^9.32.0",
package/src/index.ts CHANGED
@@ -92,12 +92,32 @@ export function twoStroke<T extends Env>(title: string, release: string) {
92
92
  });
93
93
  }
94
94
  if (route.method === "POST" || route.method === "PUT") {
95
- const rawBody = route.input
96
- ? req.headers.get("Content-Type") ===
97
- "application/x-www-form-urlencoded"
98
- ? Object.fromEntries(new URLSearchParams(await req.text()))
99
- : await req.json()
100
- : undefined;
95
+ let rawBody;
96
+ try {
97
+ rawBody = route.input
98
+ ? req.headers.get("Content-Type") ===
99
+ "application/x-www-form-urlencoded"
100
+ ? Object.fromEntries(new URLSearchParams(await req.text()))
101
+ : await req.json()
102
+ : undefined;
103
+ } catch (e) {
104
+ console.error({
105
+ message: "Request body is required'",
106
+ error: e instanceof Error ? e.message : "Error",
107
+ });
108
+ return new Response(
109
+ JSON.stringify({
110
+ error: "Request body is required",
111
+ issues: [],
112
+ }),
113
+ {
114
+ status: 400,
115
+ headers: {
116
+ "Access-Control-Allow-Origin": "*",
117
+ },
118
+ },
119
+ );
120
+ }
101
121
  const body = route.input
102
122
  ? route.input.safeParse(rawBody)
103
123
  : {