express-zod-api 29.2.2 → 29.3.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/CHANGELOG.md +23 -0
- package/README.md +19 -8
- package/dist/{documentation-helpers-CQyRUQpw.js → documentation-helpers-D_9gQsph.js} +1 -1
- package/dist/documentation.d.ts +2 -2
- package/dist/documentation.js +1 -1
- package/dist/{errors-DLj5NHc7.d.ts → errors-DiaIGPcN.d.ts} +1 -1
- package/dist/index.d.ts +22 -3
- package/dist/index.js +4 -4
- package/dist/integration.d.ts +1 -1
- package/dist/integration.js +4 -4
- package/dist/{peer-helpers-Bxv2cL5q.js → peer-helpers-CpnWOg95.js} +1 -1
- package/dist/{routing-8iV6qx5U.d.ts → routing-BudY5key.d.ts} +8 -0
- package/dist/routing-walker-tYoo7Yh5.js +1 -0
- package/package.json +1 -1
- package/dist/routing-walker-DdmIClZn.js +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
## Version 29
|
|
4
4
|
|
|
5
|
+
### v29.3.0
|
|
6
|
+
|
|
7
|
+
- Featuring Endpoint-specific status codes:
|
|
8
|
+
- Added `statusCode` option to `EndpointsFactory::build()` and `Middleware::constructor()` arguments;
|
|
9
|
+
- It can be assigned with a number or an array of numbers having at least one element;
|
|
10
|
+
- You can specify how exactly an Endpoint can respond or a Middleware can terminate the request handling;
|
|
11
|
+
- Those codes affect the generated Documentation along with the `ResultHandler` (`positive` and `negative`):
|
|
12
|
+
- If `ResultHandler` has a single response schema, those codes replace the configured ones (override);
|
|
13
|
+
- When it has different response schemas, status codes would be narrowed down (intersection):
|
|
14
|
+
- Failure to intersect (uncertain response schema for unlisted code) leads to `ResultHandlerError`.
|
|
15
|
+
- Fixed: `createRateLimitMiddleware` and `EndpointsFactory::useRateLimit` respect the custom `statusCode`:
|
|
16
|
+
- Rate-limit middleware was introduced in v28.7.0; its default status code remains `429`.
|
|
17
|
+
- Rate-limit middleware, in this regard, is supposed to declare its status code:
|
|
18
|
+
- But it could be a breaking change for some APIs and therefore, it's postponed until v30;
|
|
19
|
+
- If that behavior is desired now, you can specify the `{ statusCode: 429 }` explicitly in its configuration.
|
|
20
|
+
|
|
21
|
+
### v29.2.3
|
|
22
|
+
|
|
23
|
+
- This version prevents the HTTP status code misuse within `ResultHandler` API response definition:
|
|
24
|
+
- The `ResultHandler::positive` expects status codes less than `400`;
|
|
25
|
+
- The `ResultHandler::negative` expects status codes greater than or equal to `400`;
|
|
26
|
+
- Otherwise, throws a `ResultHandlerError` when using `Documentation` or `Integration` or at startup (dev. mode).
|
|
27
|
+
|
|
5
28
|
### v29.2.2
|
|
6
29
|
|
|
7
30
|
- Fixed the bug where multiple response schemas could share the same status code in `ResultHandler` definition:
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|

|
|
6
6
|

