@webority/api-contract 0.163.0 → 0.164.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.
package/dist/emitFile.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import{emitHubs as e}from"./emitHubs.js";import{emitType as t,literalType as n,pad as r,stringEnumMembers as i}from"./emitSchema.js";import{ContractError as a}from"./model.js";import{propertyKey as o,quote as s,typeName as c,uniqueName as l}from"./names.js";var u=[`Blob`,`FormData`,`Record`,`Readonly`,`OperationId`,`OperationTypes`],d=`Blob`,f=[`multipart/form-data`,`application/x-www-form-urlencoded`];function p(t,n){let r=new Set(u),i=/* @__PURE__ */ new Map;for(let e of t.schemas.keys())i.set(e,l(c(e),r));let a=[h(n)];for(let[e,n]of t.schemas)a.push(...m(g(i,e),n,i));let o=t.operations.filter(e=>e.id!==``).map(e=>({operation:e,op:_(c(e.id),r)}));for(let{operation:e,op:t}of o)a.push(v(t,e,i)),a.push(S(t,e,i));return a.push(k(o,i,t.schemas)),a.push(`export type OperationId = keyof typeof operations;`),a.push(A(o)),a.push(...e(t.hubs,i,r)),`${a.join(`
2
2
 
3
- `)}\n`}function m(e,r,a){let s=i(r);return s===void 0?[`export type ${e} = ${t(r,a,0)};`]:[`export const ${e} = { ${s.map(e=>`${o(e)}: ${n(e)}`).join(`, `)} } as const;`,`export type ${e} = (typeof ${e})[keyof typeof ${e}];`]}function h(e){return`// Generated by @webority/api-contract from ${e.name} (sha256 ${e.sha256}). Do not edit: change the backend contract and generate again.`}function g(e,t){let n=e.get(t);if(n===void 0)throw Error(`no type name was made for schema ${t}`);return n}function _(e,t){for(let n=1;;n+=1){let r=n===1?e:`${e}${n}`,i=`${r}Request`,a=`${r}Response`;if(!t.has(r)&&!t.has(i)&&!t.has(a))return t.add(r),t.add(i),t.add(a),r}}function v(e,t,n){let r=t.parameters.filter(e=>e.place===`path`),i=t.parameters.filter(e=>e.place===`query`),a=t.parameters.filter(e=>e.place===`header`),o=[];return r.length>0&&o.push(y(`path`,r,!1,n)),i.length>0&&o.push(y(`query`,i,!i.some(e=>e.required),n)),a.length>0&&o.push(y(`headers`,a,!a.some(e=>e.required),n)),t.body!==null&&o.push(b(t.body,n)),o.length===0?`export type ${e}Request = Readonly<Record<string, never>>;`:`export type ${e}Request = {\n${o.join(`
3
+ `)}\n`}function m(e,a,s){let c=i(a);if(c===void 0)return[`export type ${e} = ${t(a,s,0)};`];let l=c.map(e=>`${o(e)}: ${n(e)}`),u=`export const ${e} = { ${l.join(`, `)} } as const;`;return[u.length<=140?u:[`export const ${e} = {`,...l.map(e=>`${r(1)}${e},`),`} as const;`].join(`
4
+ `),`export type ${e} = (typeof ${e})[keyof typeof ${e}];`]}function h(e){return`// Generated by @webority/api-contract from ${e.name} (sha256 ${e.sha256}). Do not edit: change the backend contract and generate again.`}function g(e,t){let n=e.get(t);if(n===void 0)throw Error(`no type name was made for schema ${t}`);return n}function _(e,t){for(let n=1;;n+=1){let r=n===1?e:`${e}${n}`,i=`${r}Request`,a=`${r}Response`;if(!t.has(r)&&!t.has(i)&&!t.has(a))return t.add(r),t.add(i),t.add(a),r}}function v(e,t,n){let r=t.parameters.filter(e=>e.place===`path`),i=t.parameters.filter(e=>e.place===`query`),a=t.parameters.filter(e=>e.place===`header`),o=[];return r.length>0&&o.push(y(`path`,r,!1,n)),i.length>0&&o.push(y(`query`,i,!i.some(e=>e.required),n)),a.length>0&&o.push(y(`headers`,a,!a.some(e=>e.required),n)),t.body!==null&&o.push(b(t.body,n)),o.length===0?`export type ${e}Request = Readonly<Record<string, never>>;`:`export type ${e}Request = {\n${o.join(`
4
5
  `)}\n};`}function y(e,n,i,a){let s=n.map(e=>`${r(2)}readonly ${o(e.name)}${e.required?``:`?`}: ${t(e.schema,a,2)};`);return[`${r(1)}readonly ${e}${i?`?`:``}: {`,...s,`${r(1)}};`].join(`
5
- `)}function b(e,t){let n=x(e,t);return`${r(1)}readonly body${e.required?``:`?`}: ${n};`}function x(e,n){return f.includes(T(e.contentType))?`FormData`:e.schema===null?`unknown`:t(e.schema,n,1)}function S(e,t,n){return`export type ${e}Response = ${w(t,n)};`}var C=`undefined`;function w(e,n){let r=[];for(let i of e.replies){if(!i.status.startsWith(`2`))continue;let e=i.schema===null?C:t(i.schema,n,0);r.includes(e)||r.push(e)}return r.length>0?r.join(` | `):C}function T(e){return(e.split(`;`)[0]??``).trim().toLowerCase()}var E=`#/components/schemas/`;function D(e,n,r){let i=/* @__PURE__ */ new Set,a=e;for(;a.$ref?.startsWith(E);){let e=a.$ref.slice(21),t=r.get(e);if(t===void 0||i.has(e))return!1;i.add(e),a=t}return t(a,n,0)===d}function O(e,t,n){let r=!1,i=!1;for(let a of e.replies)a.status.startsWith(`2`)&&a.schema!==null&&(D(a.schema,t,n)?r=!0:i=!0);if(r&&i)throw new a(`${e.method} ${e.path} answers with a file and with JSON; give each its own operation`);return r?`blob`:i?`json`:`none`}function k(e,t,n){return e.length===0?`export const operations = {} as const;`:[`export const operations = {`,...e.map(({operation:e})=>{let i=O(e,t,n),a=`{ method: ${s(e.method)}, path: ${s(e.path)}, replyKind: ${s(i)} }`;return`${r(1)}${o(e.id)}: ${a},`}),`} as const;`].join(`
6
- `)}function A(e){return e.length===0?`export interface OperationTypes {}`:[`export interface OperationTypes {`,...e.map(({operation:e,op:t})=>`${r(1)}readonly ${o(e.id)}: { readonly request: ${t}Request; readonly response: ${t}Response };`),`}`].join(`
6
+ `)}function b(e,t){let n=x(e,t);return`${r(1)}readonly body${e.required?``:`?`}: ${n};`}function x(e,n){return f.includes(T(e.contentType))?`FormData`:e.schema===null?`unknown`:t(e.schema,n,1)}function S(e,t,n){return`export type ${e}Response = ${w(t,n)};`}var C=`undefined`;function w(e,n){let r=[];for(let i of e.replies){if(!i.status.startsWith(`2`))continue;let e=i.schema===null?C:t(i.schema,n,0);r.includes(e)||r.push(e)}return r.length>0?r.join(` | `):C}function T(e){return(e.split(`;`)[0]??``).trim().toLowerCase()}var E=`#/components/schemas/`;function D(e,n,r){let i=/* @__PURE__ */ new Set,a=e;for(;a.$ref?.startsWith(E);){let e=a.$ref.slice(21),t=r.get(e);if(t===void 0||i.has(e))return!1;i.add(e),a=t}return t(a,n,0)===d}function O(e,t,n){let r=!1,i=!1;for(let a of e.replies)a.status.startsWith(`2`)&&a.schema!==null&&(D(a.schema,t,n)?r=!0:i=!0);if(r&&i)throw new a(`${e.method} ${e.path} answers with a file and with JSON; give each its own operation`);return r?`blob`:i?`json`:`none`}function k(e,t,n){return e.length===0?`export const operations = {} as const;`:[`export const operations = {`,...e.flatMap(({operation:e})=>{let i=O(e,t,n),a=o(e.id),c=`${r(1)}${a}: { method: ${s(e.method)}, path: ${s(e.path)}, replyKind: ${s(i)} },`;return c.length<=140?[c]:[`${r(1)}${a}: {`,`${r(2)}method: ${s(e.method)},`,`${r(2)}path: ${s(e.path)},`,`${r(2)}replyKind: ${s(i)},`,`${r(1)}},`]}),`} as const;`].join(`
7
+ `)}function A(e){return e.length===0?`export interface OperationTypes {}`:[`export interface OperationTypes {`,...e.flatMap(({operation:e,op:t})=>{let n=o(e.id),i=`${r(1)}readonly ${n}: { readonly request: ${t}Request; readonly response: ${t}Response };`;return i.length<=140?[i]:[`${r(1)}readonly ${n}: {`,`${r(2)}readonly request: ${t}Request;`,`${r(2)}readonly response: ${t}Response;`,`${r(1)}};`]}),`}`].join(`
7
8
  `)}export{p as emitFile};
