@adhd/apigen-engine-naming 0.2.2 → 0.2.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.
- package/CHANGELOG.md +4 -0
- package/index.js +2 -2
- package/index.mjs +32 -32
- package/package.json +19 -2
package/CHANGELOG.md
CHANGED
package/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("node:fs");const E=require("node:module");require("node:path");var h=typeof document<"u"?document.currentScript:null;E.createRequire(typeof document>"u"?require("url").pathToFileURL(__filename).href:h&&h.tagName.toUpperCase()==="SCRIPT"&&h.src||new URL("index.js",document.baseURI).href);const b=new Set(["string","number","boolean","integer"]);function P(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"?b.has(n):Array.isArray(n)?n.every(r=>b.has(r)||r==="null"):Array.isArray(t.enum)&&t.enum.length>0?t.enum.every(r=>["string","number","boolean"].includes(typeof r)):!1}function x(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(P)}const w={"decimal.js":"Decimal"};function k(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}Object.entries(w).map(([e,t])=>({pattern:new RegExp(`import\\(["'][^"']*${k(e)}[^"']*["']\\)\\.\\w+`,"g"),key:t}));function v(e){return e.words.join("-")}function K(e){return e.words.map((t,n)=>n===0?t:_(t)).join("")}function y(e){return e.words.map(_).join("")}function $(e){return e.words.join("_")}function M(e){return e.replace(/\.[^.]+$/,"").replace(/[._]+/g,"-")}function j(e,t={}){var l,u;const n=[e.namespace,...e.path],r=e.safe||x(e.input)?"GET":"POST",a=((u=(l=t.http)==null?void 0:l.verb)==null?void 0:u[e.id])??r,p="/"+n.map(c=>v(c)).join("/"),s=n.map(c=>$(c)).join("_"),i=n.map(c=>$(c)).slice(0,-1).join("."),f=y(n[n.length-2]??n[0]),d=y(n[n.length-1]),g=n.map(c=>v(c));return{http:{verb:a,route:p},mcp:{name:s},grpc:{package:i,service:f,method:d},cli:{path:g}}}function O(e,t,n){var a,p;const r=(p=(a=n.http)==null?void 0:a.verb)==null?void 0:p[e];return r==="GET"||r==="POST"?r:t["x-apigen-safe"]?"GET":"POST"}class S extends Error{constructor(t){const n=t.map(r=>` [${r.transport}] "${r.target}" ← ${r.ids[0]} vs ${r.ids[1]}`);super(`apigen-naming: ${t.length} projection collision(s) detected:
|
|
2
2
|
${n.join(`
|
|
3
|
-
`)}`),this.name="CollisionDetectedError",this.collisions=t}}function
|
|
3
|
+
`)}`),this.name="CollisionDetectedError",this.collisions=t}}function T(e,t={}){const n=new Map,r=new Map,a=new Map,p=new Map,s=[];for(const o of e){const i=j(o,t),f=`${i.http.verb} ${i.http.route}`,d=n.get(f);d!==void 0&&d!==o.id?s.push({transport:"http",target:f,ids:[d,o.id]}):n.set(f,o.id);const g=r.get(i.mcp.name);g!==void 0&&g!==o.id?s.push({transport:"mcp",target:i.mcp.name,ids:[g,o.id]}):r.set(i.mcp.name,o.id);const l=`${i.grpc.package}.${i.grpc.service}/${i.grpc.method}`,u=a.get(l);u!==void 0&&u!==o.id?s.push({transport:"grpc",target:l,ids:[u,o.id]}):a.set(l,o.id);const c=i.cli.path.join(" "),m=p.get(c);m!==void 0&&m!==o.id?s.push({transport:"cli",target:c,ids:[m,o.id]}):p.set(c,o.id)}if(s.length>0)throw new S(s)}function C(e,t){return`x-${e}-${t}`}function A(e,t){return`--${e}-${t}`}function R(e,t){const n=r=>r.toUpperCase().replace(/-/g,"_");return e==="adhd"?`APIGEN_${n(t)}`:`APIGEN_${n(e)}_${n(t)}`}const U=C;function z(e){let t=e.replace(/[^a-zA-Z0-9_$]/g,"_");return(t===""||/^[0-9]/.test(t))&&(t=`_${t}`),t}function _(e){return e.length===0?e:e[0].toUpperCase()+e.slice(1)}exports.CollisionDetectedError=S;exports.checkCollisions=T;exports.envelopeCliFlag=A;exports.envelopeEnvVar=R;exports.envelopeKey=C;exports.envelopeMetaKey=U;exports.httpVerb=O;exports.normalizeFileName=M;exports.project=j;exports.sanitizeIdentifier=z;exports.toCamel=K;exports.toKebab=v;exports.toPascal=y;exports.toSnake=$;
|
package/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import "ts-morph";
|
|
2
1
|
import "node:fs";
|
|
2
|
+
import { createRequire as j } from "node:module";
|
|
3
3
|
import "node:path";
|
|
4
|
-
import
|
|
4
|
+
j(import.meta.url);
|
|
5
5
|
const h = /* @__PURE__ */ new Set(["string", "number", "boolean", "integer"]);
|
|
6
|
-
function
|
|
6
|
+
function E(e) {
|
|
7
7
|
if (typeof e != "object" || e === null)
|
|
8
8
|
return !1;
|
|
9
9
|
const t = e;
|
|
@@ -12,21 +12,21 @@ function j(e) {
|
|
|
12
12
|
const { type: n } = t;
|
|
13
13
|
return typeof n == "string" ? h.has(n) : Array.isArray(n) ? n.every((r) => h.has(r) || r === "null") : Array.isArray(t.enum) && t.enum.length > 0 ? t.enum.every((r) => ["string", "number", "boolean"].includes(typeof r)) : !1;
|
|
14
14
|
}
|
|
15
|
-
function
|
|
15
|
+
function S(e) {
|
|
16
16
|
if (!e || typeof e != "object")
|
|
17
17
|
return !1;
|
|
18
18
|
const t = e.properties;
|
|
19
19
|
return t === void 0 || typeof t != "object" || t === null ? !1 : Object.values(t).every(
|
|
20
|
-
|
|
20
|
+
E
|
|
21
21
|
);
|
|
22
22
|
}
|
|
23
|
-
const
|
|
23
|
+
const x = {
|
|
24
24
|
"decimal.js": "Decimal"
|
|
25
25
|
};
|
|
26
|
-
function
|
|
26
|
+
function _(e) {
|
|
27
27
|
return e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
28
28
|
}
|
|
29
|
-
Object.entries(
|
|
29
|
+
Object.entries(x).map(([e, t]) => ({
|
|
30
30
|
// Matches: import("...{modSpec}...").<anyMemberName> (single or double quotes)
|
|
31
31
|
// The module specifier appears inside an absolute path, so we allow any
|
|
32
32
|
// characters between the quote and the specifier, and between the specifier
|
|
@@ -34,7 +34,7 @@ Object.entries(S).map(([e, t]) => ({
|
|
|
34
34
|
// …) is matched generically (`\w+`) rather than anchored to one spelling —
|
|
35
35
|
// see the doc comment above for why.
|
|
36
36
|
pattern: new RegExp(
|
|
37
|
-
`import\\(["'][^"']*${
|
|
37
|
+
`import\\(["'][^"']*${_(e)}[^"']*["']\\)\\.\\w+`,
|
|
38
38
|
"g"
|
|
39
39
|
),
|
|
40
40
|
key: t
|
|
@@ -56,20 +56,20 @@ function G(e) {
|
|
|
56
56
|
}
|
|
57
57
|
function P(e, t = {}) {
|
|
58
58
|
var l, u;
|
|
59
|
-
const n = [e.namespace, ...e.path], r = e.safe ||
|
|
59
|
+
const n = [e.namespace, ...e.path], r = e.safe || S(e.input) ? "GET" : "POST", a = ((u = (l = t.http) == null ? void 0 : l.verb) == null ? void 0 : u[e.id]) ?? r, p = "/" + n.map((s) => $(s)).join("/"), c = n.map((s) => y(s)).join("_"), i = n.map((s) => y(s)).slice(0, -1).join("."), f = v(n[n.length - 2] ?? n[0]), g = v(n[n.length - 1]), m = n.map((s) => $(s));
|
|
60
60
|
return {
|
|
61
|
-
http: { verb:
|
|
61
|
+
http: { verb: a, route: p },
|
|
62
62
|
mcp: { name: c },
|
|
63
|
-
grpc: { package: i, service:
|
|
63
|
+
grpc: { package: i, service: f, method: g },
|
|
64
64
|
cli: { path: m }
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
function K(e, t, n) {
|
|
68
|
-
var
|
|
69
|
-
const r = (
|
|
68
|
+
var a, p;
|
|
69
|
+
const r = (p = (a = n.http) == null ? void 0 : a.verb) == null ? void 0 : p[e];
|
|
70
70
|
return r === "GET" || r === "POST" ? r : t["x-apigen-safe"] ? "GET" : "POST";
|
|
71
71
|
}
|
|
72
|
-
class
|
|
72
|
+
class w extends Error {
|
|
73
73
|
constructor(t) {
|
|
74
74
|
const n = t.map(
|
|
75
75
|
(r) => ` [${r.transport}] "${r.target}" ← ${r.ids[0]} vs ${r.ids[1]}`
|
|
@@ -82,37 +82,37 @@ ${n.join(`
|
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
function T(e, t = {}) {
|
|
85
|
-
const n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(),
|
|
85
|
+
const n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map(), c = [];
|
|
86
86
|
for (const o of e) {
|
|
87
|
-
const i = P(o, t),
|
|
88
|
-
|
|
87
|
+
const i = P(o, t), f = `${i.http.verb} ${i.http.route}`, g = n.get(f);
|
|
88
|
+
g !== void 0 && g !== o.id ? c.push({
|
|
89
89
|
transport: "http",
|
|
90
|
-
target:
|
|
91
|
-
ids: [
|
|
92
|
-
}) : n.set(
|
|
90
|
+
target: f,
|
|
91
|
+
ids: [g, o.id]
|
|
92
|
+
}) : n.set(f, o.id);
|
|
93
93
|
const m = r.get(i.mcp.name);
|
|
94
94
|
m !== void 0 && m !== o.id ? c.push({
|
|
95
95
|
transport: "mcp",
|
|
96
96
|
target: i.mcp.name,
|
|
97
97
|
ids: [m, o.id]
|
|
98
98
|
}) : r.set(i.mcp.name, o.id);
|
|
99
|
-
const l = `${i.grpc.package}.${i.grpc.service}/${i.grpc.method}`, u =
|
|
99
|
+
const l = `${i.grpc.package}.${i.grpc.service}/${i.grpc.method}`, u = a.get(l);
|
|
100
100
|
u !== void 0 && u !== o.id ? c.push({
|
|
101
101
|
transport: "grpc",
|
|
102
102
|
target: l,
|
|
103
103
|
ids: [u, o.id]
|
|
104
|
-
}) :
|
|
105
|
-
const s = i.cli.path.join(" "), d =
|
|
104
|
+
}) : a.set(l, o.id);
|
|
105
|
+
const s = i.cli.path.join(" "), d = p.get(s);
|
|
106
106
|
d !== void 0 && d !== o.id ? c.push({
|
|
107
107
|
transport: "cli",
|
|
108
108
|
target: s,
|
|
109
109
|
ids: [d, o.id]
|
|
110
|
-
}) :
|
|
110
|
+
}) : p.set(s, o.id);
|
|
111
111
|
}
|
|
112
112
|
if (c.length > 0)
|
|
113
|
-
throw new
|
|
113
|
+
throw new w(c);
|
|
114
114
|
}
|
|
115
|
-
function
|
|
115
|
+
function C(e, t) {
|
|
116
116
|
return `x-${e}-${t}`;
|
|
117
117
|
}
|
|
118
118
|
function z(e, t) {
|
|
@@ -122,8 +122,8 @@ function N(e, t) {
|
|
|
122
122
|
const n = (r) => r.toUpperCase().replace(/-/g, "_");
|
|
123
123
|
return e === "adhd" ? `APIGEN_${n(t)}` : `APIGEN_${n(e)}_${n(t)}`;
|
|
124
124
|
}
|
|
125
|
-
const D =
|
|
126
|
-
function
|
|
125
|
+
const D = C;
|
|
126
|
+
function R(e) {
|
|
127
127
|
let t = e.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
128
128
|
return (t === "" || /^[0-9]/.test(t)) && (t = `_${t}`), t;
|
|
129
129
|
}
|
|
@@ -131,16 +131,16 @@ function b(e) {
|
|
|
131
131
|
return e.length === 0 ? e : e[0].toUpperCase() + e.slice(1);
|
|
132
132
|
}
|
|
133
133
|
export {
|
|
134
|
-
|
|
134
|
+
w as CollisionDetectedError,
|
|
135
135
|
T as checkCollisions,
|
|
136
136
|
z as envelopeCliFlag,
|
|
137
137
|
N as envelopeEnvVar,
|
|
138
|
-
|
|
138
|
+
C as envelopeKey,
|
|
139
139
|
D as envelopeMetaKey,
|
|
140
140
|
K as httpVerb,
|
|
141
141
|
G as normalizeFileName,
|
|
142
142
|
P as project,
|
|
143
|
-
|
|
143
|
+
R as sanitizeIdentifier,
|
|
144
144
|
M as toCamel,
|
|
145
145
|
$ as toKebab,
|
|
146
146
|
v as toPascal,
|
package/package.json
CHANGED
|
@@ -1,13 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adhd/apigen-engine-naming",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@adhd/apigen-core-client": "^0.3.
|
|
5
|
+
"@adhd/apigen-core-client": "^0.3.1"
|
|
6
6
|
},
|
|
7
7
|
"main": "./index.js",
|
|
8
8
|
"module": "./index.mjs",
|
|
9
9
|
"typings": "./index.d.ts",
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"description": "Naming conventions for apigen code generation",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"api",
|
|
16
|
+
"naming",
|
|
17
|
+
"codegen",
|
|
18
|
+
"typescript"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/PseudoSky/adhd.git",
|
|
24
|
+
"directory": "packages/apigen/apigen-engine-naming"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/PseudoSky/adhd/tree/main/packages/apigen/apigen-engine-naming#readme",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/PseudoSky/adhd/issues"
|
|
12
29
|
}
|
|
13
30
|
}
|