|
|
7
|
-
[](https://coveralls.io/github/RobinTail/express-zod-api)
|
|
8
8
|
|
|
9
9
|

|
|
10
10
|

|
|
@@ -341,12 +341,12 @@ import { Middleware } from "express-zod-api";
|
|
|
341
341
|
|
|
342
342
|
const authMiddleware = new Middleware({
|
|
343
343
|
security: {
|
|
344
|
-
// this information is optional and used for generating documentation
|
|
345
344
|
and: [
|
|
346
345
|
{ type: "input", name: "key" },
|
|
347
346
|
{ type: "header", name: "token" },
|
|
348
347
|
],
|
|
349
|
-
},
|
|
348
|
+
}, // this information is optional and used for generating Documentation
|
|
349
|
+
statusCode: 401, // for Documentation: Middleware may interrupt handling this way
|
|
350
350
|
input: z.object({
|
|
351
351
|
key: z.string().min(1),
|
|
352
352
|
}),
|
|
@@ -456,6 +456,7 @@ import { auth } from "express-oauth2-jwt-bearer";
|
|
|
456
456
|
const factory = defaultEndpointsFactory.use(auth(), {
|
|
457
457
|
provider: (req) => ({ auth: req.auth }), // optional, can be async
|
|
458
458
|
transformer: (err) => createHttpError(401, err.message), // optional
|
|
459
|
+
statusCode: 401, // for Documentation: Middleware may interrupt handling this way
|
|
459
460
|
});
|
|
460
461
|
```
|
|
461
462
|
|
|
@@ -908,7 +909,11 @@ Install `express-rate-limit`. Consider the `createRateLimitMiddleware()` to enab
|
|
|
908
909
|
|
|
909
910
|
```ts
|
|
910
911
|
const endpoint = factory
|
|
911
|
-
.useRateLimit({
|
|
912
|
+
.useRateLimit({
|
|
913
|
+
windowMs: 60000,
|
|
914
|
+
max: 100,
|
|
915
|
+
statusCode: 429, // when set explicitly, it will be reflected in the Documentation
|
|
916
|
+
}) // shorthand, or .addMiddleware(createRateLimitMiddleware())
|
|
912
917
|
.buildVoid({
|
|
913
918
|
handler: async ({ ctx: { rateLimit, logger } }) => {
|
|
914
919
|
logger.debug("Features", rateLimit); // { limit, used, remaining, resetTime, getKey, resetKey }
|
|
@@ -1396,7 +1401,7 @@ response schemas and their corresponding status codes.
|
|
|
1396
1401
|
```ts
|
|
1397
1402
|
import { ResultHandler } from "express-zod-api";
|
|
1398
1403
|
|
|
1399
|
-
new ResultHandler({
|
|
1404
|
+
const resultHandler = new ResultHandler({
|
|
1400
1405
|
positive: (data) => ({
|
|
1401
1406
|
statusCode: [201, 202], // created or will be created
|
|
1402
1407
|
schema: z.object({ status: z.literal("created"), data }),
|
|
@@ -1411,9 +1416,15 @@ new ResultHandler({
|
|
|
1411
1416
|
schema: z.object({ status: z.literal("error"), reason: z.string() }),
|
|
1412
1417
|
},
|
|
1413
1418
|
],
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1419
|
+
});
|
|
1420
|
+
```
|
|
1421
|
+
|
|
1422
|
+
Moreover, when building an Endpoint, you can narrow the codes the Endpoint actually responds by using the local
|
|
1423
|
+
`statusCode` option, overriding the ones defined by the ResultHandler. That simplifies the generated Documentation:
|
|
1424
|
+
|
|
1425
|
+
```ts
|
|
1426
|
+
const endpoint = new EndpointsFactory(resultHandler).build({
|
|
1427
|
+
statusCode: [201, 409], // narrows the responses down to "created" (201) or "conflict" (409)
|
|
1417
1428
|
});
|
|
1418
1429
|
```
|
|
1419
1430
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{A as e,C as t,F as n,G as r,K as i,L as a,M as o,P as s,Q as c,U as l,W as u,Y as d,Z as f,b as p,q as m,r as h,tt as g,v as _}from"./routing-walker-DdmIClZn.js";import{z as v}from"zod";import*as y from"ramda";import{isReferenceObject as b,isSchemaObject as x}from"openapi3-ts/oas32";const S=e=>e.type===`object`,C=y.mergeDeepWith((e,t)=>{if(Array.isArray(e)&&Array.isArray(t))return y.concat(e,t);if(e===t)return t;throw Error(`Can not flatten properties`,{cause:{a:e,b:t}})}),w=new Set([`type`,`properties`,`required`,`examples`,`description`,`additionalProperties`]),T=e=>{for(let t of Object.keys(e))if(!w.has(t))return!1;return!0},E=y.pair(!0),ee=(e,t,n)=>!(`allOf`in e)||!e.allOf?[]:e.allOf.map(e=>{if(t===`throw`&&!(e.type===`object`&&T(e)))throw Error(`Can not merge`);return y.pair(n,e)}),te=e=>{let t=[];return e.anyOf&&t.push(...y.map(E,e.anyOf)),e.oneOf&&t.push(...y.map(E,e.oneOf)),t},ne=(e,t,n,i)=>{if(!r(e.propertyNames))return;let a=[];typeof e.propertyNames.const==`string`&&a.push(e.propertyNames.const),e.propertyNames.enum&&a.push(...e.propertyNames.enum.filter(e=>typeof e==`string`));let o={...Object(e.additionalProperties)};for(let e of a)t.properties[e]??=o;i||n.push(...a)},re=(e,t,n)=>{t.examples?.length&&(e.examples=n?y.concat(e.examples||[],t.examples):a(e.examples?.filter(r)||[],t.examples.filter(r),([e,t])=>y.mergeDeepRight(e,t)))},D=(e,t=`coerce`)=>{let n=[y.pair(!1,e)],r={type:`object`,properties:{}},i=[];for(let[e,a]of n)a.description&&(r.description??=a.description),n.push(...ee(a,t,e)),n.push(...te(a)),re(r,a,e),S(a)&&(n.push([e,{examples:O(a)}]),a.properties&&(r.properties=(t===`throw`?C:y.mergeDeepRight)(r.properties,a.properties),!e&&a.required&&i.push(...a.required)),ne(a,r,i,e));return i.length&&(r.required=[...new Set(i)]),r},O=e=>Object.entries(e.properties||{}).reduce((e,[t,n])=>{let{examples:i=[]}=r(n)?n:{};return a(e,i.map(y.objOf(t)),([e,t])=>({...e,...t}))},[]),k=`x-coerce`,A=(e,t)=>{if(e[`x-coerce`]===!0)return!0;if(e.anyOf)return e.anyOf.some(e=>A(e,t));if(e.oneOf)return e.oneOf.some(e=>A(e,t));if(e.allOf)return e.allOf.every(e=>A(e,t));if(e.type===void 0||e.type===`string`)return!0;if(t===`query`&&[`array`,`object`].includes(e.type)){let n=[e.items,e.prefixItems,e.additionalItems,Object.values(e.properties??{}),e.additionalProperties,e.propertyNames];for(let e of n){if(!r(e))continue;let n=Array.isArray(e)?e:[e];for(let e of n)if(r(e)&&!A(e,t))return!1}return!0}return!1};let j;const M=()=>j??=new Set(`a-im.accept.accept-additions.accept-charset.accept-datetime.accept-encoding.accept-features.accept-language.accept-signature.access-control.access-control-request-headers.access-control-request-method.alpn.alt-used.amp-cache-transform.apply-to-redirect-ref.authorization.available-dictionary.c-ext.c-man.c-opt.c-pep.c-pep-info.cache-control.cal-managed-id.caldav-timezones.capsule-protocol.cdn-loop.cert-not-after.cert-not-before.client-cert.client-cert-chain.close.cmcd-object.cmcd-request.cmcd-session.cmcd-status.cmsd-dynamic.cmsd-static.concealed-auth-export.configuration-context.connection.content-digest.content-disposition.content-encoding.content-id.content-language.content-length.content-location.content-md5.content-range.content-script-type.content-type.cookie.cookie2.cta-common-access-token.dasl.date.dav.default-style.delta-base.deprecation.depth.derived-from.destination.detached-jws.dictionary-id.differential-id.digest.dpop.early-data.ediint-features.expect.expect-ct.ext.forwarded.from.getprofile.hobareg.host.http2-settings.if.if-match.if-modified-since.if-none-match.if-range.if-schedule-tag-match.if-unmodified-since.im.include-referred-token-binding-id.incremental.isolation.keep-alive.label.last-event-id.link.link-template.lock-token.man.max-forwards.memento-datetime.meter.method-check.method-check-expires.mime-version.negotiate.odata-entityid.odata-isolation.odata-maxversion.odata-version.opt.ordering-type.origin.origin-agent-cluster.oscore.oslc-core-version.overwrite.pep.pep-info.permissions-policy.pics-label.ping-from.ping-to.position.pragma.prefer.preference-applied.priority.profileobject.protocol.protocol-info.protocol-query.protocol-request.proxy-authorization.proxy-features.proxy-instruction.range.redirect-ref.referer.referer-root.referrer-policy.repeatability-client-id.repeatability-first-sent.repeatability-request-id.repeatability-result.replay-nonce.reporting-endpoints.repr-digest.safe.schedule-reply.schedule-tag.sec-fetch-storage-access.sec-gpc.sec-purpose.sec-token-binding.sec-websocket-extensions.sec-websocket-key.sec-websocket-protocol.sec-websocket-version.security-scheme.signature.signature-input.slug.soapaction.status-uri.sunset.surrogate-capability.tcn.te.timeout.topic.traceparent.tracestate.trailer.transfer-encoding.ttl.upgrade.urgency.uri.user-agent.variant-vary.via.want-content-digest.want-digest.want-repr-digest.want-unencoded-digest.warning.x-content-type-options.x-frame-options`.split(`.`)),N={integer:0,number:0,string:``,boolean:!1,object:{},null:null,array:[]},P=e=>e.replace(d,e=>`{${e.slice(1)}}`),F=({},e)=>{if(e.isResponse)throw new t(`Please use ez.upload() only for input.`,e);return{type:`string`,format:`binary`}},I=({jsonSchema:e})=>({...e,externalDocs:{description:`raw binary data`,url:`https://swagger.io/specification/#working-with-binary-data`}}),L=({zodSchema:e,jsonSchema:t})=>{if(!i(e,`union`)||!(`discriminator`in e._zod.def))return t;let n=e._zod.def.discriminator;return{...t,discriminator:t.discriminator??{propertyName:n}}},R=y.tryCatch(({jsonSchema:e})=>{if(!e.allOf)throw`no allOf`;return D(e,`throw`)},(e,{jsonSchema:t})=>t),z=({jsonSchema:e})=>{if(!e.anyOf||!e.anyOf.length)return e;let t=e.anyOf[0];return Object.assign(t,{type:K(t.type)})},B=e=>e,V=({jsonSchema:e},n)=>{if(n.isResponse)throw new t(`Please use ez.dateOut() for output.`,n);return e},H=({jsonSchema:e},n)=>{if(!n.isResponse)throw new t(`Please use ez.dateIn() for input.`,n);return e},U=()=>({type:`string`,format:`bigint`,pattern:`^-?\\d+$`}),W=({zodSchema:e,jsonSchema:t})=>e._zod.def.rest===null?{...t,items:{not:{}}}:t,G=e=>{let t=Array.isArray(e.type)?e.type[0]:e.type;return N?.[t]},K=e=>e===`null`?e:typeof e==`string`?[e,`null`]:e&&[...new Set(e).add(`null`)],ie=({zodSchema:e,jsonSchema:t},n)=>{let r=e._zod.def[n.isResponse?`out`:`in`],a=e._zod.def[n.isResponse?`in`:`out`];if(!i(r,`transform`))return t;let o=B(Z(a,{ctx:n}));if(x(o))if(n.isResponse){let e=u(r,G(o));if(e&&[`number`,`string`,`boolean`].includes(e))return{...t,type:e}}else{let{type:e,...t}=o;return{...t,format:`${t.format||e} (preprocessed)`}}return t},q=({jsonSchema:e})=>{if(e.type!==`object`)return e;let t=e;return!t.properties||!(`raw`in t.properties)||!r(t.properties.raw)?e:t.properties.raw},J=e=>e.length?y.fromPairs(y.zip(y.times(e=>`example${e+1}`,e.length),y.map(y.objOf(`dataValue`),e))):void 0,Y=(e,t)=>t?.has(e)||e.startsWith(`x-`)||M().has(e),ae=({method:e,path:t,security:n,inputSources:r,isHeader:i})=>{let a=new Set(l(t)),o=r.includes(`query`),s=r.includes(`params`),c=r.includes(`headers`),u=r.includes(`cookies`)||r.includes(`signedCookies`),d;c&&n&&(d=h(n,`header`));let f;return u&&n&&(f=h(n,`cookie`)),{pathParams:a,getLocation:n=>{if(s&&a.has(n)&&a.delete(n))return`path`;if(u&&f?.has(n))return`cookie`;if(c&&(i?.(n,e,t)??Y(n,d)))return`header`;if(o&&e!==`query`)return`query`},isQueryEnabled:o}},oe=({path:e,method:t,flatRequest:n,makeRef:i,composition:a,getLocation:o,description:s=`${t.toUpperCase()} ${e} Parameter`})=>{let c=[];for(let[e,t]of Object.entries(n.properties)){if(!r(t))continue;let l=o(e);if(!l)continue;let u=B(t),d=a===`components`?i(t.id||JSON.stringify(t),u,t.id||m(s,e)):u;c.push({name:e,in:l,deprecated:t.deprecated,required:n.required?.includes(e)||l===`path`,description:u.description||s,schema:d,examples:J(x(u)&&u.examples?.length?u.examples:y.pluck(e,n.examples?.filter(y.both(r,y.has(e)))||[]))})}return c},X={nullable:z,union:L,bigint:U,intersection:R,tuple:W,pipe:ie,[o]:V,[e]:H,[p]:F,[_]:q,[s]:I},se=(e,t,n)=>{let r=[e,t],i=e=>/schema\d+$/.test(e)?void 0:e;for(let e=0;e<r.length;e++){let a=r[e];if(y.is(Object,a)){if(b(a)&&!a.$ref.startsWith(`#/components`)){let e=a.$ref.split(`/`).pop(),r=t[e];if(r){let t=r.id||i(e);a.$ref=n.makeRef(t||r,B(r),t).$ref}continue}r.push(...y.values(a))}y.is(Array,a)&&r.push(...y.values(a))}return e},Z=(e,{ctx:i,rules:a=X})=>{let{$defs:o={},properties:s={}}=v.toJSONSchema(v.object({subject:e}),{unrepresentable:`any`,io:i.isResponse?`output`:`input`,override:e=>{let r=v.globalRegistry.get(e.zodSchema)?.id;if(r){let n=i.seenIds.get(r);if(n&&n!==e.zodSchema)throw new t(`The meta id "${r}" is used by two different schemas. Please make the ids unique or reuse the same schema instance.`,i);i.seenIds.set(r,e.zodSchema)}let o=n(e.zodSchema),s=a[o&&o in a?o:e.zodSchema._zod.def.type];if(s){let t={...s(e,i)};for(let t in e.jsonSchema)delete e.jsonSchema[t];Object.assign(e.jsonSchema,t)}}});return se(r(s.subject)?s.subject:{},o,i)},Q=(e,t)=>{if(b(e))return[e,!1];let n=!1,r=y.map(e=>{let[r,i]=Q(e,t);return n||=i,r}),i=y.omit(t),a={properties:i,examples:y.map(i),required:y.without(t),allOf:r,oneOf:r,anyOf:r},o=y.evolve(a,e);return[o,n||!!o.required?.length]},ce=({method:e,path:t,schema:n,mimeTypes:r,variant:i,makeRef:a,composition:o,hasMultipleStatusCodes:s,statusCode:l,brandHandling:u,seenIds:d,description:p=`${e.toUpperCase()} ${t} ${c(i)} response ${s?l:``}`.trim()})=>{if(!f(e,r))return{description:p};let h=B(Z(n,{rules:{...u,...X},ctx:{isResponse:!0,makeRef:a,path:t,method:e,seenIds:d}})),_=[];x(h)&&h.examples&&(_.push(...h.examples),delete h.examples);let v=o===`components`?a(n,h,m(p)):h;return{description:p,content:y.fromPairs(r.map(e=>[e,{[e===g.sse?`itemSchema`:`schema`]:v,examples:J(_)}]))}},le=({format:e})=>{let t={type:`http`,scheme:`bearer`};return e&&(t.bearerFormat=e),t},ue=({name:e},t)=>{let n={type:`apiKey`,in:`query`,name:e};return t?.includes(`body`)&&(t?.includes(`query`)?(n[`x-in-alternative`]=`body`,n.description=`${e} CAN also be supplied within the request body`):(n[`x-in-actual`]=`body`,n.description=`${e} MUST be supplied within the request body instead of query`)),n},de=({name:e})=>({type:`apiKey`,in:`header`,name:e}),fe=({name:e})=>({type:`apiKey`,in:`cookie`,name:e}),pe=({url:e})=>({type:`openIdConnect`,openIdConnectUrl:e}),$=({flows:e={},oauth2MetadataUrl:t})=>({type:`oauth2`,flows:y.map(e=>({...e,scopes:e.scopes||{}}),y.reject(y.isNil,e)),oauth2MetadataUrl:t}),me=(e,t=[])=>{let n=e=>e.type===`basic`?{type:`http`,scheme:`basic`}:e.type===`bearer`?le(e):e.type===`input`?ue(e,t):e.type===`header`?de(e):e.type===`cookie`?fe(e):e.type===`openid`?pe(e):$(e);return e.map(e=>e.map(({deprecated:e,...t})=>({...n(t),deprecated:e})))},he=(e,t,n)=>e.map(e=>e.reduce((e,r)=>{let i=n(r),a=[`oauth2`,`openIdConnect`].includes(r.type);return Object.assign(e,{[i]:a?t:[]})},{})),ge=({schema:e,brandHandling:t,makeRef:n,path:r,method:i,seenIds:a})=>Z(e,{rules:{...t,...X},ctx:{isResponse:!1,makeRef:n,path:r,method:i,seenIds:a}}),_e=({method:e,path:t,bodyJsonSchema:n,hasRequiredBodyProps:i,flatRequest:a,mimeType:o,makeRef:s,composition:c,paramNames:l,description:u=`${e.toUpperCase()} ${t} Request body`})=>{let d=B(n),f=[];x(d)&&d.examples&&(f.push(...d.examples),delete d.examples);let p={schema:c===`components`?s(JSON.stringify(d),d,m(u)):d,examples:J(f.length?f:a.examples?.filter(e=>r(e)&&!Array.isArray(e)).map(y.omit(l))||[])},h={description:u,content:{[o]:p}};return(i||o===g.raw)&&(h.required=!0),h},ve=e=>Object.entries(e).reduce((e,[t,n])=>{if(!n)return e;if(typeof n==`string`)return e.concat({name:t,description:n});let{url:r,...i}=n,a={...i,name:t};return r&&(a.externalDocs={...a.externalDocs,url:r}),e.concat(a)},[]),ye=(e,t=50)=>!e||e.length<=t?e:e.slice(0,Math.max(1,t||0)-1)+`…`,be=e=>e.length?e.slice():void 0;export{me as a,Q as c,P as d,ye as f,A as h,ce as i,ae as l,D as m,ge as n,he as o,k as p,oe as r,ve as s,_e as t,be as u};
|
|
1
|
+
import{A as e,C as t,F as n,G as r,K as i,L as a,M as o,P as s,Q as c,U as l,W as u,Y as d,Z as f,b as p,q as m,r as h,tt as g,v as _}from"./routing-walker-tYoo7Yh5.js";import{z as v}from"zod";import*as y from"ramda";import{isReferenceObject as b,isSchemaObject as x}from"openapi3-ts/oas32";const S=e=>e.type===`object`,C=y.mergeDeepWith((e,t)=>{if(Array.isArray(e)&&Array.isArray(t))return y.concat(e,t);if(e===t)return t;throw Error(`Can not flatten properties`,{cause:{a:e,b:t}})}),w=new Set([`type`,`properties`,`required`,`examples`,`description`,`additionalProperties`]),T=e=>{for(let t of Object.keys(e))if(!w.has(t))return!1;return!0},E=y.pair(!0),ee=(e,t,n)=>!(`allOf`in e)||!e.allOf?[]:e.allOf.map(e=>{if(t===`throw`&&!(e.type===`object`&&T(e)))throw Error(`Can not merge`);return y.pair(n,e)}),te=e=>{let t=[];return e.anyOf&&t.push(...y.map(E,e.anyOf)),e.oneOf&&t.push(...y.map(E,e.oneOf)),t},ne=(e,t,n,i)=>{if(!r(e.propertyNames))return;let a=[];typeof e.propertyNames.const==`string`&&a.push(e.propertyNames.const),e.propertyNames.enum&&a.push(...e.propertyNames.enum.filter(e=>typeof e==`string`));let o={...Object(e.additionalProperties)};for(let e of a)t.properties[e]??=o;i||n.push(...a)},re=(e,t,n)=>{t.examples?.length&&(e.examples=n?y.concat(e.examples||[],t.examples):a(e.examples?.filter(r)||[],t.examples.filter(r),([e,t])=>y.mergeDeepRight(e,t)))},D=(e,t=`coerce`)=>{let n=[y.pair(!1,e)],r={type:`object`,properties:{}},i=[];for(let[e,a]of n)a.description&&(r.description??=a.description),n.push(...ee(a,t,e)),n.push(...te(a)),re(r,a,e),S(a)&&(n.push([e,{examples:O(a)}]),a.properties&&(r.properties=(t===`throw`?C:y.mergeDeepRight)(r.properties,a.properties),!e&&a.required&&i.push(...a.required)),ne(a,r,i,e));return i.length&&(r.required=[...new Set(i)]),r},O=e=>Object.entries(e.properties||{}).reduce((e,[t,n])=>{let{examples:i=[]}=r(n)?n:{};return a(e,i.map(y.objOf(t)),([e,t])=>({...e,...t}))},[]),k=`x-coerce`,A=(e,t)=>{if(e[`x-coerce`]===!0)return!0;if(e.anyOf)return e.anyOf.some(e=>A(e,t));if(e.oneOf)return e.oneOf.some(e=>A(e,t));if(e.allOf)return e.allOf.every(e=>A(e,t));if(e.type===void 0||e.type===`string`)return!0;if(t===`query`&&[`array`,`object`].includes(e.type)){let n=[e.items,e.prefixItems,e.additionalItems,Object.values(e.properties??{}),e.additionalProperties,e.propertyNames];for(let e of n){if(!r(e))continue;let n=Array.isArray(e)?e:[e];for(let e of n)if(r(e)&&!A(e,t))return!1}return!0}return!1};let j;const M=()=>j??=new Set(`a-im.accept.accept-additions.accept-charset.accept-datetime.accept-encoding.accept-features.accept-language.accept-signature.access-control.access-control-request-headers.access-control-request-method.alpn.alt-used.amp-cache-transform.apply-to-redirect-ref.authorization.available-dictionary.c-ext.c-man.c-opt.c-pep.c-pep-info.cache-control.cal-managed-id.caldav-timezones.capsule-protocol.cdn-loop.cert-not-after.cert-not-before.client-cert.client-cert-chain.close.cmcd-object.cmcd-request.cmcd-session.cmcd-status.cmsd-dynamic.cmsd-static.concealed-auth-export.configuration-context.connection.content-digest.content-disposition.content-encoding.content-id.content-language.content-length.content-location.content-md5.content-range.content-script-type.content-type.cookie.cookie2.cta-common-access-token.dasl.date.dav.default-style.delta-base.deprecation.depth.derived-from.destination.detached-jws.dictionary-id.differential-id.digest.dpop.early-data.ediint-features.expect.expect-ct.ext.forwarded.from.getprofile.hobareg.host.http2-settings.if.if-match.if-modified-since.if-none-match.if-range.if-schedule-tag-match.if-unmodified-since.im.include-referred-token-binding-id.incremental.isolation.keep-alive.label.last-event-id.link.link-template.lock-token.man.max-forwards.memento-datetime.meter.method-check.method-check-expires.mime-version.negotiate.odata-entityid.odata-isolation.odata-maxversion.odata-version.opt.ordering-type.origin.origin-agent-cluster.oscore.oslc-core-version.overwrite.pep.pep-info.permissions-policy.pics-label.ping-from.ping-to.position.pragma.prefer.preference-applied.priority.profileobject.protocol.protocol-info.protocol-query.protocol-request.proxy-authorization.proxy-features.proxy-instruction.range.redirect-ref.referer.referer-root.referrer-policy.repeatability-client-id.repeatability-first-sent.repeatability-request-id.repeatability-result.replay-nonce.reporting-endpoints.repr-digest.safe.schedule-reply.schedule-tag.sec-fetch-storage-access.sec-gpc.sec-purpose.sec-token-binding.sec-websocket-extensions.sec-websocket-key.sec-websocket-protocol.sec-websocket-version.security-scheme.signature.signature-input.slug.soapaction.status-uri.sunset.surrogate-capability.tcn.te.timeout.topic.traceparent.tracestate.trailer.transfer-encoding.ttl.upgrade.urgency.uri.user-agent.variant-vary.via.want-content-digest.want-digest.want-repr-digest.want-unencoded-digest.warning.x-content-type-options.x-frame-options`.split(`.`)),N={integer:0,number:0,string:``,boolean:!1,object:{},null:null,array:[]},P=e=>e.replace(d,e=>`{${e.slice(1)}}`),F=({},e)=>{if(e.isResponse)throw new t(`Please use ez.upload() only for input.`,e);return{type:`string`,format:`binary`}},I=({jsonSchema:e})=>({...e,externalDocs:{description:`raw binary data`,url:`https://swagger.io/specification/#working-with-binary-data`}}),L=({zodSchema:e,jsonSchema:t})=>{if(!i(e,`union`)||!(`discriminator`in e._zod.def))return t;let n=e._zod.def.discriminator;return{...t,discriminator:t.discriminator??{propertyName:n}}},R=y.tryCatch(({jsonSchema:e})=>{if(!e.allOf)throw`no allOf`;return D(e,`throw`)},(e,{jsonSchema:t})=>t),z=({jsonSchema:e})=>{if(!e.anyOf||!e.anyOf.length)return e;let t=e.anyOf[0];return Object.assign(t,{type:K(t.type)})},B=e=>e,V=({jsonSchema:e},n)=>{if(n.isResponse)throw new t(`Please use ez.dateOut() for output.`,n);return e},H=({jsonSchema:e},n)=>{if(!n.isResponse)throw new t(`Please use ez.dateIn() for input.`,n);return e},U=()=>({type:`string`,format:`bigint`,pattern:`^-?\\d+$`}),W=({zodSchema:e,jsonSchema:t})=>e._zod.def.rest===null?{...t,items:{not:{}}}:t,G=e=>{let t=Array.isArray(e.type)?e.type[0]:e.type;return N?.[t]},K=e=>e===`null`?e:typeof e==`string`?[e,`null`]:e&&[...new Set(e).add(`null`)],ie=({zodSchema:e,jsonSchema:t},n)=>{let r=e._zod.def[n.isResponse?`out`:`in`],a=e._zod.def[n.isResponse?`in`:`out`];if(!i(r,`transform`))return t;let o=B(Z(a,{ctx:n}));if(x(o))if(n.isResponse){let e=u(r,G(o));if(e&&[`number`,`string`,`boolean`].includes(e))return{...t,type:e}}else{let{type:e,...t}=o;return{...t,format:`${t.format||e} (preprocessed)`}}return t},q=({jsonSchema:e})=>{if(e.type!==`object`)return e;let t=e;return!t.properties||!(`raw`in t.properties)||!r(t.properties.raw)?e:t.properties.raw},J=e=>e.length?y.fromPairs(y.zip(y.times(e=>`example${e+1}`,e.length),y.map(y.objOf(`dataValue`),e))):void 0,Y=(e,t)=>t?.has(e)||e.startsWith(`x-`)||M().has(e),ae=({method:e,path:t,security:n,inputSources:r,isHeader:i})=>{let a=new Set(l(t)),o=r.includes(`query`),s=r.includes(`params`),c=r.includes(`headers`),u=r.includes(`cookies`)||r.includes(`signedCookies`),d;c&&n&&(d=h(n,`header`));let f;return u&&n&&(f=h(n,`cookie`)),{pathParams:a,getLocation:n=>{if(s&&a.has(n)&&a.delete(n))return`path`;if(u&&f?.has(n))return`cookie`;if(c&&(i?.(n,e,t)??Y(n,d)))return`header`;if(o&&e!==`query`)return`query`},isQueryEnabled:o}},oe=({path:e,method:t,flatRequest:n,makeRef:i,composition:a,getLocation:o,description:s=`${t.toUpperCase()} ${e} Parameter`})=>{let c=[];for(let[e,t]of Object.entries(n.properties)){if(!r(t))continue;let l=o(e);if(!l)continue;let u=B(t),d=a===`components`?i(t.id||JSON.stringify(t),u,t.id||m(s,e)):u;c.push({name:e,in:l,deprecated:t.deprecated,required:n.required?.includes(e)||l===`path`,description:u.description||s,schema:d,examples:J(x(u)&&u.examples?.length?u.examples:y.pluck(e,n.examples?.filter(y.both(r,y.has(e)))||[]))})}return c},X={nullable:z,union:L,bigint:U,intersection:R,tuple:W,pipe:ie,[o]:V,[e]:H,[p]:F,[_]:q,[s]:I},se=(e,t,n)=>{let r=[e,t],i=e=>/schema\d+$/.test(e)?void 0:e;for(let e=0;e<r.length;e++){let a=r[e];if(y.is(Object,a)){if(b(a)&&!a.$ref.startsWith(`#/components`)){let e=a.$ref.split(`/`).pop(),r=t[e];if(r){let t=r.id||i(e);a.$ref=n.makeRef(t||r,B(r),t).$ref}continue}r.push(...y.values(a))}y.is(Array,a)&&r.push(...y.values(a))}return e},Z=(e,{ctx:i,rules:a=X})=>{let{$defs:o={},properties:s={}}=v.toJSONSchema(v.object({subject:e}),{unrepresentable:`any`,io:i.isResponse?`output`:`input`,override:e=>{let r=v.globalRegistry.get(e.zodSchema)?.id;if(r){let n=i.seenIds.get(r);if(n&&n!==e.zodSchema)throw new t(`The meta id "${r}" is used by two different schemas. Please make the ids unique or reuse the same schema instance.`,i);i.seenIds.set(r,e.zodSchema)}let o=n(e.zodSchema),s=a[o&&o in a?o:e.zodSchema._zod.def.type];if(s){let t={...s(e,i)};for(let t in e.jsonSchema)delete e.jsonSchema[t];Object.assign(e.jsonSchema,t)}}});return se(r(s.subject)?s.subject:{},o,i)},Q=(e,t)=>{if(b(e))return[e,!1];let n=!1,r=y.map(e=>{let[r,i]=Q(e,t);return n||=i,r}),i=y.omit(t),a={properties:i,examples:y.map(i),required:y.without(t),allOf:r,oneOf:r,anyOf:r},o=y.evolve(a,e);return[o,n||!!o.required?.length]},ce=({method:e,path:t,schema:n,mimeTypes:r,variant:i,makeRef:a,composition:o,hasMultipleStatusCodes:s,statusCode:l,brandHandling:u,seenIds:d,description:p=`${e.toUpperCase()} ${t} ${c(i)} response ${s?l:``}`.trim()})=>{if(!f(e,r))return{description:p};let h=B(Z(n,{rules:{...u,...X},ctx:{isResponse:!0,makeRef:a,path:t,method:e,seenIds:d}})),_=[];x(h)&&h.examples&&(_.push(...h.examples),delete h.examples);let v=o===`components`?a(n,h,m(p)):h;return{description:p,content:y.fromPairs(r.map(e=>[e,{[e===g.sse?`itemSchema`:`schema`]:v,examples:J(_)}]))}},le=({format:e})=>{let t={type:`http`,scheme:`bearer`};return e&&(t.bearerFormat=e),t},ue=({name:e},t)=>{let n={type:`apiKey`,in:`query`,name:e};return t?.includes(`body`)&&(t?.includes(`query`)?(n[`x-in-alternative`]=`body`,n.description=`${e} CAN also be supplied within the request body`):(n[`x-in-actual`]=`body`,n.description=`${e} MUST be supplied within the request body instead of query`)),n},de=({name:e})=>({type:`apiKey`,in:`header`,name:e}),fe=({name:e})=>({type:`apiKey`,in:`cookie`,name:e}),pe=({url:e})=>({type:`openIdConnect`,openIdConnectUrl:e}),$=({flows:e={},oauth2MetadataUrl:t})=>({type:`oauth2`,flows:y.map(e=>({...e,scopes:e.scopes||{}}),y.reject(y.isNil,e)),oauth2MetadataUrl:t}),me=(e,t=[])=>{let n=e=>e.type===`basic`?{type:`http`,scheme:`basic`}:e.type===`bearer`?le(e):e.type===`input`?ue(e,t):e.type===`header`?de(e):e.type===`cookie`?fe(e):e.type===`openid`?pe(e):$(e);return e.map(e=>e.map(({deprecated:e,...t})=>({...n(t),deprecated:e})))},he=(e,t,n)=>e.map(e=>e.reduce((e,r)=>{let i=n(r),a=[`oauth2`,`openIdConnect`].includes(r.type);return Object.assign(e,{[i]:a?t:[]})},{})),ge=({schema:e,brandHandling:t,makeRef:n,path:r,method:i,seenIds:a})=>Z(e,{rules:{...t,...X},ctx:{isResponse:!1,makeRef:n,path:r,method:i,seenIds:a}}),_e=({method:e,path:t,bodyJsonSchema:n,hasRequiredBodyProps:i,flatRequest:a,mimeType:o,makeRef:s,composition:c,paramNames:l,description:u=`${e.toUpperCase()} ${t} Request body`})=>{let d=B(n),f=[];x(d)&&d.examples&&(f.push(...d.examples),delete d.examples);let p={schema:c===`components`?s(JSON.stringify(d),d,m(u)):d,examples:J(f.length?f:a.examples?.filter(e=>r(e)&&!Array.isArray(e)).map(y.omit(l))||[])},h={description:u,content:{[o]:p}};return(i||o===g.raw)&&(h.required=!0),h},ve=e=>Object.entries(e).reduce((e,[t,n])=>{if(!n)return e;if(typeof n==`string`)return e.concat({name:t,description:n});let{url:r,...i}=n,a={...i,name:t};return r&&(a.externalDocs={...a.externalDocs,url:r}),e.concat(a)},[]),ye=(e,t=50)=>!e||e.length<=t?e:e.slice(0,Math.max(1,t||0)-1)+`…`,be=e=>e.length?e.slice():void 0;export{me as a,Q as c,P as d,ye as f,A as h,ce as i,ae as l,D as m,ge as n,he as o,k as p,oe as r,ve as s,_e as t,be as u};
|
package/dist/documentation.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { M as ResponseVariant, g as CommonConfig, t as Routing } from "./routing-
|
|
1
|
+
import { M as ResponseVariant, g as CommonConfig, t as Routing } from "./routing-BudY5key.js";
|
|
2
2
|
import {
|
|
3
3
|
c as IsHeader,
|
|
4
4
|
l as depictTags,
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
s as Depicter,
|
|
7
7
|
t as DocumentationError,
|
|
8
8
|
u as trimSummary,
|
|
9
|
-
} from "./errors-
|
|
9
|
+
} from "./errors-DiaIGPcN.js";
|
|
10
10
|
import { InfoObject, OpenApiBuilder, ServerObject } from "openapi3-ts/oas32";
|
|
11
11
|
type Component = `${ResponseVariant}Response` | "requestParameter" | "requestBody";
|
|
12
12
|
/** @desc user defined function that creates a component description from its properties */
|
package/dist/documentation.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{C as e,J as t,V as n,
|
|
1
|
+
import{C as e,J as t,V as n,i as r,n as i,p as a,q as o,t as s,tt as c}from"./routing-walker-tYoo7Yh5.js";import{a as l,c as u,d,f,i as p,l as m,m as h,n as g,o as _,r as v,s as y,t as b,u as x}from"./documentation-helpers-D_9gQsph.js";import"zod";import*as S from"ramda";import{OpenApiBuilder as C}from"openapi3-ts/oas32";const w=({description:e,summary:t=e,trim:n})=>n(t);var T=class extends C{#e=new Map;#t=new Map;#n=new Map;#r=new Map;#i(e,t,n){let r=this.#n.get(e);if(!r){let t=+!n;do r=`${n??`Schema`}${t?this.#n.size+t:``}`,t++;while(this.rootDoc.components?.schemas?.[r]);this.#n.set(e,r)}return this.addSchema(r,t),{$ref:`#/components/schemas/${r}`}}#a(t,n,r){let i=r||o(n,t),a=this.#t.get(i);if(a===void 0)return this.#t.set(i,1),i;if(r)throw new e(`Duplicated operationId: "${r}"`,{method:n,isResponse:!1,path:t});return a++,this.#t.set(i,a),`${i}${a}`}#o(e){let t=JSON.stringify(e);for(let e in this.rootDoc.components?.securitySchemes||{})if(t===JSON.stringify(this.rootDoc.components?.securitySchemes?.[e]))return e;let n=(this.#e.get(e.type)||0)+1;return this.#e.set(e.type,n),`${e.type.toUpperCase()}_${n}`}#s({tags:e,server:t,info:n={title:`Generated by Express Zod API`,version:`0.0.0`}}){if(this.addInfo(n),e&&(this.rootDoc.tags=y(e)),t)for(let e of Array.isArray(t)?t:[t])this.addServer(typeof e==`string`?{url:e}:e)}#c(n,r){if(n===`head`||!r.includes(`:`))return;let i=t(r),a=this.#r.get(i);if(a!==void 0&&a!==r)throw new e(`Path has a duplicate: the normalized path "${i}" is already registered with different parameter names at "${a}"`,{method:n,path:r,isResponse:!1});a===void 0&&this.#r.set(i,r)}#l({config:t,descriptions:i,brandHandling:o,isHeader:s,summarizer:y=w,composition:C=`inline`}){let T={composition:C,brandHandling:o,makeRef:this.#i.bind(this),seenIds:new Map};return(o,C,w)=>{this.#c(o,C);let E={...T,path:C,method:o,endpoint:w},{description:D,summary:O,scopes:k,inputSchema:A,security:j}=w,M=n(o,t.inputSources),{pathParams:N,getLocation:P}=m({method:o,path:C,security:j,inputSources:M,isHeader:s}),F=this.#a(C,o,w.getOperationId(o)),I=g({...E,schema:A}),L=h(I),R=v({...E,getLocation:P,flatRequest:L,description:i?.requestParameter?.({method:o,path:C,operationId:F})});if(N.size)throw new e(`The input schema is missing the path parameter "${[...N][0]}"`,{method:o,path:C,isResponse:!1});let z={};for(let e of a){let t=w.getResponses(e);for(let{mimeTypes:n,schema:r,statusCodes:a}of t)for(let s of a)z[s]=p({...E,variant:e,schema:r,mimeTypes:n,statusCode:s,hasMultipleStatusCodes:t.length>1||a.length>1,description:i?.[`${e}Response`]?.({method:o,path:C,operationId:F,statusCode:s})})}let B;if(M.includes(`body`)){let e=S.pluck(`name`,R),[t,n]=u(I,e);B=b({...E,bodyJsonSchema:t,hasRequiredBodyProps:n,flatRequest:L,paramNames:e,mimeType:c[w.getProbableRequestType(o)],description:i?.requestBody?.({method:o,path:C,operationId:F})})}let V=_(l(r(j),M),k,e=>{let t=this.#o(e);return this.addSecurityScheme(t,e),t}),H={operationId:F,summary:y({summary:O,description:D,trim:f}),description:D,deprecated:w.isDeprecated||void 0,tags:x(w.tags),parameters:x(R),requestBody:B,security:x(V),responses:z};this.addPath(d(C),{[o]:H})}}constructor({hasHeadMethod:e=!0,...t}){super(),this.#s(t);let n=this.#l(t),r=e?i(n):n;s({...t,onEndpoint:r})}};export{T as Documentation,e as DocumentationError};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { D as ClientMethod, O as Method, f as Tag } from "./routing-
|
|
1
|
+
import { D as ClientMethod, O as Method, f as Tag } from "./routing-BudY5key.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { ReferenceObject, SchemaObjectValue, TagObject } from "openapi3-ts/oas32";
|
|
4
4
|
interface ReqResCommons {
|
package/dist/index.d.ts
CHANGED
|
@@ -31,14 +31,14 @@ import {
|
|
|
31
31
|
w as Extension,
|
|
32
32
|
x as arrayResultHandler,
|
|
33
33
|
y as AbstractResultHandler,
|
|
34
|
-
} from "./routing-
|
|
34
|
+
} from "./routing-BudY5key.js";
|
|
35
35
|
import {
|
|
36
36
|
a as RoutingError,
|
|
37
37
|
i as OutputValidationError,
|
|
38
38
|
n as InputValidationError,
|
|
39
39
|
r as MissingPeerError,
|
|
40
40
|
t as DocumentationError,
|
|
41
|
-
} from "./errors-
|
|
41
|
+
} from "./errors-DiaIGPcN.js";
|
|
42
42
|
import { z } from "zod";
|
|
43
43
|
import express, { CookieOptions, Request, Response } from "express";
|
|
44
44
|
import http from "node:http";
|
|
@@ -245,7 +245,16 @@ declare const createCookieMiddleware: (baseOptions?: CookieOptions) => Middlewar
|
|
|
245
245
|
* @param options — Partial options passed to the express-rate-limit constructor.
|
|
246
246
|
* @example createRateLimitMiddleware({ windowMs: 60000, max: 100 })
|
|
247
247
|
*/
|
|
248
|
-
declare const createRateLimitMiddleware: (
|
|
248
|
+
declare const createRateLimitMiddleware: (
|
|
249
|
+
options?: Partial<Options> & {
|
|
250
|
+
/**
|
|
251
|
+
* @desc The HTTP status code to send back when a client is rate-limited.
|
|
252
|
+
* @default 429
|
|
253
|
+
* @modifies ResultHandler.negative.statusCode — overrides when specified explicitly (opt-in, no breaking changes).
|
|
254
|
+
*/
|
|
255
|
+
statusCode?: number;
|
|
256
|
+
},
|
|
257
|
+
) => ExpressMiddleware<
|
|
249
258
|
AugmentedRequest,
|
|
250
259
|
import("express-serve-static-core").Response<any, Record<string, any>, number>,
|
|
251
260
|
{
|
|
@@ -290,6 +299,13 @@ interface BuildProps<
|
|
|
290
299
|
* @see TagOverrides
|
|
291
300
|
* */
|
|
292
301
|
tag?: Tag | Tag[];
|
|
302
|
+
/**
|
|
303
|
+
* @desc The status code(s) specific to the Endpoint, overriding the ones configured by the ResultHandler.
|
|
304
|
+
* Narrows the response schemas of ResultHandler by status codes in the generated Documentation.
|
|
305
|
+
* @example [201, 403] — the Endpoint narrows the ResultHandler responses to either "Created" or "Forbidden".
|
|
306
|
+
* @see ApiResponse#statusCode - the status codes used by ResultHandler
|
|
307
|
+
* */
|
|
308
|
+
statusCode?: number | [number, ...number[]];
|
|
293
309
|
/** @desc Marks the operation deprecated in the generated Documentation */
|
|
294
310
|
deprecated?: boolean;
|
|
295
311
|
}
|
|
@@ -306,6 +322,7 @@ declare class EndpointsFactory<
|
|
|
306
322
|
> {
|
|
307
323
|
protected resultHandler: AbstractResultHandler;
|
|
308
324
|
protected schema: IN;
|
|
325
|
+
protected statusCodes: Set<number>;
|
|
309
326
|
protected middlewares: AbstractMiddleware[];
|
|
310
327
|
/**
|
|
311
328
|
* @param resultHandler An instance of ResultHandler for handling both Endpoint outputs and all possible errors.
|
|
@@ -398,6 +415,7 @@ declare class EndpointsFactory<
|
|
|
398
415
|
| {
|
|
399
416
|
provider?: ((request: R, response: S) => AOUT | Promise<AOUT>) | undefined;
|
|
400
417
|
transformer?: (err: Error) => Error;
|
|
418
|
+
statusCode?: number | [number, ...number[]];
|
|
401
419
|
}
|
|
402
420
|
| undefined,
|
|
403
421
|
) => EndpointsFactory<Extension<IN, undefined>, (CTX extends Record<string, never> ? AOUT : CTX) & AOUT, SCO>;
|
|
@@ -430,6 +448,7 @@ declare class EndpointsFactory<
|
|
|
430
448
|
scope,
|
|
431
449
|
tag,
|
|
432
450
|
method,
|
|
451
|
+
statusCode,
|
|
433
452
|
...rest
|
|
434
453
|
}: BuildProps<BIN, BOUT, IN, CTX, SCO>): Endpoint<FinalInputSchema<IN, BIN>, BOUT, CTX>;
|
|
435
454
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import{B as e,C as t,D as n,E as r,G as i,H as a,I as o,N as s,O as c,R as l,S as u,T as ee,V as d,X as f,a as te,c as ne,d as p,et as m,f as h,g,h as _,j as re,k as ie,l as ae,m as v,o as oe,p as y,s as b,t as se,tt as x,u as S,w as ce,x as le,y as ue,z as C}from"./routing-walker-DdmIClZn.js";import{t as w}from"./peer-helpers-Bxv2cL5q.js";import{h as de,l as fe,m as pe,p as me}from"./documentation-helpers-CQyRUQpw.js";import{globalRegistry as T,z as E}from"zod";import*as he from"ramda";import D,{isHttpError as ge}from"http-errors";import _e,{blue as ve,cyanBright as ye,green as be,hex as O,italic as xe,red as Se,whiteBright as k}from"ansis";import{inspect as Ce}from"node:util";import{performance as A}from"node:perf_hooks";import j from"express";import we from"node:http";import Te from"node:https";import{setInterval as Ee}from"node:timers/promises";import{createRequest as De,createResponse as Oe}from"node-mocks-http";function ke(e){return e}const Ae=(e,t)=>e&&t?e.and(t):e||t,je=(e,t)=>e?e.and(t):t;var Me=class e{#e;constructor(e){this.#e=e}async execute(...t){try{return await this.#e(...t)}catch(r){let{response:i,logger:a,error:o}=t[0],s=new n(C(r),o||void 0);e.lastResort({response:i,logger:a,error:s})}}static lastResort({error:e,logger:t,response:n}){t.error(`Result handler failure`,e);let r=p(D(500,`An error occurred while serving the result: ${e.message}.`+(e.handled?`\nOriginal error: ${e.handled.message}.`:``),{expose:ge(e.cause)?e.cause.expose:!1}));n.status(500).type(`text/plain`).end(r)}},M=class extends Me{#e;#t;constructor(e){super(e.handler),this.#e=e.positive,this.#t=e.negative}getPositiveResponse(e){return y(this.#e,{variant:`positive`,args:[e],statusCodes:[b.positive],mimeTypes:[x.json]})}getNegativeResponse(){return y(this.#t,{variant:`negative`,args:[],statusCodes:[b.negative],mimeTypes:[x.json]})}};const N=E.object({status:E.literal(`error`),error:E.object({message:E.string()})});T.add(N,{examples:[{status:`error`,error:{message:`Sample error message`}}]});const P=new M({positive:e=>{let t=E.object({status:E.literal(`success`),data:e}),n=o(e);return n.length&&T.add(t,{examples:n.map(e=>({status:`success`,data:e}))}),t},negative:N,handler:({error:e,input:t,output:n,request:r,response:i,logger:a})=>{if(e){let n=S(e);h(n,a,r,t),i.status(n.statusCode).set(n.headers).json({status:`error`,error:{message:p(n)}});return}i.status(b.positive).json({status:`success`,data:n})}}),F=E.string();T.add(F,{examples:[`Sample error message`]});const I=new M({positive:e=>{let t=e instanceof E.ZodObject&&`items`in e.shape&&e.shape.items instanceof E.ZodArray?e.shape.items:E.array(E.any());if(o(t).length)return t;let n=o(e).filter(e=>i(e)&&`items`in e&&Array.isArray(e.items)).map(e=>e.items);if(n?.length){let e=t.meta();T.remove(t).add(t,{...e,examples:n})}return t},negative:{schema:F,mimeType:`text/plain`},handler:({response:e,output:t,error:n,logger:r,request:i,input:a})=>{if(n){let t=S(n);h(t,r,i,a),e.status(t.statusCode).type(`text/plain`).send(p(t));return}if(`items`in t&&Array.isArray(t.items)){e.status(b.positive).json(t.items);return}throw Error(`Property 'items' is missing in the endpoint output`)}}),L=e=>{let t=[];return e.scope&&t.push(e.scope),e.noStore&&t.push(`no-store`),e.noCache&&t.push(`no-cache`),e.maxAge!==void 0&&t.push(`max-age=${e.maxAge}`),e.sMaxAge!==void 0&&t.push(`s-maxage=${e.sMaxAge}`),e.mustRevalidate&&t.push(`must-revalidate`),e.proxyRevalidate&&t.push(`proxy-revalidate`),e.mustUnderstand&&t.push(`must-understand`),e.immutable&&t.push(`immutable`),e.noTransform&&t.push(`no-transform`),e.staleWhileRevalidate!==void 0&&t.push(`stale-while-revalidate=${e.staleWhileRevalidate}`),e.staleIfError!==void 0&&t.push(`stale-if-error=${e.staleIfError}`),t.join(`, `)},Ne={"max-age":`maxAge`,"max-stale":`maxStale`,"min-fresh":`minFresh`,"stale-if-error":`staleIfError`},Pe={"no-cache":`noCache`,"no-store":`noStore`,"no-transform":`noTransform`,"only-if-cached":`onlyIfCached`},Fe=e=>{if(!e)return;let t={};for(let n of e.toLowerCase().split(`,`)){let[e,r]=n.split(`=`),i=Ne[e.trim()];if(i){let e=parseInt(r?.trim()??``,10);isNaN(e)||(t[i]=e);continue}let a=Pe[e.trim()];a&&(t[a]=!0)}return t},R=e=>new _({handler:async({request:t,response:n})=>(e&&n.setHeader(`Cache-Control`,L(e)),{get ifNoneMatch(){let e=t.headers[`if-none-match`];if(!e)return;let n=e.trim();return n===`*`?n:n.split(`,`).map(e=>e.trim().replace(/^(?:W\/)?"/,``).replace(/"$/,``))},get ifModifiedSince(){let e=t.headers[`if-modified-since`];if(!e)return;let n=new Date(e);return isNaN(n.getTime())?void 0:n},get cacheControl(){return Fe(t.headers[`cache-control`])},addCachePolicy:t=>{n.setHeader(`Cache-Control`,L({...e,...t}))},setETag:e=>{n.setHeader(`ETag`,e.startsWith(`"`)?e:`"${e}"`)},setLastModified:e=>{n.setHeader(`Last-Modified`,e.toUTCString())},setVary:(...e)=>{n.setHeader(`Vary`,e.join(`, `))},setExpires:e=>{n.setHeader(`Expires`,e.toUTCString())},clearSiteData:()=>{n.setHeader(`Clear-Site-Data`,`"cache"`)},notModified:()=>{n.status(304).end()}})}),z=e=>new _({handler:async({request:t,response:n})=>({getCookie:e=>t.signedCookies?.[e]??t.cookies?.[e],setCookie:(t,r,i)=>{n.cookie(t,r,{...e,...i})},clearCookie:(t,r)=>{n.clearCookie(t,{...e,...r})}})}),B=e=>{let t=w(`express-rate-limit`)({...e,handler:(e,t,n,r)=>{n(D(429,r.message))}}),{getKey:n,resetKey:r}=t,i={getKey:n,resetKey:r};return new v(t,{provider:t=>({rateLimit:{...i,...t[e?.requestPropertyName??`rateLimit`]}})})};var V=class e{resultHandler;schema=void 0;middlewares=[];constructor(e){this.resultHandler=e}#e(t){let n=new e(this.resultHandler);return n.middlewares=this.middlewares.concat(t),n.schema=Ae(this.schema,t.schema),n}addMiddleware(e){return this.#e(e instanceof _?e:new _(e))}useCookies(...e){return this.#e(z(...e))}useCache(...e){return this.#e(R(...e))}useRateLimit(...e){return this.#e(B(...e))}use=this.addExpressMiddleware;addExpressMiddleware(...e){return this.#e(new v(...e))}addContext(e){return this.#e(new _({handler:({ctx:t})=>e(t)}))}build({input:e=l,output:t,operationId:n,scope:r,tag:i,method:a,...o}){let{middlewares:s,resultHandler:c}=this,u=typeof a==`string`?[a]:a,ee=typeof n==`function`?n:e=>n&&`${n}${e===`head`?`__HEAD`:``}`,d=typeof r==`string`?[r]:r||[],f=typeof i==`string`?[i]:i||[];return new ae({...o,middlewares:s,outputSchema:t,resultHandler:c,scopes:d,tags:f,methods:u,getOperationId:ee,inputSchema:je(this.schema,e)})}buildVoid({handler:e,...t}){return this.build({...t,output:l,handler:async t=>(await e(t),{})})}};const Ie=new V(P),Le=new V(I),H={debug:ve,info:be,warn:O(`#FFA500`),error:Se,ctx:ye},U={debug:10,info:20,warn:30,error:40},Re=e=>i(e)&&Object.keys(U).some(t=>t in e),ze=e=>e in U,Be=(e,t)=>U[e]<U[t],W=he.memoizeWith((e,t)=>`${e}${t}`,(e,t=0)=>Intl.NumberFormat(void 0,{useGrouping:!1,minimumFractionDigits:0,maximumFractionDigits:t,style:`unit`,unitDisplay:`long`,unit:e})),Ve=e=>e<1e-6?W(`nanosecond`,3).format(e/1e-6):e<.001?W(`nanosecond`).format(e/1e-6):e<1?W(`microsecond`).format(e/.001):e<1e3?W(`millisecond`).format(e):e<6e4?W(`second`,2).format(e/1e3):W(`minute`,2).format(e/6e4);var G=class e{config;constructor({color:e=_e.isSupported(),level:t=f.isProduction?`warn`:`debug`,depth:n=2,ctx:r={}}={}){this.config={color:e,level:t,depth:n,ctx:r}}format(e){let{depth:t,color:n,level:r}=this.config;return Ce(e,{depth:t,colors:n,breakLength:r===`debug`?80:1/0,compact:r!==`debug`||3})}print(e,t,n){let{level:r,ctx:{requestId:i,...a},color:o}=this.config;if(r===`silent`||Be(e,r))return;let s=[new Date().toISOString()];i&&s.push(o?H.ctx(i):i),s.push(o?`${H[e](e)}:`:`${e}:`,t),n!==void 0&&s.push(this.format(n)),Object.keys(a).length>0&&s.push(this.format(a)),console.log(s.join(` `))}debug(e,t){this.print(`debug`,e,t)}info(e,t){this.print(`info`,e,t)}warn(e,t){this.print(`warn`,e,t)}error(e,t){this.print(`error`,e,t)}child(t){return new e({...this.config,ctx:t})}get ctx(){return this.config.ctx}profile(e){let t=A.now();return()=>{let n=A.now()-t,{message:r,severity:i=`debug`,formatter:a=Ve}=typeof e==`object`?e:{message:e};this.print(typeof i==`function`?i(n):i,r,a(n))}}},He=class{logger;config;#e=new WeakMap;constructor(e,t){this.logger=e,this.config=t}#t(e,t,n){if(!e.isSchemaChecked){for(let e of[`input`,`output`]){let r=[E.toJSONSchema(t[`${e}Schema`],{unrepresentable:`any`})];for(let t of r){t.type&&t.type!==`object`&&this.logger.warn(`Endpoint ${e} schema is not object-based`,n);for(let e of[`allOf`,`oneOf`,`anyOf`])t[e]&&r.push(...t[e])}}if(t.getProbableRequestType()===`json`){let e=g(t.inputSchema,`input`);e&&this.logger.warn(`The final input schema of the endpoint contains an unsupported JSON payload type.`,{...n,reason:e})}for(let e of ne)for(let{mimeTypes:r,schema:i}of t.getResponses(e)){if(!r?.includes(x.json))continue;let t=g(i,`output`);t&&this.logger.warn(`The final ${e} response schema of the endpoint contains an unsupported JSON payload type.`,{...n,reason:t})}e.isSchemaChecked=!0}}#n(e,t,n,r,a){if(e.paths.has(r))return;let{pathParams:o,getLocation:s,isQueryEnabled:c}=fe({method:n,path:r,security:t.security,inputSources:d(n,this.config.inputSources)});if(!(o.size===0&&!c)){e.flat??=pe(E.toJSONSchema(t.inputSchema,{unrepresentable:`any`,io:`input`,override:({zodSchema:e,jsonSchema:t})=>{(e._zod.traits.has(`$ZodPreprocess`)||`coerce`in e._zod.def&&e._zod.def.coerce)&&(t[me]=!0)}}));for(let[t,n]of Object.entries(e.flat.properties)){if(!i(n))continue;let r=s(t);(r===`path`||r===`query`)&&(r===`path`&&!e.flat.required?.includes(t)&&this.logger.warn(`The path parameter "${t}" is declared optional in the input schema, but path parameters are always required since Express matches the route only when the segment is present.`,{...a,name:t}),!de(n,r)&&this.logger.warn(`The ${r} parameter "${t}" has a schema that most likely would not accept the parsed data, ${r===`path`?`since path parameters always arrive as strings`:`depending on the "queryParser" config option`}. Convert the parsed value from "z.string()" using ".transform()" method, or use "z.coerce" at least.`,{...a,name:t,jsonSchema:n}))}for(let e of o)this.logger.warn(`The input schema of the endpoint is most likely missing the parameter of the path it's assigned to.`,{...a,param:e});e.paths.add(r)}}check=(e,t,n)=>{let r=this.#e.get(n);r||(r={isSchemaChecked:!1,paths:new Set},this.#e.set(n,r)),this.#t(r,n,{method:e,path:t}),this.#n(r,n,e,t,{method:e,path:t})}};const K=e=>e.sort((e,t)=>m(t)-+m(e)||e.localeCompare(t)).join(`, `).toUpperCase(),Ue=e=>({method:t},n,r)=>{let i=K(e);n.set({Allow:i}),r(D(405,`${t} is not allowed`,{headers:{Allow:i}}))},We=({app:e,getLogger:t,config:n,routing:r})=>{let i=f.isProduction?void 0:new He(t(),n),a=new Map;return se({routing:r,config:n,onEndpoint:(e,t,r)=>{i?.check(e,t,r),a.has(t)||a.set(t,new Map(n.cors?[[`options`,r]]:[])),a.get(t)?.set(e,r)},onStatic:e.use.bind(e)}),a},q=({app:e,config:t,getLogger:n,...r})=>{let i=We({app:e,getLogger:n,config:t,...r}),a=new Map;for(let[r,o]of i){let i=Array.from(o.keys());i.includes(`get`)&&i.push(`head`);for(let[a,s]of o){let o=[];t.cors&&o.push((e,t,n)=>{t.set(`Access-Control-Allow-Methods`,K(i)),n()}),o.push(async(e,r)=>{let i=n(e);return s.execute({request:e,response:r,logger:i,config:t})}),e[a]?.(r,...o)}t.hintAllowedMethods!==!1&&a.set(r,Ue(i))}for(let[t,n]of a)e.all(t,n)},Ge=e=>`_httpMessage`in e&&typeof e._httpMessage==`object`&&e._httpMessage!==null&&`headersSent`in e._httpMessage&&typeof e._httpMessage.headersSent==`boolean`&&`setHeader`in e._httpMessage&&typeof e._httpMessage.setHeader==`function`,Ke=e=>`server`in e&&typeof e.server==`object`&&e.server!==null&&`close`in e.server&&typeof e.server.close==`function`,qe=e=>`encrypted`in e&&typeof e.encrypted==`boolean`&&e.encrypted,Je=({},e)=>void(!e.headersSent&&e.setHeader(`connection`,`close`)),Ye=e=>{let{promise:t,resolve:n,reject:r}=Promise.withResolvers();return e.close(e=>e?r(e):n()),t},Xe=({timeout:e=1e3,logger:t}={})=>{let n,r=new Set,i=new Set,a=e=>void i.delete(e),o=e=>a(e.destroy()),s=e=>void(Ge(e)?!e._httpMessage.headersSent&&e._httpMessage.setHeader(`connection`,`close`):o(e)),c=e=>void(n?e.destroy():i.add(e.once(`close`,()=>a(e)).once(`error`,()=>o(e)))),l=async()=>{for(let e of r)e.on(`request`,Je);t?.info(`Graceful shutdown`,{sockets:i.size,timeout:e});for(let e of i)(qe(e)||Ke(e))&&s(e);for await(let t of Ee(10,Date.now()))if(i.size===0||Date.now()-t>=e)break;for(let e of i)o(e);return Promise.allSettled(r.values().map(Ye))},u={sockets:i,add:(...e)=>{for(let t of e)if(!r.has(t)){r.add(t);for(let e of[`connection`,`secureConnection`])t.on(e,c)}return u},shutdown:()=>n??=l(),get isShuttingDown(){return!!n}};return u},J=Symbol.for(`express-zod-api`),Ze=({errorHandler:e,getLogger:t})=>async(n,r,i,a)=>n?e.execute({error:C(n),request:r,response:i,input:null,output:null,ctx:{},logger:t(r)}):a(),Qe=({errorHandler:e,getLogger:t})=>async(n,r)=>{let i=D(404,`Can not ${n.method} ${n.path}`),a=t(n);await e.execute({request:n,response:r,logger:a,error:i,input:null,output:null,ctx:{}})},$e=e=>(t,{},n)=>{if(Object.values(t?.files||[]).flat().find(({truncated:e})=>e))return n(e);n()},et=({config:e})=>{let t=w(`cookie-parser`),{secret:n,...r}={...typeof e.cookies==`object`&&e.cookies};return t(n,Object.keys(r).length?r:void 0)},tt=e=>typeof e==`function`?e:({},e,t)=>{e.set({"Access-Control-Allow-Origin":`*`,"Access-Control-Allow-Headers":`content-type`}),t()},nt=e=>({log:t=>{/not eligible/i.test(t)||e.debug(t)}}),rt=({getLogger:e,config:t})=>{let n=w(`express-fileupload`),{limitError:r,beforeUpload:i,...a}={...typeof t.upload==`object`&&t.upload},o=[];return o.push(async(t,r,o)=>{let s=e(t);return await i?.({request:t,logger:s}),n({debug:!0,...a,abortOnLimit:!1,parseNested:!0,logger:nt(s)})(t,r,o)}),r&&o.push($e(r)),o},it=(e,{},t)=>{Buffer.isBuffer(e.body)&&(e.body={raw:e.body}),t()},at=({logger:e,config:{childLoggerProvider:t,accessLogger:n=({method:e,path:t},n)=>n.debug(`${e}: ${t}`)}})=>async(r,i,a)=>{let o=await t?.({request:r,parent:e})||e;n?.(r,o),r.res&&(r.res.locals[J]={logger:o}),a()},ot=e=>t=>t?.res?.locals[J]?.logger||e;let st=!1;const ct=e=>{st||(st=!0,process.on(`deprecation`,({message:t,namespace:n,name:r,stack:i})=>e.warn(`${r} (${n}): ${t}`,i.split(`
|
|
2
|
-
`).slice(1))))};let
|
|
1
|
+
import{B as e,C as t,D as n,E as r,G as i,H as a,I as o,N as s,O as c,R as l,S as u,T as d,V as f,X as p,a as m,c as h,d as ee,et as te,f as g,g as _,h as v,j as ne,k as re,l as y,m as b,o as ie,p as ae,s as x,t as oe,tt as S,u as se,w as ce,x as le,y as ue,z as C}from"./routing-walker-tYoo7Yh5.js";import{t as w}from"./peer-helpers-CpnWOg95.js";import{h as de,l as fe,m as pe,p as me}from"./documentation-helpers-D_9gQsph.js";import{globalRegistry as T,z as E}from"zod";import*as he from"ramda";import D,{isHttpError as ge}from"http-errors";import _e,{blue as ve,cyanBright as ye,green as be,hex as O,italic as xe,red as Se,whiteBright as k}from"ansis";import{inspect as Ce}from"node:util";import{performance as A}from"node:perf_hooks";import j from"express";import we from"node:http";import Te from"node:https";import{setInterval as Ee}from"node:timers/promises";import{createRequest as De,createResponse as Oe}from"node-mocks-http";function ke(e){return e}const Ae=(e,t)=>e&&t?e.and(t):e||t,je=(e,t)=>e?e.and(t):t;var Me=class e{#e;constructor(e){this.#e=e}async execute(...t){try{return await this.#e(...t)}catch(r){let{response:i,logger:a,error:o}=t[0],s=new n(C(r),o||void 0);e.lastResort({response:i,logger:a,error:s})}}static lastResort({error:e,logger:t,response:n}){t.error(`Result handler failure`,e);let r=h(D(500,`An error occurred while serving the result: ${e.message}.`+(e.handled?`\nOriginal error: ${e.handled.message}.`:``),{expose:ge(e.cause)?e.cause.expose:!1}));n.status(500).type(`text/plain`).end(r)}},M=class extends Me{#e;#t;constructor(e){super(e.handler),this.#e=e.positive,this.#t=e.negative}getPositiveResponse(e){return se(this.#e,{variant:`positive`,args:[e]})}getNegativeResponse(){return se(this.#t,{variant:`negative`,args:[]})}};const N=E.object({status:E.literal(`error`),error:E.object({message:E.string()})});T.add(N,{examples:[{status:`error`,error:{message:`Sample error message`}}]});const P=new M({positive:e=>{let t=E.object({status:E.literal(`success`),data:e}),n=o(e);return n.length&&T.add(t,{examples:n.map(e=>({status:`success`,data:e}))}),t},negative:N,handler:({error:e,input:t,output:n,request:r,response:i,logger:a})=>{if(e){let n=x(e);y(n,a,r,t),i.status(n.statusCode).set(n.headers).json({status:`error`,error:{message:h(n)}});return}i.status(g.positive).json({status:`success`,data:n})}}),F=E.string();T.add(F,{examples:[`Sample error message`]});const I=new M({positive:e=>{let t=e instanceof E.ZodObject&&`items`in e.shape&&e.shape.items instanceof E.ZodArray?e.shape.items:E.array(E.any());if(o(t).length)return t;let n=o(e).filter(e=>i(e)&&`items`in e&&Array.isArray(e.items)).map(e=>e.items);if(n?.length){let e=t.meta();T.remove(t).add(t,{...e,examples:n})}return t},negative:{schema:F,mimeType:`text/plain`},handler:({response:e,output:t,error:n,logger:r,request:i,input:a})=>{if(n){let t=x(n);y(t,r,i,a),e.status(t.statusCode).type(`text/plain`).send(h(t));return}if(`items`in t&&Array.isArray(t.items)){e.status(g.positive).json(t.items);return}throw Error(`Property 'items' is missing in the endpoint output`)}}),L=e=>{let t=[];return e.scope&&t.push(e.scope),e.noStore&&t.push(`no-store`),e.noCache&&t.push(`no-cache`),e.maxAge!==void 0&&t.push(`max-age=${e.maxAge}`),e.sMaxAge!==void 0&&t.push(`s-maxage=${e.sMaxAge}`),e.mustRevalidate&&t.push(`must-revalidate`),e.proxyRevalidate&&t.push(`proxy-revalidate`),e.mustUnderstand&&t.push(`must-understand`),e.immutable&&t.push(`immutable`),e.noTransform&&t.push(`no-transform`),e.staleWhileRevalidate!==void 0&&t.push(`stale-while-revalidate=${e.staleWhileRevalidate}`),e.staleIfError!==void 0&&t.push(`stale-if-error=${e.staleIfError}`),t.join(`, `)},Ne={"max-age":`maxAge`,"max-stale":`maxStale`,"min-fresh":`minFresh`,"stale-if-error":`staleIfError`},Pe={"no-cache":`noCache`,"no-store":`noStore`,"no-transform":`noTransform`,"only-if-cached":`onlyIfCached`},Fe=e=>{if(!e)return;let t={};for(let n of e.toLowerCase().split(`,`)){let[e,r]=n.split(`=`),i=Ne[e.trim()];if(i){let e=parseInt(r?.trim()??``,10);isNaN(e)||(t[i]=e);continue}let a=Pe[e.trim()];a&&(t[a]=!0)}return t},R=e=>new v({handler:async({request:t,response:n})=>(e&&n.setHeader(`Cache-Control`,L(e)),{get ifNoneMatch(){let e=t.headers[`if-none-match`];if(!e)return;let n=e.trim();return n===`*`?n:n.split(`,`).map(e=>e.trim().replace(/^(?:W\/)?"/,``).replace(/"$/,``))},get ifModifiedSince(){let e=t.headers[`if-modified-since`];if(!e)return;let n=new Date(e);return isNaN(n.getTime())?void 0:n},get cacheControl(){return Fe(t.headers[`cache-control`])},addCachePolicy:t=>{n.setHeader(`Cache-Control`,L({...e,...t}))},setETag:e=>{n.setHeader(`ETag`,e.startsWith(`"`)?e:`"${e}"`)},setLastModified:e=>{n.setHeader(`Last-Modified`,e.toUTCString())},setVary:(...e)=>{n.setHeader(`Vary`,e.join(`, `))},setExpires:e=>{n.setHeader(`Expires`,e.toUTCString())},clearSiteData:()=>{n.setHeader(`Clear-Site-Data`,`"cache"`)},notModified:()=>{n.status(304).end()}})}),z=e=>new v({handler:async({request:t,response:n})=>({getCookie:e=>t.signedCookies?.[e]??t.cookies?.[e],setCookie:(t,r,i)=>{n.cookie(t,r,{...e,...i})},clearCookie:(t,r)=>{n.clearCookie(t,{...e,...r})}})}),B=e=>{let t=w(`express-rate-limit`)({statusCode:429,...e,handler:(e,t,n,r)=>{n(D(r.statusCode,r.message))}}),{getKey:n,resetKey:r}=t,i={getKey:n,resetKey:r};return new b(t,{statusCode:e?.statusCode,provider:t=>({rateLimit:{...i,...t[e?.requestPropertyName??`rateLimit`]}})})};var V=class e{resultHandler;schema=void 0;statusCodes=new Set;middlewares=[];constructor(e){this.resultHandler=e}#e(t){let n=new e(this.resultHandler);return n.middlewares=this.middlewares.concat(t),n.schema=Ae(this.schema,t.schema),n.statusCodes=this.statusCodes.union(t.statusCodes),n}addMiddleware(e){return this.#e(e instanceof v?e:new v(e))}useCookies(...e){return this.#e(z(...e))}useCache(...e){return this.#e(R(...e))}useRateLimit(...e){return this.#e(B(...e))}use=this.addExpressMiddleware;addExpressMiddleware(...e){return this.#e(new b(...e))}addContext(e){return this.#e(new v({handler:({ctx:t})=>e(t)}))}build({input:e=l,output:t,operationId:n,scope:r,tag:i,method:a,statusCode:o,...s}){let{middlewares:c,resultHandler:u}=this,d=typeof a==`string`?[a]:a,f=typeof n==`function`?n:e=>n&&`${n}${e===`head`?`__HEAD`:``}`,p=typeof r==`string`?[r]:r||[],m=typeof i==`string`?[i]:i||[];return new ie({...s,middlewares:c,outputSchema:t,resultHandler:u,scopes:p,tags:m,methods:d,getOperationId:f,inputSchema:je(this.schema,e),statusCodes:this.statusCodes.union(new Set(typeof o==`number`?[o]:o||[]))})}buildVoid({handler:e,...t}){return this.build({...t,output:l,handler:async t=>(await e(t),{})})}};const Ie=new V(P),Le=new V(I),H={debug:ve,info:be,warn:O(`#FFA500`),error:Se,ctx:ye},U={debug:10,info:20,warn:30,error:40},Re=e=>i(e)&&Object.keys(U).some(t=>t in e),ze=e=>e in U,Be=(e,t)=>U[e]<U[t],W=he.memoizeWith((e,t)=>`${e}${t}`,(e,t=0)=>Intl.NumberFormat(void 0,{useGrouping:!1,minimumFractionDigits:0,maximumFractionDigits:t,style:`unit`,unitDisplay:`long`,unit:e})),Ve=e=>e<1e-6?W(`nanosecond`,3).format(e/1e-6):e<.001?W(`nanosecond`).format(e/1e-6):e<1?W(`microsecond`).format(e/.001):e<1e3?W(`millisecond`).format(e):e<6e4?W(`second`,2).format(e/1e3):W(`minute`,2).format(e/6e4);var G=class e{config;constructor({color:e=_e.isSupported(),level:t=p.isProduction?`warn`:`debug`,depth:n=2,ctx:r={}}={}){this.config={color:e,level:t,depth:n,ctx:r}}format(e){let{depth:t,color:n,level:r}=this.config;return Ce(e,{depth:t,colors:n,breakLength:r===`debug`?80:1/0,compact:r!==`debug`||3})}print(e,t,n){let{level:r,ctx:{requestId:i,...a},color:o}=this.config;if(r===`silent`||Be(e,r))return;let s=[new Date().toISOString()];i&&s.push(o?H.ctx(i):i),s.push(o?`${H[e](e)}:`:`${e}:`,t),n!==void 0&&s.push(this.format(n)),Object.keys(a).length>0&&s.push(this.format(a)),console.log(s.join(` `))}debug(e,t){this.print(`debug`,e,t)}info(e,t){this.print(`info`,e,t)}warn(e,t){this.print(`warn`,e,t)}error(e,t){this.print(`error`,e,t)}child(t){return new e({...this.config,ctx:t})}get ctx(){return this.config.ctx}profile(e){let t=A.now();return()=>{let n=A.now()-t,{message:r,severity:i=`debug`,formatter:a=Ve}=typeof e==`object`?e:{message:e};this.print(typeof i==`function`?i(n):i,r,a(n))}}},He=class{logger;config;#e=new WeakMap;constructor(e,t){this.logger=e,this.config=t}#t(e,t,n){if(!e.isSchemaChecked){for(let e of[`input`,`output`]){let r=[E.toJSONSchema(t[`${e}Schema`],{unrepresentable:`any`})];for(let t of r){t.type&&t.type!==`object`&&this.logger.warn(`Endpoint ${e} schema is not object-based`,n);for(let e of[`allOf`,`oneOf`,`anyOf`])t[e]&&r.push(...t[e])}}if(t.getProbableRequestType()===`json`){let e=_(t.inputSchema,`input`);e&&this.logger.warn(`The final input schema of the endpoint contains an unsupported JSON payload type.`,{...n,reason:e})}for(let e of ae)for(let{mimeTypes:r,schema:i}of t.getResponses(e)){if(!r?.includes(S.json))continue;let t=_(i,`output`);t&&this.logger.warn(`The final ${e} response schema of the endpoint contains an unsupported JSON payload type.`,{...n,reason:t})}e.isSchemaChecked=!0}}#n(e,t,n,r,a){if(e.paths.has(r))return;let{pathParams:o,getLocation:s,isQueryEnabled:c}=fe({method:n,path:r,security:t.security,inputSources:f(n,this.config.inputSources)});if(!(o.size===0&&!c)){e.flat??=pe(E.toJSONSchema(t.inputSchema,{unrepresentable:`any`,io:`input`,override:({zodSchema:e,jsonSchema:t})=>{(e._zod.traits.has(`$ZodPreprocess`)||`coerce`in e._zod.def&&e._zod.def.coerce)&&(t[me]=!0)}}));for(let[t,n]of Object.entries(e.flat.properties)){if(!i(n))continue;let r=s(t);(r===`path`||r===`query`)&&(r===`path`&&!e.flat.required?.includes(t)&&this.logger.warn(`The path parameter "${t}" is declared optional in the input schema, but path parameters are always required since Express matches the route only when the segment is present.`,{...a,name:t}),!de(n,r)&&this.logger.warn(`The ${r} parameter "${t}" has a schema that most likely would not accept the parsed data, ${r===`path`?`since path parameters always arrive as strings`:`depending on the "queryParser" config option`}. Convert the parsed value from "z.string()" using ".transform()" method, or use "z.coerce" at least.`,{...a,name:t,jsonSchema:n}))}for(let e of o)this.logger.warn(`The input schema of the endpoint is most likely missing the parameter of the path it's assigned to.`,{...a,param:e});e.paths.add(r)}}check=(e,t,n)=>{let r=this.#e.get(n);r||(r={isSchemaChecked:!1,paths:new Set},this.#e.set(n,r)),this.#t(r,n,{method:e,path:t}),this.#n(r,n,e,t,{method:e,path:t})}};const K=e=>e.sort((e,t)=>te(t)-+te(e)||e.localeCompare(t)).join(`, `).toUpperCase(),Ue=e=>({method:t},n,r)=>{let i=K(e);n.set({Allow:i}),r(D(405,`${t} is not allowed`,{headers:{Allow:i}}))},We=({app:e,getLogger:t,config:n,routing:r})=>{let i=p.isProduction?void 0:new He(t(),n),a=new Map;return oe({routing:r,config:n,onEndpoint:(e,t,r)=>{i?.check(e,t,r),a.has(t)||a.set(t,new Map(n.cors?[[`options`,r]]:[])),a.get(t)?.set(e,r)},onStatic:e.use.bind(e)}),a},q=({app:e,config:t,getLogger:n,...r})=>{let i=We({app:e,getLogger:n,config:t,...r}),a=new Map;for(let[r,o]of i){let i=Array.from(o.keys());i.includes(`get`)&&i.push(`head`);for(let[a,s]of o){let o=[];t.cors&&o.push((e,t,n)=>{t.set(`Access-Control-Allow-Methods`,K(i)),n()}),o.push(async(e,r)=>{let i=n(e);return s.execute({request:e,response:r,logger:i,config:t})}),e[a]?.(r,...o)}t.hintAllowedMethods!==!1&&a.set(r,Ue(i))}for(let[t,n]of a)e.all(t,n)},Ge=e=>`_httpMessage`in e&&typeof e._httpMessage==`object`&&e._httpMessage!==null&&`headersSent`in e._httpMessage&&typeof e._httpMessage.headersSent==`boolean`&&`setHeader`in e._httpMessage&&typeof e._httpMessage.setHeader==`function`,Ke=e=>`server`in e&&typeof e.server==`object`&&e.server!==null&&`close`in e.server&&typeof e.server.close==`function`,qe=e=>`encrypted`in e&&typeof e.encrypted==`boolean`&&e.encrypted,Je=({},e)=>void(!e.headersSent&&e.setHeader(`connection`,`close`)),Ye=e=>{let{promise:t,resolve:n,reject:r}=Promise.withResolvers();return e.close(e=>e?r(e):n()),t},Xe=({timeout:e=1e3,logger:t}={})=>{let n,r=new Set,i=new Set,a=e=>void i.delete(e),o=e=>a(e.destroy()),s=e=>void(Ge(e)?!e._httpMessage.headersSent&&e._httpMessage.setHeader(`connection`,`close`):o(e)),c=e=>void(n?e.destroy():i.add(e.once(`close`,()=>a(e)).once(`error`,()=>o(e)))),l=async()=>{for(let e of r)e.on(`request`,Je);t?.info(`Graceful shutdown`,{sockets:i.size,timeout:e});for(let e of i)(qe(e)||Ke(e))&&s(e);for await(let t of Ee(10,Date.now()))if(i.size===0||Date.now()-t>=e)break;for(let e of i)o(e);return Promise.allSettled(r.values().map(Ye))},u={sockets:i,add:(...e)=>{for(let t of e)if(!r.has(t)){r.add(t);for(let e of[`connection`,`secureConnection`])t.on(e,c)}return u},shutdown:()=>n??=l(),get isShuttingDown(){return!!n}};return u},J=Symbol.for(`express-zod-api`),Ze=({errorHandler:e,getLogger:t})=>async(n,r,i,a)=>n?e.execute({error:C(n),request:r,response:i,input:null,output:null,ctx:{},logger:t(r)}):a(),Qe=({errorHandler:e,getLogger:t})=>async(n,r)=>{let i=D(404,`Can not ${n.method} ${n.path}`),a=t(n);await e.execute({request:n,response:r,logger:a,error:i,input:null,output:null,ctx:{}})},$e=e=>(t,{},n)=>{if(Object.values(t?.files||[]).flat().find(({truncated:e})=>e))return n(e);n()},et=({config:e})=>{let t=w(`cookie-parser`),{secret:n,...r}={...typeof e.cookies==`object`&&e.cookies};return t(n,Object.keys(r).length?r:void 0)},Y=e=>typeof e==`function`?e:({},e,t)=>{e.set({"Access-Control-Allow-Origin":`*`,"Access-Control-Allow-Headers":`content-type`}),t()},tt=e=>({log:t=>{/not eligible/i.test(t)||e.debug(t)}}),nt=({getLogger:e,config:t})=>{let n=w(`express-fileupload`),{limitError:r,beforeUpload:i,...a}={...typeof t.upload==`object`&&t.upload},o=[];return o.push(async(t,r,o)=>{let s=e(t);return await i?.({request:t,logger:s}),n({debug:!0,...a,abortOnLimit:!1,parseNested:!0,logger:tt(s)})(t,r,o)}),r&&o.push($e(r)),o},rt=(e,{},t)=>{Buffer.isBuffer(e.body)&&(e.body={raw:e.body}),t()},it=({logger:e,config:{childLoggerProvider:t,accessLogger:n=({method:e,path:t},n)=>n.debug(`${e}: ${t}`)}})=>async(r,i,a)=>{let o=await t?.({request:r,parent:e})||e;n?.(r,o),r.res&&(r.res.locals[J]={logger:o}),a()},at=e=>t=>t?.res?.locals[J]?.logger||e;let ot=!1;const st=e=>{ot||(ot=!0,process.on(`deprecation`,({message:t,namespace:n,name:r,stack:i})=>e.warn(`${r} (${n}): ${t}`,i.split(`
|
|
2
|
+
`).slice(1))))};let X,Z,Q;const ct=({servers:e,logger:t,options:{timeout:n,beforeExit:r,events:i=[`SIGINT`,`SIGTERM`]}})=>{X??=Xe({logger:t,timeout:n}),X.add(...e),r&&(Q??=new Set).add(r),Z??=async()=>{X?.isShuttingDown||(await X?.shutdown(),Q&&await Promise.allSettled(Q.values().map(async e=>e())),process.exit())};for(let e of i)process.listeners(e).includes(Z)||process.on(e,Z)},lt=e=>{if(e.columns<62)return;let t=xe(`for Angie`.padStart(52)),n=O(`#F5A9B8`),r=O(`#5BCEFA`),i=[r`8888888888${t}`,r`888`,r`888`,r`8888888 888 888 88888b. 888d888 .d88b. .d8888b .d8888b`,n`888 `Y8bd8P' 888 "88b 888P" d8P Y8b 88K 88K`,n`888 X88K 888 888 888 88888888 "Y8888b. "Y8888b.`,n`888 .d8""8b. 888 d88P 888 Y8b. X88 X88`,k`8888888888 888 888 88888P" 888 "Y8888 88888P' 88888P'`,k` 888`,k`8888888888P 888 888 d8888 8888888b. 8888888`,k` d88P 888 d88888 888 Y88b 888`,n` d88P 888 d88P888 888 888 888`,n` d88P .d88b. .d88888 d88P 888 888 d88P 888`,n` d88P d88""88b d88" 888 d88P 888 8888888P" 888`,r` d88P 888 888 888 888 d88P 888 888 888`,r` d88P Y88..88P Y88b 888 d8888888888 888 888`,r`d8888888888 "Y88P" "Y88888 d88P 888 888 8888888`];e.write(`
|
|
3
3
|
`+i.join(`
|
|
4
4
|
`)+`
|
|
5
5
|
|
|
6
|
-
`)},
|
|
7
|
-
`)).parse({event:t,data:n}),
|
|
6
|
+
`)},ut=e=>{e.startupLogo!==!1&<(process.stdout);let t=e.errorHandler||P,n=Re(e.logger)?e.logger:new G(e.logger);n.debug(`Running`,{build:`v29.3.0`,env:p.env}),st(n);let r=it({logger:n,config:e}),i={getLogger:at(n),errorHandler:t},a=Qe(i),o=Ze(i);return{...i,logger:n,notFoundHandler:a,catcher:o,loggingMiddleware:r}},dt=(e,t)=>{let{logger:n,getLogger:r,notFoundHandler:i,loggingMiddleware:a}=ut(e),o=e.app.use(a);return e.cors&&o.use(Y(e.cors)),q({app:o,routing:t,getLogger:r,config:e}),{notFoundHandler:i,logger:n}},ft=(e,t)=>{let{logger:n,getLogger:r,notFoundHandler:i,catcher:a,loggingMiddleware:o}=ut(e),s=j().disable(`x-powered-by`).set(`query parser`,e.queryParser??`simple`).use(o);if(e.beforeParsers?.({app:s,getLogger:r}),e.compression){let t=w(`compression`);s.use(t(typeof e.compression==`object`?e.compression:void 0))}return e.cookies&&s.use(et({config:e})),e.cors&&s.use(Y(e.cors)),s.use(e.jsonParser||j.json()).use(e.formParser||j.urlencoded()).use(e.rawParser||j.raw(),rt),e.upload&&s.use(...nt({config:e,getLogger:r})),e.beforeRouting?.({app:s,getLogger:r}),q({app:s,routing:t,getLogger:r,config:e}),e.afterRouting?.({app:s,getLogger:r}),s.use(a,i),{app:s,logger:n}},pt=(e,t)=>{let{app:n,logger:r}=ft(e,t),i=[],a=(e,t)=>()=>e.listen(t,()=>r.info(`Listening`,t)),o=[];if(e.http){let t=we.createServer(n);i.push(t),o.push(a(t,e.http.listen))}if(e.https){let t=Te.createServer(e.https.options,n);i.push(t),o.push(a(t,e.https.listen))}return i.length||r.warn(`No servers configured.`),e.gracefulShutdown&&ct({logger:r,servers:i,options:e.gracefulShutdown===!0?{}:e.gracefulShutdown}),{app:n,logger:r,servers:o.map(e=>e())}},mt=e=>De({...e,headers:{"content-type":S.json,...e?.headers}}),ht=e=>Oe(e),gt=e=>{let t={warn:[],error:[],info:[],debug:[]};return new Proxy(e||{},{get(e,n,r){return n===`_getLogs`?()=>t:ze(n)?(...e)=>t[n].push(e):Reflect.get(e,n,r)}})},_t=({requestProps:e,responseOptions:t,configProps:n,loggerProps:r})=>{let i=mt(e),a=ht({req:i,...t});a.req=t?.req||i,i.res=a;let o=gt(r);return{requestMock:i,responseMock:a,loggerMock:o,configMock:{cors:!1,logger:o,...n}}},vt=async({endpoint:e,...t})=>{let{requestMock:n,responseMock:r,loggerMock:i,configMock:a}=_t(t);return await e.execute({request:n,response:r,config:a,logger:i}),{requestMock:n,responseMock:r,loggerMock:i}},yt=async({middleware:t,ctx:n={},...r})=>{let{configMock:{inputSources:i,errorHandler:a=P},...o}=_t(r),s=e(o.requestMock,i),c={request:o.requestMock,response:o.responseMock,logger:o.loggerMock,input:s,ctx:n};try{let e=await t.execute(c);return{...o,output:e}}catch(e){return await a.execute({...c,error:C(e),output:null}),{...o,output:{}}}},$=(e,t)=>E.object({data:t,event:E.literal(e),id:E.string().optional(),retry:E.int().positive().optional()}),bt=(e,t,n)=>$(String(t),e[t]).transform(e=>[`event: ${e.event}`,`data: ${JSON.stringify(e.data)}`,``,``].join(`
|
|
7
|
+
`)).parse({event:t,data:n}),xt=e=>e.headersSent||e.writeHead(200,{connection:`keep-alive`,"content-type":S.sse,"cache-control":`no-cache`}),St=e=>new v({handler:async({request:t,response:n})=>{let r=new AbortController,i=setTimeout(()=>xt(n),1e4);return t.once(`close`,()=>{clearTimeout(i),r.abort()}),{isClosed:()=>n.writableEnded||n.closed,signal:r.signal,emit:(t,r)=>{xt(n),n.write(bt(e,t,r),`utf-8`),n.flush?.()}}}}),Ct=e=>new M({positive:()=>{let[t,...r]=Object.entries(e).map(([e,t])=>$(e,t));if(!t)throw new n(Error(`At least one SSE event is required.`));return{mimeType:S.sse,schema:r.length?E.discriminatedUnion(`event`,[t,...r]):t}},negative:{mimeType:`text/plain`,schema:E.string()},handler:async({response:e,error:t,logger:n,request:r,input:i})=>{if(t){let a=x(t);y(a,n,r,i),e.headersSent||e.status(a.statusCode).type(`text/plain`).write(h(a),`utf-8`)}e.end()}});var wt=class extends V{constructor(e){super(Ct(e)),this.middlewares=[St(e)]}};const Tt=[`total`,`limit`,`offset`],Et=[`nextCursor`,`limit`];function Dt({style:e,itemSchema:t,itemsName:n=`items`,maxLimit:r=100,defaultLimit:i=20}){if(!Number.isInteger(r)||r<1)throw Error(`ez.paginated: maxLimit must be a positive integer`);if(!Number.isInteger(i)||i<1)throw Error(`ez.paginated: defaultLimit must be a positive integer`);if(i>r)throw Error(`ez.paginated: defaultLimit must not be greater than maxLimit`);if(e===`offset`&&Tt.includes(n))throw Error(`ez.paginated: itemsName must not match reserved keys for offset output (${Tt.join(`, `)})`);if(e===`cursor`&&Et.includes(n))throw Error(`ez.paginated: itemsName must not match reserved keys for cursor output (${Et.join(`, `)})`);let a=E.coerce.number().int().min(1).max(r).default(i).describe(`Page size (number of ${n} per page)`);if(e===`offset`){let e=E.coerce.number().int().min(0).default(0).describe(`Number of ${n} to skip`);return{input:E.object({limit:a,offset:e}),output:E.object({[n]:E.array(t).describe(`Page of ${n}`),total:E.number().int().min(0).describe(`Total number of ${n}`),limit:E.number().int().min(1).describe(`Page size used`),offset:E.number().int().min(0).describe(`Offset used`)})}}let o=E.string().optional().describe(`Cursor for the next page; omit for first page`);return{input:E.object({cursor:o,limit:a}),output:E.object({[n]:E.array(t).describe(`Page of ${n}`),nextCursor:E.string().nullable().describe(`Cursor for the next page, or null if no more pages`),limit:E.number().int().min(1).describe(`Page size used`)})}}const Ot={dateIn:ne,dateOut:re,form:u,upload:le,raw:ue,buffer:s,paginated:Dt};export{G as BuiltinLogger,t as DocumentationError,V as EndpointsFactory,wt as EventStreamFactory,ce as InputValidationError,v as Middleware,d as MissingPeerError,r as OutputValidationError,M as ResultHandler,c as RoutingError,m as ServeStatic,Le as arrayEndpointsFactory,I as arrayResultHandler,dt as attachRouting,ee as createApiResponse,R as createCacheMiddleware,ke as createConfig,z as createCookieMiddleware,B as createRateLimitMiddleware,pt as createServer,Ie as defaultEndpointsFactory,P as defaultResultHandler,x as ensureHttpError,Ot as ez,a as getMessageFromError,vt as testEndpoint,yt as testMiddleware};
|
package/dist/integration.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as FlatObject, g as CommonConfig, l as EmptyObject, t as Routing } from "./routing-
|
|
1
|
+
import { d as FlatObject, g as CommonConfig, l as EmptyObject, t as Routing } from "./routing-BudY5key.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import ts, { default as ts$1 } from "typescript";
|
|
4
4
|
interface NextHandlerInc<U> {
|
package/dist/integration.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{$ as e,A as t,F as n,K as r,M as i,P as a,W as o,Z as s,_ as c,
|
|
1
|
+
import{$ as e,A as t,F as n,K as r,M as i,P as a,W as o,Z as s,_ as c,n as l,p as u,q as d,r as f,t as p,tt as m,v as h}from"./routing-walker-tYoo7Yh5.js";import{t as g}from"./peer-helpers-CpnWOg95.js";import{globalRegistry as _,z as v}from"zod";import*as y from"ramda";import b from"typescript";const x={Path:`Path`,Implementation:`Implementation`,DefaultContext:`DefaultContext`,key:`key`,path:`path`,params:`params`,ctx:`ctx`,method:`method`,request:`request`,event:`event`,data:`data`,handler:`handler`,msg:`msg`,parseRequest:`parseRequest`,substitute:`substitute`,provide:`provide`,on:`on`,implementation:`implementation`,hasBody:`hasBody`,undefined:`undefined`,response:`response`,rest:`rest`,searchParams:`searchParams`,defaultImplementation:`defaultImplementation`,hasFiles:`hasFiles`,value:`value`,File:`File`,FormData:`FormData`,headers:`headers`,body:`body`,client:`client`,contentType:`contentType`,isBlob:`isBlob`,source:`source`,Method:`Method`,SomeOf:`SomeOf`,Request:`Request`,Pagination:`Pagination`,override:`override`},S={input:`Input`,positive:`PositiveResponse`,negative:`NegativeResponse`,encoded:`EncodedResponse`,response:`Response`},C=e=>Array.from(e,e=>`"${e}"`),w=e=>e;var ee=class{serverUrl;paths=new Set;tags=new Map;registry=new Map;constructor(e){this.serverUrl=e}makeMethodType=()=>{let t=C(e).join(` | `);return`export type ${x.Method} = ${t};`};makeOmit=(e,t,n=``)=>`Omit<${e}, ${n&&`\n/** ${n} */\n`}${C(t).join(` | `)}>`;makeSomeOfType=()=>`type ${x.SomeOf}<T> = T[keyof T];`;makeRequestType=()=>`export type ${x.Request} = keyof ${S.input};`;someOf=e=>`${x.SomeOf}<${e}>`;makePathType=()=>{let e=C(this.paths).join(` | `);return`export type ${x.Path} = ${e};`};makePublicInterfaces=()=>Object.keys(S).map(e=>{let t=Array.from(this.registry).map(([t,{store:n,isDeprecated:r}])=>` ${r?`/** @deprecated */
|
|
2
2
|
`:``}"${t}": ${n[e]};`).join(`
|
|
3
3
|
`);return`export interface ${S[e]} {\n${t}\n}`});makeEndpointTags=()=>`export const endpointTags = {\n${Array.from(this.tags).map(([e,t])=>` "${e}": [${C(t).join(`, `)}]`).join(`,
|
|
4
4
|
`)}\n}`;makeImplementationType=()=>{let e=[`${x.method}: ${x.Method}`,`${x.path}: string`,`${x.params}: Record<string, any>`,`${x.ctx}?: T`].join(`,`);return`export type ${x.Implementation}<T extends Record<string, unknown>> = (${e}) => Promise<any>;`};makeDefaultContextType=()=>`export type ${x.DefaultContext} = { ${x.override}?: (init: RequestInit) => RequestInit };`;makeParseRequestFn=()=>{let e=`${x.request}: string`,t=`[${x.Method}, ${x.Path}]`,n=`${x.request}.${w(`split`)}(/ (.+)/, 2) as ${t}`;return`const ${x.parseRequest} = (${e}) => ${n};`};makeSubstituteFn=()=>{let e=`${x.path}: string, ${x.params}: Record<string, any>`,t=`\`:\${${x.key}}\``,n=`: [typeof ${x.path}, typeof ${x.params}]`;return[`const ${x.substitute} = (${e})${n} => {`,` if (${x.params} instanceof Blob) return [${x.path}, ${x.params}] as const;`,` const ${x.rest} = { ...${x.params} };`,` for (const ${x.key} in ${x.params}) {`,` ${x.path} = ${x.path}.${w(`replace`)}(${t}, () => {`,` delete ${x.rest}[${x.key}];`,` return ${x.params}[${x.key}];`,` });`,` }`,` return [${x.path}, ${x.rest}] as const;`,`}`].join(`
|
|
@@ -6,8 +6,8 @@ import{$ as e,A as t,F as n,K as r,M as i,P as a,W as o,Z as s,_ as c,c as l,n a
|
|
|
6
6
|
`)};makeDefaultImplementation=e=>{let t=`${x.method}, ${x.path}, ${x.params}, ${x.ctx}`,n=C([`get`,`head`,`delete`]).join(`, `),r=`!${x.hasBody} || ${x.hasFiles} ? ${x.undefined} : { "Content-Type": ${x.isBlob} ? "${m.raw}" : "${m.json}" };`,i=`${x.response}.${w(`headers`)}.${w(`get`)}("content-type")`,a=`\`?\${new ${URLSearchParams.name}(${x.params})}\``;return[`const ${x.defaultImplementation}: ${x.Implementation}<${x.DefaultContext}> = async (${t}) => {`,` const ${x.isBlob} = ${x.params} instanceof Blob;`,` const ${x.hasFiles} = !${x.isBlob} && Object.${w(`values`)}(${x.params}).${w(`some`)}((one) => one instanceof Blob || one instanceof ${x.File});`,` const ${x.hasBody} = ![${n}].includes(${x.method});`,` const ${x.searchParams} = ${x.isBlob} || ${x.hasBody} ? "" : ${a};`,` const ${x.headers} = ${r}`,` let ${x.body}: RequestInit["${x.body}"] = ${x.undefined};`,` if (${x.hasBody}) {`,` if (${x.isBlob}) {`,` ${x.body} = ${x.params};`,` } else if (${x.hasFiles}) {`,` ${x.body} = new ${FormData.name}();`,` for (const [${x.key}, ${x.value}] of Object.${w(`entries`)}(${x.params}))`,` if (${x.value} !== undefined) ${x.body}.${w(`append`)}(${x.key}, ${x.value});`,` } else {`,` ${x.body} = JSON.${w(`stringify`)}(${x.params});`,` }`,` }`,` let init: RequestInit = {`,` ${w(`method`)}: ${x.method}.${w(`toUpperCase`)}(),`,` ${w(`credentials`)}: ${e?`"include"`:x.undefined},`,` ${x.headers},`,` ${x.body},`,` };`,` if (${x.ctx}?.${x.override}) init = ${x.ctx}.${x.override}(init);`,` const ${x.response} = await ${fetch.name}(`,` new ${URL.name}(\`\${${x.path}}\${${x.searchParams}}\`, "${this.serverUrl}"),`,` init,`,` );`,` const ${x.contentType} = ${i};`,` if (!${x.contentType}) return;`,` if (${x.contentType}.${w(`startsWith`)}("${m.json}")) return ${x.response}.${w(`json`)}();`,` if (${x.contentType}.${w(`startsWith`)}("text/")) return ${x.response}.${w(`text`)}();`,` return ${x.response}.${w(`blob`)}();`,`};`].join(`
|
|
7
7
|
`)};makeSubscriptionClass=(e,t)=>{let n=`${x.substitute}(${x.parseRequest}(${x.request})[1], ${x.params})`,r=`Extract<R, { ${w(`event`)}: E }>["${w(`data`)}"]`,i=`(${x.msg} as ${MessageEvent.name}).${w(`data`)}`;return[`export class ${e}<`,` K extends Extract<${x.Request}, \`get \${string}\`>,`,` R extends Extract<${S.positive}[K], { ${w(`event`)}: string }>,`,`> {`,` public ${x.source}: EventSource;`,` public constructor(${x.request}: K, ${x.params}: ${S.input}[K]) {`,` const [${x.path}, ${x.rest}] = ${n};`,` const ${x.searchParams} = \`?\${new ${URLSearchParams.name}(${x.rest})}\`;`,` this.${x.source} = new EventSource(`,` new URL(\`\${${x.path}}\${${x.searchParams}}\`, "${this.serverUrl}"),`,` { ${w(`withCredentials`)}: ${t?`true`:x.undefined} }`,` );`,` }`,` public ${x.on}<E extends R["${w(`event`)}"]>(`,` ${w(`event`)}: E,`,` ${x.handler}: (${x.data}: ${r}) => void | Promise<void>,`,` ) {`,` this.${x.source}.${w(`addEventListener`)}(${x.event}, (${x.msg}) =>`,` ${x.handler}(JSON.${w(`parse`)}(${i})),`,` );`,` return this;`,` }`,`}`].join(`
|
|
8
8
|
`)};makeUsageStatements=(e,t)=>[`const ${x.client} = new ${e}();`,`${x.client}.${x.provide}("get /v1/user/retrieve", { id: "10" });`,`new ${t}("get /v1/events/stream", {}).${x.on}("time", (time) => {});`].join(`
|
|
9
|
-
`)};const T=b.factory,E=/^[A-Za-z_$][A-Za-z0-9_$]*$/,D=[b.SyntaxKind.AnyKeyword,b.SyntaxKind.BigIntKeyword,b.SyntaxKind.BooleanKeyword,b.SyntaxKind.NeverKeyword,b.SyntaxKind.NumberKeyword,b.SyntaxKind.ObjectKeyword,b.SyntaxKind.StringKeyword,b.SyntaxKind.SymbolKeyword,b.SyntaxKind.UndefinedKeyword,b.SyntaxKind.UnknownKeyword,b.SyntaxKind.VoidKeyword],O=e=>typeof e==`number`?T.createNumericLiteral(e):typeof e==`bigint`?T.createBigIntLiteral(e.toString()):typeof e==`boolean`?e?T.createTrue():T.createFalse():e===null?T.createNull():T.createStringLiteral(e),k=e=>T.createIdentifier(e),A=e=>typeof e==`string`&&E.test(e)?k(e):O(e),j=(e,t)=>typeof e==`number`?T.createKeywordTypeNode(e):typeof e==`string`||b.isIdentifier(e)?T.createTypeReferenceNode(e,t&&y.map(j,t)):e,M=e=>{let t=new Map;for(let n of e)t.set(N(n)?n.kind:n,n);return T.createUnionTypeNode(Array.from(t.values()))},N=e=>D.includes(e.kind),P=(e,t)=>b.addSyntheticLeadingComment(e,b.SyntaxKind.MultiLineCommentTrivia,`* ${t} `,!0),F=(e,t)=>{let n=b.createSourceFile(`print.ts`,``,b.ScriptTarget.Latest,!1,b.ScriptKind.TS);return b.createPrinter(t).printNode(b.EmitHint.Unspecified,e,n)},te=(e,t,{isOptional:n,hasUndefined:r=n,isDeprecated:i,comment:a}={})=>{let o=j(t),s=T.createPropertySignature(void 0,A(e),n?T.createToken(b.SyntaxKind.QuestionToken):void 0,r?M([o,j(b.SyntaxKind.UndefinedKeyword)]):o),c=y.reject(y.isNil,[i?`@deprecated`:void 0,a]);return c.length?P(s,c.join(` `)):s},I=e=>T.createLiteralTypeNode(O(e)),L=(e,{rules:t,onMissing:r,ctx:i={}})=>{let a=n(e),o=a&&a in t?t[a]:t[e._zod.def.type],s=e=>L(e,{ctx:i,rules:t,onMissing:r});return o?o(e,{...i,next:s}):r(e,i)},R={name:y.path([`name`,`text`]),type:y.path([`type`]),optional:y.path([`questionToken`])},z=({_zod:{def:e}})=>{let t=e.values.map(e=>e===void 0?j(b.SyntaxKind.UndefinedKeyword):I(e));return t.length===1?t[0]:M(t)},B=({_zod:{def:e}},{next:t})=>{let{parts:n}=e,i=0,a=()=>{let e=``;for(;i<n.length;){let t=n[i];if(r(t))break;i++,e+=t??``}return e},o=T.createTemplateHead(a()),s=[];for(;i<n.length;){let e=t(n[i++]),r=a(),o=i<n.length?T.createTemplateMiddle:T.createTemplateTail;s.push(T.createTemplateLiteralTypeSpan(e,o(r)))}return s.length?T.createTemplateLiteralType(o,s):I(o.text)},V=(e,{isResponse:t,next:n,makeAlias:r})=>{let i=()=>{let r=Object.entries(e._zod.def.shape).map(([e,r])=>{let{description:i,deprecated:a}=_.get(r)||{},o=(t?r._zod.optout:r._zod.optin)===`optional`,s=o&&!(r instanceof v.core.$ZodExactOptional);return te(e,n(r),{comment:i,isDeprecated:a,isOptional:o,hasUndefined:s})});return T.createTypeLiteralNode(r)};return c(e,{io:t?`output`:`input`})?r(e,i):i()},H=({_zod:{def:e}},{next:t})=>T.createArrayTypeNode(t(e.element)),U=({_zod:{def:e}})=>M(y.map(I,Object.values(e.entries))),W=({_zod:{def:e}},{next:t})=>M(e.options.map(t)),G=({_zod:{def:e}},{next:t})=>M([t(e.innerType),I(null)]),K=({_zod:{def:e}},{next:t})=>T.createTupleTypeNode(e.items.map(t).concat(e.rest===null?[]:T.createRestTypeNode(t(e.rest)))),q=({_zod:{def:e}},{next:t})=>{let[n,r]=[e.keyType,e.valueType].map(t),i=j(`Record`,[n,r]);return e.mode===`loose`?T.createIntersectionTypeNode([i,j(`Record`,[`PropertyKey`,r])]):i},J=y.tryCatch(e=>{if(!e.every(b.isTypeLiteralNode))throw Error(`Not objects`);let t=y.chain(y.prop(`members`),e),n=y.uniqWith((...e)=>{if(!y.eqBy(R.name,...e))return!1;if(y.both(y.eqBy(R.type),y.eqBy(R.optional))(...e))return!0;throw Error(`Has conflicting prop`)},t);return T.createTypeLiteralNode(n)},(e,t)=>T.createIntersectionTypeNode(t)),Y=({_zod:{def:e}},{next:t})=>J([e.left,e.right].map(t)),X=e=>()=>j(e),Z=({_zod:{def:e}},{next:t})=>t(e.innerType),Q=e=>j(e?b.SyntaxKind.UnknownKeyword:b.SyntaxKind.AnyKeyword),ne=({_zod:{def:e}},{next:t,isResponse:n})=>{let i=e[n?`out`:`in`],a=e[n?`in`:`out`];if(!r(i,`transform`))return t(i);let s=t(a),c={[b.SyntaxKind.AnyKeyword]:``,[b.SyntaxKind.BigIntKeyword]:BigInt(0),[b.SyntaxKind.BooleanKeyword]:!1,[b.SyntaxKind.NumberKeyword]:0,[b.SyntaxKind.ObjectKeyword]:{},[b.SyntaxKind.StringKeyword]:``,[b.SyntaxKind.UndefinedKeyword]:void 0}[s.kind],l=o(i,c),u={number:b.SyntaxKind.NumberKeyword,bigint:b.SyntaxKind.BigIntKeyword,boolean:b.SyntaxKind.BooleanKeyword,string:b.SyntaxKind.StringKeyword,undefined:b.SyntaxKind.UndefinedKeyword,object:b.SyntaxKind.ObjectKeyword};return j(l&&u[l]||Q(n))},re=()=>I(null),ie=({_zod:{def:e}},{makeAlias:t,next:n})=>t(e.getter,()=>n(e.getter())),ae=()=>j(`Blob`),oe=(e,{next:t})=>t(e._zod.def.shape.raw),se={string:X(b.SyntaxKind.StringKeyword),number:X(b.SyntaxKind.NumberKeyword),bigint:X(b.SyntaxKind.BigIntKeyword),boolean:X(b.SyntaxKind.BooleanKeyword),any:X(b.SyntaxKind.AnyKeyword),undefined:X(b.SyntaxKind.UndefinedKeyword),[i]:X(b.SyntaxKind.StringKeyword),[t]:X(b.SyntaxKind.StringKeyword),never:X(b.SyntaxKind.NeverKeyword),void:X(b.SyntaxKind.UndefinedKeyword),unknown:X(b.SyntaxKind.UnknownKeyword),null:re,array:H,tuple:K,record:q,object:V,literal:z,template_literal:B,intersection:Y,union:W,default:Z,enum:U,optional:Z,nonoptional:Z,nullable:G,catch:Z,pipe:ne,lazy:ie,readonly:Z,[a]:ae,[h]:oe},$=(e,{brandHandling:t,ctx:n})=>L(e,{rules:{...t,...se},onMissing:({},{isResponse:e})=>Q(e),ctx:n});var ce=class extends ee{#e=[];#t=new Map;#n;#r(e,t){let n=this.#t.get(e);if(!n){n=`Type${this.#t.size+1}`,this.#t.set(e,n);let r=t();this.#e.push(e=>`type ${n} = ${F(r,e)};`)}return j(n)}constructor({routing:e,config:t,brandHandling:n,variant:r=`client`,clientClassName:i=`Client`,subscriptionClassName:a=`Subscription`,serverUrl:o=`https://example.com`,noBodySchema:c=v.undefined(),hasHeadMethod:m=!0,hasCredentials:h=!1}){super(o);let g={makeAlias:this.#r.bind(this)},_={brandHandling:n,ctx:{...g,isResponse:!1}},y={brandHandling:n,ctx:{...g,isResponse:!0}},b=!1,x=(e,t,n)=>{let r=d.bind(null,e,t),{isDeprecated:i,inputSchema:a,tags:o}=n,
|
|
10
|
-
`)});let g=
|
|
11
|
-
`)}\n}`),Object.assign(t,{[i]:
|
|
9
|
+
`)};const T=b.factory,E=/^[A-Za-z_$][A-Za-z0-9_$]*$/,D=[b.SyntaxKind.AnyKeyword,b.SyntaxKind.BigIntKeyword,b.SyntaxKind.BooleanKeyword,b.SyntaxKind.NeverKeyword,b.SyntaxKind.NumberKeyword,b.SyntaxKind.ObjectKeyword,b.SyntaxKind.StringKeyword,b.SyntaxKind.SymbolKeyword,b.SyntaxKind.UndefinedKeyword,b.SyntaxKind.UnknownKeyword,b.SyntaxKind.VoidKeyword],O=e=>typeof e==`number`?T.createNumericLiteral(e):typeof e==`bigint`?T.createBigIntLiteral(e.toString()):typeof e==`boolean`?e?T.createTrue():T.createFalse():e===null?T.createNull():T.createStringLiteral(e),k=e=>T.createIdentifier(e),A=e=>typeof e==`string`&&E.test(e)?k(e):O(e),j=(e,t)=>typeof e==`number`?T.createKeywordTypeNode(e):typeof e==`string`||b.isIdentifier(e)?T.createTypeReferenceNode(e,t&&y.map(j,t)):e,M=e=>{let t=new Map;for(let n of e)t.set(N(n)?n.kind:n,n);return T.createUnionTypeNode(Array.from(t.values()))},N=e=>D.includes(e.kind),P=(e,t)=>b.addSyntheticLeadingComment(e,b.SyntaxKind.MultiLineCommentTrivia,`* ${t} `,!0),F=(e,t)=>{let n=b.createSourceFile(`print.ts`,``,b.ScriptTarget.Latest,!1,b.ScriptKind.TS);return b.createPrinter(t).printNode(b.EmitHint.Unspecified,e,n)},te=(e,t,{isOptional:n,hasUndefined:r=n,isDeprecated:i,comment:a}={})=>{let o=j(t),s=T.createPropertySignature(void 0,A(e),n?T.createToken(b.SyntaxKind.QuestionToken):void 0,r?M([o,j(b.SyntaxKind.UndefinedKeyword)]):o),c=y.reject(y.isNil,[i?`@deprecated`:void 0,a]);return c.length?P(s,c.join(` `)):s},I=e=>T.createLiteralTypeNode(O(e)),L=(e,{rules:t,onMissing:r,ctx:i={}})=>{let a=n(e),o=a&&a in t?t[a]:t[e._zod.def.type],s=e=>L(e,{ctx:i,rules:t,onMissing:r});return o?o(e,{...i,next:s}):r(e,i)},R={name:y.path([`name`,`text`]),type:y.path([`type`]),optional:y.path([`questionToken`])},z=({_zod:{def:e}})=>{let t=e.values.map(e=>e===void 0?j(b.SyntaxKind.UndefinedKeyword):I(e));return t.length===1?t[0]:M(t)},B=({_zod:{def:e}},{next:t})=>{let{parts:n}=e,i=0,a=()=>{let e=``;for(;i<n.length;){let t=n[i];if(r(t))break;i++,e+=t??``}return e},o=T.createTemplateHead(a()),s=[];for(;i<n.length;){let e=t(n[i++]),r=a(),o=i<n.length?T.createTemplateMiddle:T.createTemplateTail;s.push(T.createTemplateLiteralTypeSpan(e,o(r)))}return s.length?T.createTemplateLiteralType(o,s):I(o.text)},V=(e,{isResponse:t,next:n,makeAlias:r})=>{let i=()=>{let r=Object.entries(e._zod.def.shape).map(([e,r])=>{let{description:i,deprecated:a}=_.get(r)||{},o=(t?r._zod.optout:r._zod.optin)===`optional`,s=o&&!(r instanceof v.core.$ZodExactOptional);return te(e,n(r),{comment:i,isDeprecated:a,isOptional:o,hasUndefined:s})});return T.createTypeLiteralNode(r)};return c(e,{io:t?`output`:`input`})?r(e,i):i()},H=({_zod:{def:e}},{next:t})=>T.createArrayTypeNode(t(e.element)),U=({_zod:{def:e}})=>M(y.map(I,Object.values(e.entries))),W=({_zod:{def:e}},{next:t})=>M(e.options.map(t)),G=({_zod:{def:e}},{next:t})=>M([t(e.innerType),I(null)]),K=({_zod:{def:e}},{next:t})=>T.createTupleTypeNode(e.items.map(t).concat(e.rest===null?[]:T.createRestTypeNode(t(e.rest)))),q=({_zod:{def:e}},{next:t})=>{let[n,r]=[e.keyType,e.valueType].map(t),i=j(`Record`,[n,r]);return e.mode===`loose`?T.createIntersectionTypeNode([i,j(`Record`,[`PropertyKey`,r])]):i},J=y.tryCatch(e=>{if(!e.every(b.isTypeLiteralNode))throw Error(`Not objects`);let t=y.chain(y.prop(`members`),e),n=y.uniqWith((...e)=>{if(!y.eqBy(R.name,...e))return!1;if(y.both(y.eqBy(R.type),y.eqBy(R.optional))(...e))return!0;throw Error(`Has conflicting prop`)},t);return T.createTypeLiteralNode(n)},(e,t)=>T.createIntersectionTypeNode(t)),Y=({_zod:{def:e}},{next:t})=>J([e.left,e.right].map(t)),X=e=>()=>j(e),Z=({_zod:{def:e}},{next:t})=>t(e.innerType),Q=e=>j(e?b.SyntaxKind.UnknownKeyword:b.SyntaxKind.AnyKeyword),ne=({_zod:{def:e}},{next:t,isResponse:n})=>{let i=e[n?`out`:`in`],a=e[n?`in`:`out`];if(!r(i,`transform`))return t(i);let s=t(a),c={[b.SyntaxKind.AnyKeyword]:``,[b.SyntaxKind.BigIntKeyword]:BigInt(0),[b.SyntaxKind.BooleanKeyword]:!1,[b.SyntaxKind.NumberKeyword]:0,[b.SyntaxKind.ObjectKeyword]:{},[b.SyntaxKind.StringKeyword]:``,[b.SyntaxKind.UndefinedKeyword]:void 0}[s.kind],l=o(i,c),u={number:b.SyntaxKind.NumberKeyword,bigint:b.SyntaxKind.BigIntKeyword,boolean:b.SyntaxKind.BooleanKeyword,string:b.SyntaxKind.StringKeyword,undefined:b.SyntaxKind.UndefinedKeyword,object:b.SyntaxKind.ObjectKeyword};return j(l&&u[l]||Q(n))},re=()=>I(null),ie=({_zod:{def:e}},{makeAlias:t,next:n})=>t(e.getter,()=>n(e.getter())),ae=()=>j(`Blob`),oe=(e,{next:t})=>t(e._zod.def.shape.raw),se={string:X(b.SyntaxKind.StringKeyword),number:X(b.SyntaxKind.NumberKeyword),bigint:X(b.SyntaxKind.BigIntKeyword),boolean:X(b.SyntaxKind.BooleanKeyword),any:X(b.SyntaxKind.AnyKeyword),undefined:X(b.SyntaxKind.UndefinedKeyword),[i]:X(b.SyntaxKind.StringKeyword),[t]:X(b.SyntaxKind.StringKeyword),never:X(b.SyntaxKind.NeverKeyword),void:X(b.SyntaxKind.UndefinedKeyword),unknown:X(b.SyntaxKind.UnknownKeyword),null:re,array:H,tuple:K,record:q,object:V,literal:z,template_literal:B,intersection:Y,union:W,default:Z,enum:U,optional:Z,nonoptional:Z,nullable:G,catch:Z,pipe:ne,lazy:ie,readonly:Z,[a]:ae,[h]:oe},$=(e,{brandHandling:t,ctx:n})=>L(e,{rules:{...t,...se},onMissing:({},{isResponse:e})=>Q(e),ctx:n});var ce=class extends ee{#e=[];#t=new Map;#n;#r(e,t){let n=this.#t.get(e);if(!n){n=`Type${this.#t.size+1}`,this.#t.set(e,n);let r=t();this.#e.push(e=>`type ${n} = ${F(r,e)};`)}return j(n)}constructor({routing:e,config:t,brandHandling:n,variant:r=`client`,clientClassName:i=`Client`,subscriptionClassName:a=`Subscription`,serverUrl:o=`https://example.com`,noBodySchema:c=v.undefined(),hasHeadMethod:m=!0,hasCredentials:h=!1}){super(o);let g={makeAlias:this.#r.bind(this)},_={brandHandling:n,ctx:{...g,isResponse:!1}},y={brandHandling:n,ctx:{...g,isResponse:!0}},b=!1,x=(e,t,n)=>{let r=d.bind(null,e,t),{isDeprecated:i,inputSchema:a,tags:o}=n,l=`${e} ${t}`,p=r(`input`),m=f(n.security,`cookie`);m.size&&(b=!0);let h=$(a,_);this.#e.push(e=>{let t=F(h,e);return[`/** ${l} */`,`type ${p} = ${m.size?this.makeOmit(t,m,`security cookies`):t};`].join(`
|
|
10
|
+
`)});let g=u.reduce((t,i)=>{let a=n.getResponses(i),o=[];for(let[t,{schema:n,mimeTypes:u,statusCodes:d}]of a.entries()){let a=s(e,u),f=r(i,`variant`,`${t+1}`),p=$(a?n:c,y);this.#e.push(e=>`/** ${l} */\ntype ${f} = ${F(p,e)};`);for(let e of d)o.push(` ${e}: ${f};`)}let u=r(i,`response`,`variants`);return this.#e.push(`/** ${l} */\ninterface ${u} {\n${o.join(`
|
|
11
|
+
`)}\n}`),Object.assign(t,{[i]:u})},{});this.paths.add(t);let v={input:p,positive:this.someOf(g.positive),negative:this.someOf(g.negative),response:`${S.positive}["${l}"] | ${S.negative}["${l}"]`,encoded:`${g.positive} & ${g.negative}`};this.registry.set(l,{isDeprecated:i,store:v}),this.tags.set(l,o)};p({routing:e,config:t,onEndpoint:m?l(x):x}),this.#e.push(this.makeSomeOfType(),this.makePathType(),this.makeMethodType(),...this.makePublicInterfaces(),this.makeRequestType()),r!==`types`&&(this.#e.push(this.makeEndpointTags(),this.makeParseRequestFn(),this.makeSubstituteFn(),this.makeImplementationType(),this.makePaginationType(),this.makeDefaultContextType(),this.makeDefaultImplementation(h&&b),this.makeClientClass(i),this.makeSubscriptionClass(a,h&&b)),this.#n=this.makeUsageStatements(i,a))}print(e){let t=this.#e.map(t=>typeof t==`function`?t(e):t);return this.#n&&t.push(`// Usage example:\n/*\n${this.#n}*/`),t.join(`
|
|
12
12
|
|
|
13
13
|
`)}async printFormatted({printerOptions:e,format:t}={}){let n=t;if(!n){try{let e=g(`prettier`).format;n=t=>e(t,{filepath:`client.ts`})}catch{}try{let e=g(`oxfmt`).format;n=async t=>{let{code:n,errors:r}=await e(`client.ts`,t);if(r.length)throw Error(`OxFmt failed to format the code`,{cause:r});return n}}catch{}}this.#n&&n&&(this.#n=await n(this.#n));let r=this.print(e);return n?n(r):r}};export{ce as Integration};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{T as e}from"./routing-walker-
|
|
1
|
+
import{T as e}from"./routing-walker-tYoo7Yh5.js";import{createRequire as t}from"node:module";let n;const r=(r,i=`default`)=>{try{let e=(n??=t(import.meta.url))(r);return i==="default"?e.default===void 0?e:e.default:e[i]}catch{throw new e(r)}};export{r as t};
|
|
@@ -22,6 +22,7 @@ interface ApiResponse<S extends z.ZodType> {
|
|
|
22
22
|
/**
|
|
23
23
|
* @desc The status code(s) for this response.
|
|
24
24
|
* @default 200 for a positive response, 400 for a negative one
|
|
25
|
+
* @see BuildProps#statusCode - for Endpoint-specific overrides
|
|
25
26
|
* */
|
|
26
27
|
statusCode?: number | [number, ...number[]];
|
|
27
28
|
/**
|
|
@@ -588,6 +589,7 @@ declare class Middleware<
|
|
|
588
589
|
constructor({
|
|
589
590
|
input,
|
|
590
591
|
security,
|
|
592
|
+
statusCode,
|
|
591
593
|
handler,
|
|
592
594
|
}: {
|
|
593
595
|
/**
|
|
@@ -601,6 +603,8 @@ declare class Middleware<
|
|
|
601
603
|
* @see Documentation
|
|
602
604
|
* */
|
|
603
605
|
security?: LogicalContainer<Security<Extract<keyof z.input<IN>, string>, SCO>>;
|
|
606
|
+
/** @desc The status code(s) the Middleware may interrupt the request handling (used by Documentation). */
|
|
607
|
+
statusCode?: number | [number, ...number[]];
|
|
604
608
|
/** @desc The handler returning a context available to Endpoints. */
|
|
605
609
|
handler: Handler$1<z.output<IN>, CTX, RET>;
|
|
606
610
|
});
|
|
@@ -631,11 +635,14 @@ declare class ExpressMiddleware<R extends Request, S extends Response, RET exten
|
|
|
631
635
|
{
|
|
632
636
|
provider,
|
|
633
637
|
transformer,
|
|
638
|
+
statusCode,
|
|
634
639
|
}?: {
|
|
635
640
|
/** @desc Extracts context properties from request and response after the native middleware execution. */
|
|
636
641
|
provider?: (request: R, response: S) => RET | Promise<RET>;
|
|
637
642
|
/** @desc Transforms errors caught from the native middleware before they propagate further. */
|
|
638
643
|
transformer?: (err: Error) => Error;
|
|
644
|
+
/** @desc The status code(s) the Middleware may interrupt the request handling (used by Documentation). */
|
|
645
|
+
statusCode?: number | [number, ...number[]];
|
|
639
646
|
},
|
|
640
647
|
);
|
|
641
648
|
}
|
|
@@ -673,6 +680,7 @@ declare class Endpoint<IN extends IOSchema, OUT extends IOSchema, CTX extends Fl
|
|
|
673
680
|
methods?: Method[];
|
|
674
681
|
scopes?: string[];
|
|
675
682
|
tags?: string[];
|
|
683
|
+
statusCodes: ReadonlySet<number>;
|
|
676
684
|
});
|
|
677
685
|
deprecated(): this;
|
|
678
686
|
execute({
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{globalRegistry as e,z as t}from"zod";import*as n from"ramda";import{isPromise as r}from"node:util/types";import i,{isHttpError as a}from"http-errors";import o from"express";const s={json:`application/json`,upload:`multipart/form-data`,raw:`application/octet-stream`,sse:`text/event-stream`,form:`application/x-www-form-urlencoded`},c=[`get`,`post`,`put`,`delete`,`patch`,`query`],l=[...c,`head`],u=e=>c.includes(e),d=t.object({}),f=/:([A-Za-z0-9_]+)/g,ee=e=>e.match(f)?.map(e=>e.slice(1))||[],p=(e,t=1)=>e.replace(f,()=>`:${t++}`),te=e=>{let t=(e.header(`content-type`)||``).toLowerCase().startsWith(s.upload);return`files`in e&&t},ne={get:[`query`,`params`],post:[`body`,`params`,`files`],put:[`body`,`params`],patch:[`body`,`params`],delete:[`query`,`params`],query:[`query`,`body`,`params`]},re=[`body`,`query`,`params`],m=e=>e.method.toLowerCase(),h=(e,t={})=>{if(e===`options`)return[];let n=e===`head`?`get`:u(e)?e:void 0;return(n?t[n]||ne[n]:void 0)||re},g=(e,t={})=>{let n=m(e);return h(n,t).filter(t=>t!==`files`||te(e)).reduce((t,n)=>Object.assign(t,e[n]),{})},_=e=>e instanceof Error?e:e instanceof t.ZodError?new t.ZodRealError(e.issues):Error(String(e)),v=e=>e instanceof t.ZodError?e.issues.map(({path:e,message:n})=>`${e.length?`${t.core.toDotPath(e)}: `:``}${n}`).join(`; `):e.message,y=(e,t)=>S(e)&&`_zod`in e&&(!t||n.path([`_zod`,`def`,`type`],e)===t),b=(e,t,r)=>e.length&&t.length?n.xprod(e,t).map(r):e.concat(t),x=e=>e.charAt(0).toUpperCase()+e.slice(1).toLowerCase(),ie=(...e)=>{let t=n.chain(e=>e.split(/[^A-Z0-9]/gi),e);return n.chain(e=>e.replaceAll(/[A-Z]+/g,e=>`/${e}`).split(`/`),t).map(x).join(``)},ae=n.tryCatch((e,n)=>typeof t.parse(e,n),n.always(void 0)),S=e=>typeof e==`object`&&!!e,C={_cache:void 0,get env(){return C._cache??=process.env.NODE_ENV??`development`},get isProduction(){return C.env===`production`}},oe=(e,t)=>!!t&&e!==`head`,w=`x-brand`,T=t=>{let{[w]:n}=e.get(t)||{};if(typeof n==`symbol`||typeof n==`string`||typeof n==`number`)return n},E=t=>{let{examples:n}=e.get(t)||{};return Array.isArray(n)?n:[]},D=Symbol(`Buffer`),O=()=>t.custom(e=>Buffer.isBuffer(e),{error:`Expected Buffer`}).meta({[w]:D}),k=Symbol(`DateIn`),se=({examples:e,...n}={})=>t.union([t.iso.date(),t.iso.datetime({local:!0,offset:!0})]).meta({examples:e}).transform(e=>new Date(e)).pipe(t.date()).meta({...n,[w]:k}),A=Symbol(`DateOut`),ce=(e={})=>t.date().transform(e=>e.toISOString()).pipe(t.iso.datetime()).meta({...e,[w]:A});var j=class extends Error{name=`RoutingError`;cause;constructor(e,t,n){super(e),this.cause={method:t,path:n}}},le=class extends Error{name=`DocumentationError`;cause;constructor(e,{method:t,path:n,isResponse:r}){super(e),this.cause=`${r?`Response`:`Input`} schema of an Endpoint assigned to ${t.toUpperCase()} method of ${n} path.`}},M=class extends Error{name=`IOSchemaError`},ue=class extends M{cause;name=`DeepCheckError`;constructor(e){super(`Found`,{cause:e}),this.cause=e}},N=class extends M{cause;name=`OutputValidationError`;constructor(e){let n=new t.ZodError(e.issues.map(({path:e,...t})=>({...t,path:[`output`,...e]})));super(v(n),{cause:e}),this.cause=e}},P=class extends M{cause;name=`InputValidationError`;constructor(e){super(v(e),{cause:e}),this.cause=e}},F=class extends Error{cause;handled;name=`ResultHandlerError`;constructor(e,t){super(v(e),{cause:e}),this.cause=e,this.handled=t}},de=class extends Error{name=`MissingPeerError`;constructor(e){super(`Missing peer dependency: ${e}. Please install it to use the feature.`)}};const I=Symbol(`Form`),fe=e=>(e instanceof t.ZodObject?e:t.object(e)).meta({[w]:I}),L=Symbol(`Upload`),pe=e=>S(e)&&`name`in e&&`encoding`in e&&`mimetype`in e&&`data`in e&&`tempFilePath`in e&&`truncated`in e&&`size`in e&&`md5`in e&&`mv`in e,me=()=>t.custom(e=>pe(e)&&typeof e.name==`string`&&typeof e.encoding==`string`&&typeof e.mimetype==`string`&&Buffer.isBuffer(e.data)&&typeof e.tempFilePath==`string`&&typeof e.truncated==`boolean`&&typeof e.size==`number`&&typeof e.md5==`string`&&typeof e.mv==`function`,{error:({input:e})=>({message:`Expected file upload, received ${typeof e}`})}).meta({[w]:L}),R=Symbol(`Raw`),z=t.object({raw:O()}),he=e=>z.extend(e).meta({[w]:R});function ge(e){return e?he(e):z.meta({[w]:R})}const B=(e,{io:r,condition:i})=>n.tryCatch(()=>void t.toJSONSchema(e,{io:r,unrepresentable:`any`,override:({zodSchema:e})=>{if(i(e))throw new ue(e)}}),e=>e.cause)(),_e=(e,{io:r})=>{let i=[t.toJSONSchema(e,{io:r,unrepresentable:`any`})];for(let e=0;e<i.length;e++){let t=i[e];if(n.is(Object,t)){if(t.$ref===`#`)return!0;i.push(...n.values(t))}n.is(Array,t)&&i.push(...n.values(t))}return!1},ve=e=>B(e,{condition:e=>{let t=T(e);return typeof t==`symbol`&&[L,R,I].includes(t)},io:`input`}),ye=new Set([`nan`,`symbol`,`map`,`set`,`bigint`,`void`,`promise`,`never`,`function`]),be=(e,t)=>B(e,{io:t,condition:e=>{let n=T(e),{type:r}=e._zod.def;return!!(ye.has(r)||n===D||t===`input`&&(r===`date`||n===A)||t===`output`&&(n===k||n===R||n===L))}});var xe=class{},V=class extends xe{#e;#t;#n;#r;constructor({input:e,security:t,statusCode:n,handler:r}){super(),this.#e=e,this.#t=t,this.#n=typeof n==`number`?[n]:n?.slice()??[],this.#r=r}get security(){return this.#t}get schema(){return this.#e}get statusCodes(){return Object.freeze(new Set(this.#n))}async execute({input:e,...n}){try{let t=await(this.#e||d).parseAsync(e);return this.#r({...n,input:t})}catch(e){throw e instanceof t.ZodError?new P(e):e}}},H=class extends V{constructor(e,{provider:t=()=>({}),transformer:n=e=>e,statusCode:i}={}){super({statusCode:i,handler:async({request:i,response:a})=>{let{promise:o,resolve:s,reject:c}=Promise.withResolvers(),l=e=>{if(e&&e instanceof Error)return c(n(e));s(t(i,a))},u=e(i,a,l);return r(u)&&u.catch(l),o}})}};const U={positive:200,negative:400},Se=Object.keys(U),W=e=>e<400;function Ce(e){return e instanceof t.ZodType?{schema:e}:e}const G=(e,{variant:r,args:i})=>{typeof e==`function`&&(e=e(...i));let a={statusCodes:[U[r]],mimeTypes:[s.json]};if(e instanceof t.ZodType)return[{schema:e,...a}];if(Array.isArray(e)&&!e.length)throw new F(Error(`At least one ${r} response schema required.`));let o=(Array.isArray(e)?e:[e]).map(({schema:e,statusCode:t,mimeType:n})=>({schema:e,statusCodes:typeof t==`number`?[t]:t||a.statusCodes,mimeTypes:typeof n==`string`?[n]:n===void 0?a.mimeTypes:n})),c=n.chain(n.prop(`statusCodes`),o),l=c.find(e=>W(e)===(r===`negative`));if(l!==void 0)throw new F(Error(`The status code ${l} is not valid for a ${r} API response.`));if(o.length>1){let e=n.find(e=>c.indexOf(e)!==c.lastIndexOf(e),c);if(e!==void 0)throw new F(Error(`The status code ${e} is used by multiple response schemas.`))}return o},we=(e,t,n)=>{let r=new Set(Array.from(t).filter(e=>W(e)===(n===`positive`)));if(!r.size)return e;if(e.length===1)return e.map(e=>({...e,statusCodes:Array.from(r)}));let i=[],a=e.reduce((e,{statusCodes:t,...n})=>{let a=new Set(t).intersection(r);return a.size?(i.push({...n,statusCodes:Array.from(a)}),e.difference(a)):e},new Set(r));if(a.size){let e=Array.from(a).join(`, `),t=a.size>1;throw new F(Error(`The ResultHandler defines multiple ${n} response schemas, but the overriding status code${t?`s`:``} ${e} of the Endpoint ${t?`are`:`is`} not listed for any of them, therefore it is unclear how such override${t?`s`:``} would be handled. Consider adding ${t?`them`:`it`} to ResultHandler.`))}return i},Te=(e,t,{url:n},r)=>!e.expose&&t.error(`Server side error`,{error:e,url:n,payload:r}),Ee=e=>a(e)?e:i(e instanceof P?400:500,v(e),{cause:e.cause||e}),De=e=>C.isProduction&&!e.expose?i(e.statusCode).message:e.message,Oe=e=>Object.entries(e._zod.def.shape).reduce((e,[t,r])=>b(e,E(r).map(n.objOf(t)),([e,t])=>({...e,...t})),[]);var K=class{nest(e){return{...e,"":this}}},ke=class r extends K{#e;#t;#n=n.once(()=>{if(E(this.#e.outputSchema).length||!y(this.#e.outputSchema,`object`))return;let t=Oe(this.#e.outputSchema);if(!t.length)return;let n=this.#e.outputSchema.meta();e.remove(this.#e.outputSchema).add(this.#e.outputSchema,{...n,examples:t})});constructor(e){super(),this.#e=e}#r(e){return new r({...this.#e,...e})}deprecated(){return this.#r({deprecated:!0})}get isDeprecated(){return this.#e.deprecated||!1}get description(){return this.#e.description}get summary(){return this.#e.summary}get methods(){return Object.freeze(this.#e.methods)}get inputSchema(){return this.#e.inputSchema}get outputSchema(){return this.#e.outputSchema}getProbableRequestType(e){return e===`query`?`form`:this.#t??=(()=>{let e=ve(this.#e.inputSchema);if(e){let t=T(e);if(t===L)return`upload`;if(t===R)return`raw`;if(t===I)return`form`}return`json`})()}getResponses(e){e===`positive`&&this.#n();let t=e===`negative`?this.#e.resultHandler.getNegativeResponse():this.#e.resultHandler.getPositiveResponse(this.#e.outputSchema);return Object.freeze(this.#e.statusCodes.size?we(t,this.#e.statusCodes,e):t)}get security(){let e=n.pluck(`security`,this.#e.middlewares||[]);return n.reject(n.isNil,e)}get scopes(){return Object.freeze(this.#e.scopes||[])}get tags(){return Object.freeze(this.#e.tags||[])}getOperationId(e){return this.#e.getOperationId?.(e)}async#i(e){try{return await this.#e.outputSchema.parseAsync(e)}catch(e){throw e instanceof t.ZodError?new N(e):e}}async#a({method:e,logger:t,ctx:n,response:r,...i}){for(let a of this.#e.middlewares||[])if(!(e===`options`&&!(a instanceof H))&&(Object.assign(n,await a.execute({...i,ctx:n,response:r,logger:t})),r.writableEnded)){t.warn(`A middleware has closed the stream. Accumulated context:`,n);break}}async#o({input:e,...n}){let r;try{r=await this.#e.inputSchema.parseAsync(e)}catch(e){throw e instanceof t.ZodError?new P(e):e}return this.#e.handler({...n,input:r})}async#s(e){await this.#e.resultHandler.execute(e)}async execute({request:e,response:t,logger:n,config:r}){let i=m(e),a={},o,s=g(e,r.inputSources);try{if(await this.#a({method:i,input:s,request:e,response:t,logger:n,ctx:a}),t.writableEnded)return;if(i===`options`)return void t.status(200).end();let r=await this.#o({input:s,logger:n,ctx:a});if(t.writableEnded)return;o={output:await this.#i(r),error:null}}catch(e){o={output:null,error:_(e)}}await this.#s({...o,input:s,request:e,response:t,logger:n,ctx:a})}},q=class{#e;constructor(...e){this.#e=e}apply(e,t){return t(e,o.static(...this.#e))}};const J=e=>S(e)&&`or`in e,Y=e=>S(e)&&`and`in e,X=e=>!Y(e)&&!J(e),Ae=e=>{let t=n.filter(X,e),r=n.chain(n.prop(`and`),n.filter(Y,e)),[i,a]=n.partition(X,r),o=n.concat(t,i),s=n.filter(J,e);return n.map(n.prop(`or`),n.concat(s,a)).reduce((e,t)=>b(e,n.map(e=>X(e)?[e]:e.and,t),([e,t])=>n.concat(e,t)),n.reject(n.isEmpty,[o]))},Z=(e,t)=>Y(e)?n.chain(e=>Z(e,t),e.and):J(e)?n.chain(e=>Z(e,t),e.or):e.type===t?[e.name]:[],je=(e,t)=>new Set(n.chain(e=>Z(e,t),e)),Me=e=>(t,...n)=>{e(t,...n),t===`get`&&e(`head`,...n)},Ne=e=>{let[t,n]=e.trim().split(/ (.+)/,2);return n&&u(t)?[n,t]:[e]},Pe=e=>e.trim().split(`/`).filter(Boolean).join(`/`),Q=({recognizeMethodDependentRoutes:e=!0},t,n)=>Object.entries(t).map(([t,r])=>{let[i,a]=e&&u(t)&&r instanceof K?[`/`,t]:Ne(t);return[[n||``].concat(Pe(i)||[]).join(`/`)||`/`,r,a]}),Fe=(e,t)=>{throw new j(`Route with explicit method can only be assigned with Endpoint`,e,t)},Ie=(e,t,n)=>{if(!(!n||n.includes(e)))throw new j(`Method ${e} is not supported by the assigned Endpoint.`,e,t)},$=(e,t,n)=>{let r=t.includes(`:`)?p(t):t,i=`${e} ${r}`;if(n.has(i))throw new j(`Route has a duplicate: the normalized path "${r}" is already registered`,e,t);n.add(i)},Le=({routing:e,config:t,onEndpoint:n,onStatic:r})=>{let i=Q(t,e),a=new Set;for(let e=0;e<i.length;e++){let[o,s,c]=i[e];if(s instanceof K)if(c)$(c,o,a),Ie(c,o,s.methods),n(c,o,s);else{let{methods:e=[`get`]}=s;for(let t of e)$(t,o,a),n(t,o,s)}else c&&Fe(c,o),s instanceof q?r&&s.apply(o,r):i.splice(e+1,0,...Q(t,s,o))}};export{l as $,A,g as B,le as C,F as D,N as E,T as F,S as G,v as H,E as I,p as J,y as K,b as L,k as M,O as N,j as O,D as P,x as Q,d as R,fe as S,de as T,ee as U,h as V,ae as W,C as X,f as Y,oe as Z,_e as _,q as a,L as b,De as c,Ce as d,u as et,U as f,be as g,V as h,Ae as i,se as j,ce as k,Te as l,H as m,Me as n,ke as o,Se as p,ie as q,je as r,Ee as s,Le as t,s as tt,G as u,R as v,P as w,me as x,ge as y,_ as z};
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{globalRegistry as e,z as t}from"zod";import*as n from"ramda";import{isPromise as r}from"node:util/types";import i,{isHttpError as a}from"http-errors";import o from"express";const s={json:`application/json`,upload:`multipart/form-data`,raw:`application/octet-stream`,sse:`text/event-stream`,form:`application/x-www-form-urlencoded`},c=[`get`,`post`,`put`,`delete`,`patch`,`query`],l=[...c,`head`],u=e=>c.includes(e),d=t.object({}),f=/:([A-Za-z0-9_]+)/g,ee=e=>e.match(f)?.map(e=>e.slice(1))||[],p=(e,t=1)=>e.replace(f,()=>`:${t++}`),te=e=>{let t=(e.header(`content-type`)||``).toLowerCase().startsWith(s.upload);return`files`in e&&t},ne={get:[`query`,`params`],post:[`body`,`params`,`files`],put:[`body`,`params`],patch:[`body`,`params`],delete:[`query`,`params`],query:[`query`,`body`,`params`]},m=[`body`,`query`,`params`],h=e=>e.method.toLowerCase(),g=(e,t={})=>{if(e===`options`)return[];let n=e===`head`?`get`:u(e)?e:void 0;return(n?t[n]||ne[n]:void 0)||m},_=(e,t={})=>{let n=h(e);return g(n,t).filter(t=>t!==`files`||te(e)).reduce((t,n)=>Object.assign(t,e[n]),{})},v=e=>e instanceof Error?e:e instanceof t.ZodError?new t.ZodRealError(e.issues):Error(String(e)),y=e=>e instanceof t.ZodError?e.issues.map(({path:e,message:n})=>`${e.length?`${t.core.toDotPath(e)}: `:``}${n}`).join(`; `):e.message,b=(e,t)=>C(e)&&`_zod`in e&&(!t||n.path([`_zod`,`def`,`type`],e)===t),x=(e,t,r)=>e.length&&t.length?n.xprod(e,t).map(r):e.concat(t),S=e=>e.charAt(0).toUpperCase()+e.slice(1).toLowerCase(),re=(...e)=>{let t=n.chain(e=>e.split(/[^A-Z0-9]/gi),e);return n.chain(e=>e.replaceAll(/[A-Z]+/g,e=>`/${e}`).split(`/`),t).map(S).join(``)},ie=n.tryCatch((e,n)=>typeof t.parse(e,n),n.always(void 0)),C=e=>typeof e==`object`&&!!e,w={_cache:void 0,get env(){return w._cache??=process.env.NODE_ENV??`development`},get isProduction(){return w.env===`production`}},ae=(e,t)=>!!t&&e!==`head`,T=`x-brand`,E=t=>{let{[T]:n}=e.get(t)||{};if(typeof n==`symbol`||typeof n==`string`||typeof n==`number`)return n},D=t=>{let{examples:n}=e.get(t)||{};return Array.isArray(n)?n:[]},O=Symbol(`Buffer`),k=()=>t.custom(e=>Buffer.isBuffer(e),{error:`Expected Buffer`}).meta({[T]:O}),A=Symbol(`DateIn`),oe=({examples:e,...n}={})=>t.union([t.iso.date(),t.iso.datetime({local:!0,offset:!0})]).meta({examples:e}).transform(e=>new Date(e)).pipe(t.date()).meta({...n,[T]:A}),j=Symbol(`DateOut`),M=(e={})=>t.date().transform(e=>e.toISOString()).pipe(t.iso.datetime()).meta({...e,[T]:j});var N=class extends Error{name=`RoutingError`;cause;constructor(e,t,n){super(e),this.cause={method:t,path:n}}},se=class extends Error{name=`DocumentationError`;cause;constructor(e,{method:t,path:n,isResponse:r}){super(e),this.cause=`${r?`Response`:`Input`} schema of an Endpoint assigned to ${t.toUpperCase()} method of ${n} path.`}},P=class extends Error{name=`IOSchemaError`},ce=class extends P{cause;name=`DeepCheckError`;constructor(e){super(`Found`,{cause:e}),this.cause=e}},F=class extends P{cause;name=`OutputValidationError`;constructor(e){let n=new t.ZodError(e.issues.map(({path:e,...t})=>({...t,path:[`output`,...e]})));super(y(n),{cause:e}),this.cause=e}},I=class extends P{cause;name=`InputValidationError`;constructor(e){super(y(e),{cause:e}),this.cause=e}},L=class extends Error{cause;handled;name=`ResultHandlerError`;constructor(e,t){super(y(e),{cause:e}),this.cause=e,this.handled=t}},le=class extends Error{name=`MissingPeerError`;constructor(e){super(`Missing peer dependency: ${e}. Please install it to use the feature.`)}};const R=Symbol(`Form`),ue=e=>(e instanceof t.ZodObject?e:t.object(e)).meta({[T]:R}),z=Symbol(`Upload`),de=e=>C(e)&&`name`in e&&`encoding`in e&&`mimetype`in e&&`data`in e&&`tempFilePath`in e&&`truncated`in e&&`size`in e&&`md5`in e&&`mv`in e,fe=()=>t.custom(e=>de(e)&&typeof e.name==`string`&&typeof e.encoding==`string`&&typeof e.mimetype==`string`&&Buffer.isBuffer(e.data)&&typeof e.tempFilePath==`string`&&typeof e.truncated==`boolean`&&typeof e.size==`number`&&typeof e.md5==`string`&&typeof e.mv==`function`,{error:({input:e})=>({message:`Expected file upload, received ${typeof e}`})}).meta({[T]:z}),B=Symbol(`Raw`),V=t.object({raw:k()}),pe=e=>V.extend(e).meta({[T]:B});function me(e){return e?pe(e):V.meta({[T]:B})}const H=(e,{io:r,condition:i})=>n.tryCatch(()=>void t.toJSONSchema(e,{io:r,unrepresentable:`any`,override:({zodSchema:e})=>{if(i(e))throw new ce(e)}}),e=>e.cause)(),he=(e,{io:r})=>{let i=[t.toJSONSchema(e,{io:r,unrepresentable:`any`})];for(let e=0;e<i.length;e++){let t=i[e];if(n.is(Object,t)){if(t.$ref===`#`)return!0;i.push(...n.values(t))}n.is(Array,t)&&i.push(...n.values(t))}return!1},ge=e=>H(e,{condition:e=>{let t=E(e);return typeof t==`symbol`&&[z,B,R].includes(t)},io:`input`}),_e=new Set([`nan`,`symbol`,`map`,`set`,`bigint`,`void`,`promise`,`never`,`function`]),ve=(e,t)=>H(e,{io:t,condition:e=>{let n=E(e),{type:r}=e._zod.def;return!!(_e.has(r)||n===O||t===`input`&&(r===`date`||n===j)||t===`output`&&(n===A||n===B||n===z))}});var ye=class{},U=class extends ye{#e;#t;#n;constructor({input:e,security:t,handler:n}){super(),this.#e=e,this.#t=t,this.#n=n}get security(){return this.#t}get schema(){return this.#e}async execute({input:e,...n}){try{let t=await(this.#e||d).parseAsync(e);return this.#n({...n,input:t})}catch(e){throw e instanceof t.ZodError?new I(e):e}}},W=class extends U{constructor(e,{provider:t=()=>({}),transformer:n=e=>e}={}){super({handler:async({request:i,response:a})=>{let{promise:o,resolve:s,reject:c}=Promise.withResolvers(),l=e=>{if(e&&e instanceof Error)return c(n(e));s(t(i,a))},u=e(i,a,l);return r(u)&&u.catch(l),o}})}};const be=(e,{variant:r,args:i,...a})=>{if(typeof e==`function`&&(e=e(...i)),e instanceof t.ZodType)return[{schema:e,...a}];if(Array.isArray(e)&&!e.length)throw new L(Error(`At least one ${r} response schema required.`));let o=(Array.isArray(e)?e:[e]).map(({schema:e,statusCode:t,mimeType:n})=>({schema:e,statusCodes:typeof t==`number`?[t]:t||a.statusCodes,mimeTypes:typeof n==`string`?[n]:n===void 0?a.mimeTypes:n}));if(o.length>1){let e=n.chain(n.prop(`statusCodes`),o),t=n.find(t=>e.indexOf(t)!==e.lastIndexOf(t),e);if(t!==void 0)throw new L(Error(`The status code ${t} is used by multiple response schemas.`))}return o},xe=(e,t,{url:n},r)=>!e.expose&&t.error(`Server side error`,{error:e,url:n,payload:r}),Se=e=>a(e)?e:i(e instanceof I?400:500,y(e),{cause:e.cause||e}),Ce=e=>w.isProduction&&!e.expose?i(e.statusCode).message:e.message,we=e=>Object.entries(e._zod.def.shape).reduce((e,[t,r])=>x(e,D(r).map(n.objOf(t)),([e,t])=>({...e,...t})),[]);var G=class{nest(e){return{...e,"":this}}},Te=class r extends G{#e;#t;#n=n.once(()=>{if(D(this.#e.outputSchema).length||!b(this.#e.outputSchema,`object`))return;let t=we(this.#e.outputSchema);if(!t.length)return;let n=this.#e.outputSchema.meta();e.remove(this.#e.outputSchema).add(this.#e.outputSchema,{...n,examples:t})});constructor(e){super(),this.#e=e}#r(e){return new r({...this.#e,...e})}deprecated(){return this.#r({deprecated:!0})}get isDeprecated(){return this.#e.deprecated||!1}get description(){return this.#e.description}get summary(){return this.#e.summary}get methods(){return Object.freeze(this.#e.methods)}get inputSchema(){return this.#e.inputSchema}get outputSchema(){return this.#e.outputSchema}getProbableRequestType(e){return e===`query`?`form`:this.#t??=(()=>{let e=ge(this.#e.inputSchema);if(e){let t=E(e);if(t===z)return`upload`;if(t===B)return`raw`;if(t===R)return`form`}return`json`})()}getResponses(e){return e===`positive`&&this.#n(),Object.freeze(e===`negative`?this.#e.resultHandler.getNegativeResponse():this.#e.resultHandler.getPositiveResponse(this.#e.outputSchema))}get security(){let e=n.pluck(`security`,this.#e.middlewares||[]);return n.reject(n.isNil,e)}get scopes(){return Object.freeze(this.#e.scopes||[])}get tags(){return Object.freeze(this.#e.tags||[])}getOperationId(e){return this.#e.getOperationId?.(e)}async#i(e){try{return await this.#e.outputSchema.parseAsync(e)}catch(e){throw e instanceof t.ZodError?new F(e):e}}async#a({method:e,logger:t,ctx:n,response:r,...i}){for(let a of this.#e.middlewares||[])if(!(e===`options`&&!(a instanceof W))&&(Object.assign(n,await a.execute({...i,ctx:n,response:r,logger:t})),r.writableEnded)){t.warn(`A middleware has closed the stream. Accumulated context:`,n);break}}async#o({input:e,...n}){let r;try{r=await this.#e.inputSchema.parseAsync(e)}catch(e){throw e instanceof t.ZodError?new I(e):e}return this.#e.handler({...n,input:r})}async#s(e){await this.#e.resultHandler.execute(e)}async execute({request:e,response:t,logger:n,config:r}){let i=h(e),a={},o,s=_(e,r.inputSources);try{if(await this.#a({method:i,input:s,request:e,response:t,logger:n,ctx:a}),t.writableEnded)return;if(i===`options`)return void t.status(200).end();let r=await this.#o({input:s,logger:n,ctx:a});if(t.writableEnded)return;o={output:await this.#i(r),error:null}}catch(e){o={output:null,error:v(e)}}await this.#s({...o,input:s,request:e,response:t,logger:n,ctx:a})}};const K={positive:200,negative:400},Ee=Object.keys(K);function De(e){return e instanceof t.ZodType?{schema:e}:e}var q=class{#e;constructor(...e){this.#e=e}apply(e,t){return t(e,o.static(...this.#e))}};const J=e=>C(e)&&`or`in e,Y=e=>C(e)&&`and`in e,X=e=>!Y(e)&&!J(e),Oe=e=>{let t=n.filter(X,e),r=n.chain(n.prop(`and`),n.filter(Y,e)),[i,a]=n.partition(X,r),o=n.concat(t,i),s=n.filter(J,e);return n.map(n.prop(`or`),n.concat(s,a)).reduce((e,t)=>x(e,n.map(e=>X(e)?[e]:e.and,t),([e,t])=>n.concat(e,t)),n.reject(n.isEmpty,[o]))},Z=(e,t)=>Y(e)?n.chain(e=>Z(e,t),e.and):J(e)?n.chain(e=>Z(e,t),e.or):e.type===t?[e.name]:[],ke=(e,t)=>new Set(n.chain(e=>Z(e,t),e)),Ae=e=>(t,...n)=>{e(t,...n),t===`get`&&e(`head`,...n)},je=e=>{let[t,n]=e.trim().split(/ (.+)/,2);return n&&u(t)?[n,t]:[e]},Me=e=>e.trim().split(`/`).filter(Boolean).join(`/`),Q=({recognizeMethodDependentRoutes:e=!0},t,n)=>Object.entries(t).map(([t,r])=>{let[i,a]=e&&u(t)&&r instanceof G?[`/`,t]:je(t);return[[n||``].concat(Me(i)||[]).join(`/`)||`/`,r,a]}),Ne=(e,t)=>{throw new N(`Route with explicit method can only be assigned with Endpoint`,e,t)},Pe=(e,t,n)=>{if(!(!n||n.includes(e)))throw new N(`Method ${e} is not supported by the assigned Endpoint.`,e,t)},$=(e,t,n)=>{let r=t.includes(`:`)?p(t):t,i=`${e} ${r}`;if(n.has(i))throw new N(`Route has a duplicate: the normalized path "${r}" is already registered`,e,t);n.add(i)},Fe=({routing:e,config:t,onEndpoint:n,onStatic:r})=>{let i=Q(t,e),a=new Set;for(let e=0;e<i.length;e++){let[o,s,c]=i[e];if(s instanceof G)if(c)$(c,o,a),Pe(c,o,s.methods),n(c,o,s);else{let{methods:e=[`get`]}=s;for(let t of e)$(t,o,a),n(t,o,s)}else c&&Ne(c,o),s instanceof q?r&&s.apply(o,r):i.splice(e+1,0,...Q(t,s,o))}};export{l as $,j as A,_ as B,se as C,L as D,F as E,E as F,C as G,y as H,D as I,p as J,b as K,x as L,A as M,k as N,N as O,O as P,S as Q,d as R,ue as S,le as T,ee as U,g as V,ie as W,w as X,f as Y,ae as Z,he as _,q as a,z as b,Ee as c,Ce as d,u as et,xe as f,ve as g,U as h,Oe as i,oe as j,M as k,Te as l,W as m,Ae as n,De as o,be as p,re as q,ke as r,K as s,Fe as t,s as tt,Se as u,B as v,I as w,fe as x,me as y,v as z};
|