package/dist/emitHubs.js CHANGED
@@ -1,4 +1,5 @@
1
- import{emitType as e,pad as t}from"./emitSchema.js";import{propertyKey as n,quote as r,typeName as i}from"./names.js";var a=[`Method`,`Event`,`Methods`,`Events`,`Invoke`,`On`],o=/^[A-Za-z_$][A-Za-z0-9_$]*$/,s=/* @__PURE__ */ new Set(/* @__PURE__ */ `await.break.case.catch.class.const.continue.debugger.default.delete.do.else.enum.export.extends.false.finally.for.function.if.implements.import.in.instanceof.interface.let.new.null.package.private.protected.public.return.static.super.switch.this.throw.true.try.typeof.var.void.while.with.yield`.split(`.`));function c(e,t,n){let r=[],a=[...e].sort((e,t)=>b(e.name,t.name));for(let e of a){let a=l(i(e.name),n);r.push(...d(a,e,t))}return r}function l(e,t){for(let n=1;;n+=1){let r=n===1?e:`${e}${n}`,i=[r,...a.map(e=>`${r}${e}`)];if(i.every(e=>!t.has(e))){for(let e of i)t.add(e);return r}}}var u=`undefined`;function d(t,n,r){let i=y(n.methods),a=y(n.events),o=[m(t,i,a),`export type ${t}Method = keyof typeof ${t}.methods;`,`export type ${t}Event = keyof typeof ${t}.events;`,g(`${t}Methods`,i,t=>t.result===null?u:e(t.result,r,2),r),g(`${t}Events`,a,()=>u,r)];return i.length>0&&o.push(p(`export type ${t}Invoke = <K extends ${t}Method>(`,[`method: K`,`...args: ${t}Methods[K]["args"]`],`) => Promise<${t}Methods[K]["result"]>;`)),a.length>0&&o.push(p(`export type ${t}On = <K extends ${t}Event>(`,[`event: K`,`handler: (...args: ${t}Events[K]["args"]) => void`],`) => void;`)),o}var f=140;function p(e,n,r){let i=`${e}${n.join(`, `)}${r}`;if(i.length<=f)return i;let a=(n[n.length-1]??``).startsWith(`...`)?``:`,`;return[e,...n.map((e,r)=>`${t(1)}${e}${r===n.length-1?a:`,`}`),r].join(`
2
- `)}function m(e,n,r){return[`export const ${e} = {`,`${t(1)}methods: ${h(n)},`,`${t(1)}events: ${h(r)},`,`} as const;`].join(`
3
- `)}function h(e){return e.length===0?`{}`:`{ ${e.map(e=>`${n(e.name)}: ${r(e.name)}`).join(`, `)} }`}function g(e,r,i,a){if(r.length===0)return`export interface ${e} {}`;let o=r.map(e=>`${t(1)}readonly ${n(e.name)}: { readonly args: readonly [${_(e.parameters,a)}]; readonly result: ${i(e)} };`);return[`export interface ${e} {`,...o,`}`].join(`
4
- `)}function _(t,n){return t.map((t,r)=>`${v(t.name,r)}: ${e(t.schema,n,2)}`).join(`, `)}function v(e,t){return o.test(e)&&!s.has(e)?e:`arg${t}`}function y(e){return[...e].sort((e,t)=>b(e.name,t.name))}function b(e,t){return e<t?-1:+(e>t)}export{c as emitHubs};
1
+ import{emitType as e,pad as t}from"./emitSchema.js";import{propertyKey as n,quote as r,typeName as i}from"./names.js";var a=[`Method`,`Event`,`Methods`,`Events`,`Invoke`,`On`],o=/^[A-Za-z_$][A-Za-z0-9_$]*$/,s=/* @__PURE__ */ new Set(/* @__PURE__ */ `await.break.case.catch.class.const.continue.debugger.default.delete.do.else.enum.export.extends.false.finally.for.function.if.implements.import.in.instanceof.interface.let.new.null.package.private.protected.public.return.static.super.switch.this.throw.true.try.typeof.var.void.while.with.yield`.split(`.`));function c(e,t,n){let r=[],a=[...e].sort((e,t)=>b(e.name,t.name));for(let e of a){let a=l(i(e.name),n);r.push(...d(a,e,t))}return r}function l(e,t){for(let n=1;;n+=1){let r=n===1?e:`${e}${n}`,i=[r,...a.map(e=>`${r}${e}`)];if(i.every(e=>!t.has(e))){for(let e of i)t.add(e);return r}}}var u=`undefined`;function d(t,n,r){let i=y(n.methods),a=y(n.events),o=[p(t,i,a),`export type ${t}Method = keyof typeof ${t}.methods;`,`export type ${t}Event = keyof typeof ${t}.events;`,h(`${t}Methods`,i,t=>t.result===null?u:e(t.result,r,2),r),h(`${t}Events`,a,()=>u,r)];return i.length>0&&o.push(f(`export type ${t}Invoke = <K extends ${t}Method>(`,[`method: K`,`...args: ${t}Methods[K]["args"]`],`) => Promise<${t}Methods[K]["result"]>;`)),a.length>0&&o.push(f(`export type ${t}On = <K extends ${t}Event>(`,[`event: K`,`handler: (...args: ${t}Events[K]["args"]) => void`],`) => void;`)),o}function f(e,n,r){let i=`${e}${n.join(`, `)}${r}`;if(i.length<=140)return i;let a=(n[n.length-1]??``).startsWith(`...`)?``:`,`;return[e,...n.map((e,r)=>`${t(1)}${e}${r===n.length-1?a:`,`}`),r].join(`
2
+ `)}function p(e,t,n){return[`export const ${e} = {`,...m(`methods`,t),...m(`events`,n),`} as const;`].join(`
3
+ `)}function m(e,i){if(i.length===0)return[`${t(1)}${e}: {},`];let a=i.map(e=>`${n(e.name)}: ${r(e.name)}`),o=`${t(1)}${e}: { ${a.join(`, `)} },`;return o.length<=140?[o]:[`${t(1)}${e}: {`,...a.map(e=>`${t(2)}${e},`),`${t(1)}},`]}function h(e,t,r,i){if(t.length===0)return`export interface ${e} {}`;let a=t.flatMap(e=>g(n(e.name),e.parameters,r(e),i));return[`export interface ${e} {`,...a,`}`].join(`
4
+ `)}function g(e,n,r,i){let a=_(n,i,2).join(`, `),o=`${t(1)}readonly ${e}: { readonly args: readonly [${a}]; readonly result: ${r} };`;if(o.length<=140||o.includes(`
5
+ `))return[o];let s=`${t(2)}readonly args: readonly [${a}];`;if(s.length<=140)return[`${t(1)}readonly ${e}: {`,s,`${t(2)}readonly result: ${r};`,`${t(1)}};`];let c=_(n,i,3).map(e=>`${t(3)}${e},`);return[`${t(1)}readonly ${e}: {`,`${t(2)}readonly args: readonly [`,...c,`${t(2)}];`,`${t(2)}readonly result: ${r};`,`${t(1)}};`]}function _(t,n,r){return t.map((t,i)=>`${v(t.name,i)}: ${e(t.schema,n,r)}`)}function v(e,t){return o.test(e)&&!s.has(e)?e:`arg${t}`}function y(e){return[...e].sort((e,t)=>b(e.name,t.name))}function b(e,t){return e<t?-1:+(e>t)}export{c as emitHubs};
@@ -1,2 +1,3 @@
1
1
  import type { Hub } from "./model.js";
2
2
  export declare function emitHubs(hubs: readonly Hub[], schemaNames: ReadonlyMap<string, string>, taken: Set<string>): readonly string[];
3
+ export declare const LINE_WIDTH = 140;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webority/api-contract",
3
- "version": "0.163.0",
3
+ "version": "0.164.0",
4
4
  "description": "Generates one TypeScript file of API types from a backend OpenAPI 3.0, 3.1 or 3.2 contract, as plain text, with no runtime dependencies.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "module",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/node": "25.6.0",
30
- "@webority/biome-config": "0.163.0",
30
+ "@webority/biome-config": "0.164.0",
31
31
  "typescript": "^7.0.2",
32
32
  "vite": "^8.3.6",
33
33
  "vitest": "5.1.0"