@adhd/apigen-codegen-openapi 0.1.1 → 0.1.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/index.js +1 -1
- package/index.mjs +71 -30
- package/lib/to-openapi.d.ts +1 -1
- package/package.json +5 -1
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("ts-morph");require("node:fs");require("node:path");require("ts-json-schema-generator");const g=new Set(["string","number","boolean","integer"]);function j(e){if(typeof e!="object"||e===null)return!1;const t=e;if(t.$ref!==void 0||t.oneOf||t.anyOf||t.allOf)return!1;const{type:n}=t;return typeof n=="string"?g.has(n):Array.isArray(n)?n.every(r=>g.has(r)||r==="null"):Array.isArray(t.enum)&&t.enum.length>0?t.enum.every(r=>["string","number","boolean"].includes(typeof r)):!1}function v(e){if(!e||typeof e!="object")return!1;const t=e.properties;return t===void 0||typeof t!="object"||t===null?!1:Object.values(t).every(j)}const O={"decimal.js":"Decimal"};function q(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}Object.entries(O).map(([e,t])=>({pattern:new RegExp(`import\\(["'][^"']*${q(e)}[^"']*["']\\)\\.\\w+`,"g"),key:t}));function m(e){return e.words.join("-")}function b(e){return e.words.map(k).join("")}function h(e){return e.words.join("_")}function S(e,t={}){var y,d;const n=[e.namespace,...e.path],r=e.safe||v(e.input)?"GET":"POST",o=((d=(y=t.http)==null?void 0:y.verb)==null?void 0:d[e.id])??r,s="/"+n.map(i=>m(i)).join("/"),u=n.map(i=>h(i)).join("_"),a=n.map(i=>h(i)).slice(0,-1).join("."),f=b(n[n.length-2]??n[0]),p=b(n[n.length-1]),c=n.map(i=>m(i));return{http:{verb:o,route:s},mcp:{name:u},grpc:{package:a,service:f,method:p},cli:{path:c}}}function k(e){return e.length===0?e:e[0].toUpperCase()+e.slice(1)}function A(e,t={}){const n=t.title??"API",r=t.version??"0.0.0",o={};for(const s of e){const u=S(s),{verb:l,route:a}=u.http,f=l.toLowerCase(),p={operationId:s.id,summary:s.id,responses:{200:P(s.output)}};if(l==="GET")p.parameters=_(s.input);else{const c=s.input;typeof c=="object"&&c!==null&&Object.keys(c).length>0&&(p.requestBody={required:!0,content:{"application/json":{schema:c}}})}o[a]===void 0&&(o[a]={}),o[a][f]=p}return{openapi:"3.1.0",info:{title:n,version:r},paths:o}}function P(e){return typeof e=="object"&&e!==null&&Object.keys(e).length>0?{description:"Success",content:{"application/json":{schema:e}}}:{description:"Success"}}function _(e){if(typeof e!="object"||e===null||typeof e.properties!="object"||e.properties===null)return[];const t=Array.isArray(e.required)?e.required:[],n=e.properties;return Object.entries(n).map(([r,o])=>({name:r,in:"query",required:t.includes(r),schema:o}))}const w="@adhd/apigen-codegen-openapi";exports.__apigen_pkg=w;exports.toOpenApi=A;
|
package/index.mjs
CHANGED
|
@@ -1,39 +1,80 @@
|
|
|
1
|
+
import "ts-morph";
|
|
2
|
+
import "node:fs";
|
|
3
|
+
import "node:path";
|
|
4
|
+
import "ts-json-schema-generator";
|
|
5
|
+
const d = /* @__PURE__ */ new Set(["string", "number", "boolean", "integer"]);
|
|
6
|
+
function j(e) {
|
|
7
|
+
if (typeof e != "object" || e === null)
|
|
8
|
+
return !1;
|
|
9
|
+
const t = e;
|
|
10
|
+
if (t.$ref !== void 0 || t.oneOf || t.anyOf || t.allOf)
|
|
11
|
+
return !1;
|
|
12
|
+
const { type: n } = t;
|
|
13
|
+
return typeof n == "string" ? d.has(n) : Array.isArray(n) ? n.every((r) => d.has(r) || r === "null") : Array.isArray(t.enum) && t.enum.length > 0 ? t.enum.every((r) => ["string", "number", "boolean"].includes(typeof r)) : !1;
|
|
14
|
+
}
|
|
15
|
+
function v(e) {
|
|
16
|
+
if (!e || typeof e != "object")
|
|
17
|
+
return !1;
|
|
18
|
+
const t = e.properties;
|
|
19
|
+
return t === void 0 || typeof t != "object" || t === null ? !1 : Object.values(t).every(
|
|
20
|
+
j
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
const O = {
|
|
24
|
+
"decimal.js": "Decimal"
|
|
25
|
+
};
|
|
26
|
+
function S(e) {
|
|
27
|
+
return e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
28
|
+
}
|
|
29
|
+
Object.entries(O).map(([e, t]) => ({
|
|
30
|
+
// Matches: import("...{modSpec}...").<anyMemberName> (single or double quotes)
|
|
31
|
+
// The module specifier appears inside an absolute path, so we allow any
|
|
32
|
+
// characters between the quote and the specifier, and between the specifier
|
|
33
|
+
// and the closing quote. The trailing member accessor (`.default`, `.Decimal`,
|
|
34
|
+
// …) is matched generically (`\w+`) rather than anchored to one spelling —
|
|
35
|
+
// see the doc comment above for why.
|
|
36
|
+
pattern: new RegExp(
|
|
37
|
+
`import\\(["'][^"']*${S(e)}[^"']*["']\\)\\.\\w+`,
|
|
38
|
+
"g"
|
|
39
|
+
),
|
|
40
|
+
key: t
|
|
41
|
+
}));
|
|
1
42
|
function g(e) {
|
|
2
43
|
return e.words.join("-");
|
|
3
44
|
}
|
|
4
|
-
function
|
|
5
|
-
return e.words.map(
|
|
45
|
+
function b(e) {
|
|
46
|
+
return e.words.map(A).join("");
|
|
6
47
|
}
|
|
7
|
-
function
|
|
48
|
+
function h(e) {
|
|
8
49
|
return e.words.join("_");
|
|
9
50
|
}
|
|
10
|
-
function
|
|
11
|
-
var
|
|
12
|
-
const
|
|
51
|
+
function k(e, t = {}) {
|
|
52
|
+
var m, y;
|
|
53
|
+
const n = [e.namespace, ...e.path], r = e.safe || v(e.input) ? "GET" : "POST", o = ((y = (m = t.http) == null ? void 0 : m.verb) == null ? void 0 : y[e.id]) ?? r, s = "/" + n.map((c) => g(c)).join("/"), u = n.map((c) => h(c)).join("_"), a = n.map((c) => h(c)).slice(0, -1).join("."), f = b(n[n.length - 2] ?? n[0]), p = b(n[n.length - 1]), i = n.map((c) => g(c));
|
|
13
54
|
return {
|
|
14
|
-
http: { verb: o, route:
|
|
55
|
+
http: { verb: o, route: s },
|
|
15
56
|
mcp: { name: u },
|
|
16
|
-
grpc: { package: a, service:
|
|
57
|
+
grpc: { package: a, service: f, method: p },
|
|
17
58
|
cli: { path: i }
|
|
18
59
|
};
|
|
19
60
|
}
|
|
20
|
-
function
|
|
61
|
+
function A(e) {
|
|
21
62
|
return e.length === 0 ? e : e[0].toUpperCase() + e.slice(1);
|
|
22
63
|
}
|
|
23
|
-
function
|
|
24
|
-
const
|
|
25
|
-
for (const
|
|
26
|
-
const u =
|
|
27
|
-
operationId:
|
|
28
|
-
summary:
|
|
64
|
+
function $(e, t = {}) {
|
|
65
|
+
const n = t.title ?? "API", r = t.version ?? "0.0.0", o = {};
|
|
66
|
+
for (const s of e) {
|
|
67
|
+
const u = k(s), { verb: l, route: a } = u.http, f = l.toLowerCase(), p = {
|
|
68
|
+
operationId: s.id,
|
|
69
|
+
summary: s.id,
|
|
29
70
|
responses: {
|
|
30
|
-
200:
|
|
71
|
+
200: P(s.output)
|
|
31
72
|
}
|
|
32
73
|
};
|
|
33
|
-
if (
|
|
34
|
-
p.parameters =
|
|
74
|
+
if (l === "GET")
|
|
75
|
+
p.parameters = q(s.input);
|
|
35
76
|
else {
|
|
36
|
-
const i =
|
|
77
|
+
const i = s.input;
|
|
37
78
|
typeof i == "object" && i !== null && Object.keys(i).length > 0 && (p.requestBody = {
|
|
38
79
|
required: !0,
|
|
39
80
|
content: {
|
|
@@ -43,15 +84,15 @@ function q(e, r = {}) {
|
|
|
43
84
|
}
|
|
44
85
|
});
|
|
45
86
|
}
|
|
46
|
-
o[a] === void 0 && (o[a] = {}), o[a][
|
|
87
|
+
o[a] === void 0 && (o[a] = {}), o[a][f] = p;
|
|
47
88
|
}
|
|
48
89
|
return {
|
|
49
90
|
openapi: "3.1.0",
|
|
50
|
-
info: { title:
|
|
91
|
+
info: { title: n, version: r },
|
|
51
92
|
paths: o
|
|
52
93
|
};
|
|
53
94
|
}
|
|
54
|
-
function
|
|
95
|
+
function P(e) {
|
|
55
96
|
return typeof e == "object" && e !== null && Object.keys(e).length > 0 ? {
|
|
56
97
|
description: "Success",
|
|
57
98
|
content: {
|
|
@@ -59,19 +100,19 @@ function v(e) {
|
|
|
59
100
|
}
|
|
60
101
|
} : { description: "Success" };
|
|
61
102
|
}
|
|
62
|
-
function
|
|
103
|
+
function q(e) {
|
|
63
104
|
if (typeof e != "object" || e === null || typeof e.properties != "object" || e.properties === null)
|
|
64
105
|
return [];
|
|
65
|
-
const
|
|
66
|
-
return Object.entries(
|
|
67
|
-
name:
|
|
106
|
+
const t = Array.isArray(e.required) ? e.required : [], n = e.properties;
|
|
107
|
+
return Object.entries(n).map(([r, o]) => ({
|
|
108
|
+
name: r,
|
|
68
109
|
in: "query",
|
|
69
|
-
required:
|
|
110
|
+
required: t.includes(r),
|
|
70
111
|
schema: o
|
|
71
112
|
}));
|
|
72
113
|
}
|
|
73
|
-
const
|
|
114
|
+
const B = "@adhd/apigen-codegen-openapi";
|
|
74
115
|
export {
|
|
75
|
-
|
|
76
|
-
|
|
116
|
+
B as __apigen_pkg,
|
|
117
|
+
$ as toOpenApi
|
|
77
118
|
};
|
package/lib/to-openapi.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adhd/apigen-codegen-openapi",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"main": "./index.js",
|
|
5
5
|
"module": "./index.mjs",
|
|
6
6
|
"typings": "./index.d.ts",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@adhd/apigen-core-client": "^0.1.0",
|
|
12
|
+
"@adhd/apigen-engine-naming": "^0.1.1"
|
|
9
13
|
}
|
|
10
14
|
}
|