apiwork 0.0.0 → 0.0.2
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/README.md +311 -0
- package/dist/bin/apiwork.js +59 -0
- package/dist/chunk-4AJOYIJC.js +37 -0
- package/dist/chunk-7NDS6XB6.js +57 -0
- package/dist/chunk-MWFEILAW.js +28 -0
- package/dist/chunk-NQPUJJ3O.js +37 -0
- package/dist/chunk-QGKZLUZR.js +1151 -0
- package/dist/chunk-ZP6ZWEPI.js +127 -0
- package/dist/index.js +9 -0
- package/dist/sorbus/index.js +8 -0
- package/dist/typescript/index.js +8 -0
- package/dist/zod/index.js +8 -0
- package/package.json +57 -4
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildEndpoints,
|
|
3
|
+
buildSchemas,
|
|
4
|
+
buildScopeIndex,
|
|
5
|
+
pascalCase,
|
|
6
|
+
resolveClientIdentifier,
|
|
7
|
+
resolveDomainIdentifier,
|
|
8
|
+
resolveEndpointIdentifier,
|
|
9
|
+
resolveGenerateOptions
|
|
10
|
+
} from "./chunk-QGKZLUZR.js";
|
|
11
|
+
import {
|
|
12
|
+
camelCase
|
|
13
|
+
} from "./chunk-MWFEILAW.js";
|
|
14
|
+
|
|
15
|
+
// src/sorbus/client.ts
|
|
16
|
+
function buildClient(schema, resolvedOptions, context) {
|
|
17
|
+
const lines = [];
|
|
18
|
+
const extension = resolvedOptions.importExtension;
|
|
19
|
+
const resources = schema.resources.map((resource) => ({
|
|
20
|
+
camelIdentifier: camelCase(resource.identifier),
|
|
21
|
+
pascalIdentifier: pascalCase(resource.identifier)
|
|
22
|
+
}));
|
|
23
|
+
const clientName = resolveClientIdentifier("Client", context);
|
|
24
|
+
const createClientName = resolveClientIdentifier("createClient", context);
|
|
25
|
+
const contractName = resolveClientIdentifier("contract", context);
|
|
26
|
+
lines.push("import { createClientFactory } from 'sorbus';");
|
|
27
|
+
lines.push("");
|
|
28
|
+
const typeImports = resources.map(
|
|
29
|
+
(resource) => resolveEndpointIdentifier(
|
|
30
|
+
`${resource.pascalIdentifier}OperationTree`,
|
|
31
|
+
context
|
|
32
|
+
)
|
|
33
|
+
).join(", ");
|
|
34
|
+
lines.push(`import type { ${typeImports} } from './endpoints${extension}';`);
|
|
35
|
+
lines.push(`import { ${contractName} } from './contract${extension}';`);
|
|
36
|
+
lines.push("");
|
|
37
|
+
lines.push(`export interface ${clientName} {`);
|
|
38
|
+
for (const resource of resources) {
|
|
39
|
+
const operationTree = resolveEndpointIdentifier(
|
|
40
|
+
`${resource.pascalIdentifier}OperationTree`,
|
|
41
|
+
context
|
|
42
|
+
);
|
|
43
|
+
lines.push(` ${resource.camelIdentifier}: ${operationTree};`);
|
|
44
|
+
}
|
|
45
|
+
lines.push("}");
|
|
46
|
+
lines.push("");
|
|
47
|
+
lines.push(
|
|
48
|
+
`export const ${createClientName} = createClientFactory<${clientName}>(${contractName});`
|
|
49
|
+
);
|
|
50
|
+
lines.push("");
|
|
51
|
+
return lines.join("\n");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/sorbus/contract.ts
|
|
55
|
+
function buildContract(schema, resolvedOptions, context) {
|
|
56
|
+
const lines = [];
|
|
57
|
+
const extension = resolvedOptions.importExtension;
|
|
58
|
+
const hasError = schema.types.some((type) => type.name === "error");
|
|
59
|
+
const resources = schema.resources.map((resource) => ({
|
|
60
|
+
camelIdentifier: camelCase(resource.identifier),
|
|
61
|
+
identifier: resource.identifier
|
|
62
|
+
}));
|
|
63
|
+
const contractName = resolveClientIdentifier("contract", context);
|
|
64
|
+
const schemaImports = [];
|
|
65
|
+
if (hasError) {
|
|
66
|
+
const errorSchemaName = `${resolveDomainIdentifier("error", context)}Schema`;
|
|
67
|
+
schemaImports.push(errorSchemaName);
|
|
68
|
+
}
|
|
69
|
+
if (schemaImports.length > 0) {
|
|
70
|
+
lines.push(
|
|
71
|
+
`import { ${schemaImports.join(", ")} } from './api${extension}';`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
const valueImports = resources.map(
|
|
75
|
+
(resource) => resolveEndpointIdentifier(resource.camelIdentifier, context)
|
|
76
|
+
).join(", ");
|
|
77
|
+
lines.push(`import { ${valueImports} } from './endpoints${extension}';`);
|
|
78
|
+
lines.push("");
|
|
79
|
+
lines.push(`export const ${contractName} = {`);
|
|
80
|
+
lines.push(" endpoints: {");
|
|
81
|
+
for (const resource of resources) {
|
|
82
|
+
const name = resolveEndpointIdentifier(resource.camelIdentifier, context);
|
|
83
|
+
lines.push(` ${name},`);
|
|
84
|
+
}
|
|
85
|
+
lines.push(" },");
|
|
86
|
+
if (hasError) {
|
|
87
|
+
const errorSchemaName = `${resolveDomainIdentifier("error", context)}Schema`;
|
|
88
|
+
lines.push(` error: ${errorSchemaName},`);
|
|
89
|
+
}
|
|
90
|
+
lines.push("} as const;");
|
|
91
|
+
lines.push("");
|
|
92
|
+
return lines.join("\n");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// src/sorbus/generate.ts
|
|
96
|
+
function generate(schema, options = {}) {
|
|
97
|
+
const resolvedOptions = resolveGenerateOptions(options);
|
|
98
|
+
const scopeIndex = buildScopeIndex(schema);
|
|
99
|
+
const context = { options: resolvedOptions, scopeIndex };
|
|
100
|
+
const files = /* @__PURE__ */ new Map();
|
|
101
|
+
for (const [filename, content] of buildSchemas(
|
|
102
|
+
schema,
|
|
103
|
+
{ zod: true },
|
|
104
|
+
resolvedOptions
|
|
105
|
+
)) {
|
|
106
|
+
files.set(filename, content);
|
|
107
|
+
}
|
|
108
|
+
for (const [filename, content] of buildEndpoints(
|
|
109
|
+
schema,
|
|
110
|
+
scopeIndex,
|
|
111
|
+
{
|
|
112
|
+
endpoints: true,
|
|
113
|
+
reexportChildren: true,
|
|
114
|
+
zod: true
|
|
115
|
+
},
|
|
116
|
+
resolvedOptions
|
|
117
|
+
)) {
|
|
118
|
+
files.set(filename, content);
|
|
119
|
+
}
|
|
120
|
+
files.set("contract.ts", buildContract(schema, resolvedOptions, context));
|
|
121
|
+
files.set("client.ts", buildClient(schema, resolvedOptions, context));
|
|
122
|
+
return files;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export {
|
|
126
|
+
generate
|
|
127
|
+
};
|
package/dist/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,60 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "apiwork",
|
|
3
|
-
"version": "0.0.0",
|
|
4
|
-
"description": "Codegen for Apiwork APIs",
|
|
5
2
|
"author": "Skiftle",
|
|
6
|
-
"
|
|
3
|
+
"bin": {
|
|
4
|
+
"apiwork": "./dist/bin/apiwork.js"
|
|
5
|
+
},
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/skiftle/apiwork-js/issues"
|
|
8
|
+
},
|
|
9
|
+
"description": "JavaScript toolkit for Apiwork.",
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@biomejs/biome": "2.4.12",
|
|
12
|
+
"@skiftle/biome-config": "0.0.7",
|
|
13
|
+
"@types/node": "25.6.0",
|
|
14
|
+
"tsup": "8.5.1",
|
|
15
|
+
"typescript": "6.0.2",
|
|
16
|
+
"vitest": "4.1.4"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=22"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"./sorbus": {
|
|
27
|
+
"import": "./dist/sorbus/index.js",
|
|
28
|
+
"types": "./dist/sorbus/index.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./typescript": {
|
|
31
|
+
"import": "./dist/typescript/index.js",
|
|
32
|
+
"types": "./dist/typescript/index.d.ts"
|
|
33
|
+
},
|
|
34
|
+
"./zod": {
|
|
35
|
+
"import": "./dist/zod/index.js",
|
|
36
|
+
"types": "./dist/zod/index.d.ts"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"homepage": "https://github.com/skiftle/apiwork-js",
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"name": "apiwork",
|
|
45
|
+
"packageManager": "pnpm@10.33.0",
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "https://github.com/skiftle/apiwork-js.git"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsup",
|
|
52
|
+
"check": "biome check .",
|
|
53
|
+
"check:write": "biome check --write .",
|
|
54
|
+
"prepublishOnly": "npm run build",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"typecheck": "tsc --noEmit"
|
|
57
|
+
},
|
|
58
|
+
"type": "module",
|
|
59
|
+
"version": "0.0.2"
|
|
7
60
|
}
|