resora 0.2.10 → 0.2.12
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/bin/index.mjs +1 -1
- package/dist/index.cjs +4 -4
- package/dist/index.d.cts +76 -7
- package/dist/index.d.mts +76 -7
- package/dist/index.mjs +4 -4
- package/package.json +1 -1
package/bin/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{copyFileSync as e,existsSync as t,mkdirSync as n,readFileSync as r,rmSync as i,writeFileSync as a}from"fs";import o,{dirname as s,join as c}from"path";import{createRequire as l}from"module";import{pathToFileURL as u}from"url";import{Command as d,Kernel as f}from"@h3ravel/musket";let p={first:`first`,last:`last`,prev:`prev`,next:`next`},m={previous:`previous`,next:`next`};const h=e=>{p={...p,...e}},g=e=>{m={...m,...e}};let _=o.resolve(process.cwd(),`node_modules/resora/stubs`);t(_)||(_=o.resolve(process.cwd(),`stubs`));const v=()=>({stubsDir:_,preferredCase:`camel`,responseStructure:{wrap:!0,rootKey:`data`},paginatedExtras:[`meta`,`links`],baseUrl
|
|
2
|
+
import{copyFileSync as e,existsSync as t,mkdirSync as n,readFileSync as r,rmSync as i,writeFileSync as a}from"fs";import o,{dirname as s,join as c}from"path";import{createRequire as l}from"module";import{pathToFileURL as u}from"url";import{Command as d,Kernel as f}from"@h3ravel/musket";let p={first:`first`,last:`last`,prev:`prev`,next:`next`},m={previous:`previous`,next:`next`};const h=e=>{p={...p,...e}},g=e=>{m={...m,...e}};let _=o.resolve(process.cwd(),`node_modules/resora/stubs`);t(_)||(_=o.resolve(process.cwd(),`stubs`));const v=()=>({stubsDir:_,preferredCase:`camel`,responseStructure:{wrap:!0,rootKey:`data`},paginatedExtras:[`meta`,`links`],baseUrl:``,pageName:`page`,paginatedLinks:{first:`first`,last:`last`,prev:`prev`,next:`next`},paginatedMeta:{to:`to`,from:`from`,links:`links`,path:`path`,total:`total`,per_page:`per_page`,last_page:`last_page`,current_page:`current_page`},cursorMeta:{previous:`previous`,next:`next`},resourcesDir:`src/resources`,stubs:{config:`resora.config.stub`,resource:`resource.stub`,collection:`resource.collection.stub`}}),y=e=>{let t=v();return Object.assign(t,e,{stubs:Object.assign(t.stubs,e.stubs||{})},{cursorMeta:Object.assign(t.cursorMeta,e.cursorMeta||{})},{paginatedMeta:e.paginatedMeta||t.paginatedMeta},{paginatedLinks:Object.assign(t.paginatedLinks,e.paginatedLinks||{})},{responseStructure:Object.assign(t.responseStructure,e.responseStructure||{})})};let b=!1,x;const S=e=>{e.preferredCase!==`camel`&&e.preferredCase,e.responseStructure,e.paginatedExtras,h(e.paginatedLinks),e.paginatedMeta,g(e.cursorMeta),e.baseUrl,e.pageName},C=async e=>await import(`${u(e).href}?resora_runtime=${Date.now()}`),w=e=>{S(y((e?.default??e)||{})),b=!0},T=()=>{let e=l(import.meta.url),n=[o.join(process.cwd(),`resora.config.cjs`)];for(let r of n)if(t(r))try{return w(e(r)),!0}catch{continue}return!1};(async()=>{if(!b){if(x)return await x;T()||(x=(async()=>{let e=[o.join(process.cwd(),`resora.config.js`),o.join(process.cwd(),`resora.config.ts`)];for(let n of e)if(t(n))try{w(await C(n));return}catch{continue}b=!0})(),await x)}})();var E=class{command;config={};constructor(e={}){this.config=y(e)}async loadConfig(e={}){this.config=y(e);let n=[c(process.cwd(),`resora.config.ts`),c(process.cwd(),`resora.config.js`),c(process.cwd(),`resora.config.cjs`)];for(let e of n)if(t(e))try{let{default:t}=await import(e);Object.assign(this.config,t);break}catch(t){console.error(`Error loading config file at ${e}:`,t)}return this}getConfig(){return this.config}init(){let n=c(process.cwd(),`resora.config.js`),i=c(this.config.stubsDir,this.config.stubs.config);return t(n)&&!this.command.option(`force`)&&(this.command.error(`Error: ${n} already exists.`),process.exit(1)),this.ensureDirectory(n),t(n)&&this.command.option(`force`)&&e(n,n.replace(/\.js$/,`.backup.${Date.now()}.js`)),a(n,r(i,`utf-8`)),{path:n}}ensureDirectory(e){let r=s(e);t(r)||n(r,{recursive:!0})}generateFile(e,n,o,s){t(n)&&!s?.force?(this.command.error(`Error: ${n} already exists.`),process.exit(1)):t(n)&&s?.force&&i(n);let c=r(e,`utf-8`);for(let[e,t]of Object.entries(o))c=c.replace(RegExp(`{{${e}}}`,`g`),t);return this.ensureDirectory(n),a(n,c),n}makeResource(e,n){let r=e;n?.collection&&!e.endsWith(`Collection`)&&!e.endsWith(`Resource`)?r+=`Collection`:!n?.collection&&!e.endsWith(`Resource`)&&!e.endsWith(`Collection`)&&(r+=`Resource`);let i=`${r}.ts`,a=c(this.config.resourcesDir,i),o=c(this.config.stubsDir,n?.collection||e.endsWith(`Collection`)?this.config.stubs.collection:this.config.stubs.resource);t(o)||(this.command.error(`Error: Stub file ${o} not found.`),process.exit(1)),r=r.split(`/`).pop()?.split(`.`).shift();let s=r.replace(/(Resource|Collection)$/,``)+`Resource`,l=[`/**`,` * The resource that this collection collects.`,` */`,`collects = ${s}`].join(`
|
|
3
3
|
`),u=`import ${s} from './${s}'\n`,d=(!!n?.collection||e.endsWith(`Collection`))&&t(c(this.config.resourcesDir,`${s}.ts`)),f=this.generateFile(o,a,{ResourceName:r,CollectionResourceName:r.replace(/(Resource|Collection)$/,``)+`Resource`,"collects = Resource":d?l:``,"import = Resource":d?u:``},n);return{name:r,path:f}}},D=class extends d{signature=`init
|
|
4
4
|
{--force : Force overwrite if config file already exists (existing file will be backed up) }
|
|
5
5
|
`;description=`Initialize Resora`;async handle(){this.app.command=this,this.app.init(),this.success(`Resora initialized`)}},O=class extends d{signature=`#create:
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`fs`),l=require(`path`);l=s(l);let u=require(`module`),d=require(`url`),f=require(`@h3ravel/musket`);function ee(e){return e}let p,m,h=[`meta`,`links`],g={first:`first`,last:`last`,prev:`prev`,next:`next`},_=`https://localhost`,v=`page`,y={to:`to`,from:`from`,links:`links`,path:`path`,total:`total`,per_page:`per_page`,last_page:`last_page`,current_page:`current_page`},b={previous:`previous`,next:`next`};const x=e=>{p=e},S=()=>p,C=e=>{m=e},w=()=>m,te=e=>{m={...m||{},rootKey:e}},ne=e=>{m={...m||{},wrap:e}},re=()=>m?.wrap,ie=()=>m?.rootKey,ae=e=>{m={...m||{},factory:e}},oe=()=>m?.factory,se=e=>{h=e},ce=()=>h,le=e=>{g={...g,...e}},ue=()=>g,de=e=>{_=e},fe=()=>_,pe=e=>{v=e},me=()=>v,T=e=>{y=e},E=()=>y,D=e=>{b={...b,...e}},O=()=>b,k=e=>{if(typeof e!=`object`||!e||Array.isArray(e)||e instanceof Date||e instanceof RegExp)return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null},A=(e,t,n=`data`)=>!t||Object.keys(t).length===0?e:Array.isArray(e)?{[n]:e,...t}:k(e)?{...e,...t}:{[n]:e,...t},j=(e,t)=>{if(!t)return e;if(!e)return t;let n={...e};for(let[e,r]of Object.entries(t)){let t=n[e];k(t)&&k(r)?n[e]=j(t,r):n[e]=r}return n},M=e=>{if(!e||typeof e!=`object`)return!1;let t=e;return typeof t.toObject==`function`&&typeof t.getRawAttributes==`function`},N=e=>!e||typeof e!=`object`?!1:typeof e.all==`function`,P=e=>{if(Array.isArray(e))return e.map(e=>P(e));if(M(e))return P(e.toObject());if(N(e)){let t=e.all();return Array.isArray(t)?t.map(e=>P(e)):P(t)}return k(e)?Object.entries(e).reduce((e,[t,n])=>(e[t]=P(n),e),{}):e},F=()=>{let e=ce();return Array.isArray(e)?{metaKey:e.includes(`meta`)?`meta`:void 0,linksKey:e.includes(`links`)?`links`:void 0,cursorKey:e.includes(`cursor`)?`cursor`:void 0}:{metaKey:e.meta,linksKey:e.links,cursorKey:e.cursor}},I=(e,t)=>{if(e===void 0)return;let n=t||``,r=fe()||``,i=/^https?:\/\//i.test(n),a=r.replace(/\/$/,``),o=n.replace(/^\//,``),s=i?n:a?o?`${a}/${o}`:a:``;if(!s)return;let c=new URL(s);return c.searchParams.set(me()||`page`,String(e)),c.toString()},L=e=>{let{metaKey:t,linksKey:n,cursorKey:r}=F(),i={},a=!!e&&typeof e==`object`&&!!e.meta&&typeof e.meta==`object`&&(Array.isArray(e.data)||N(e.data)),o=a?{first:typeof e.firstPageUrl==`function`?e.firstPageUrl():void 0,last:typeof e.lastPageUrl==`function`?e.lastPageUrl():void 0,prev:typeof e.previousPageUrl==`function`?e.previousPageUrl():void 0,next:typeof e.nextPageUrl==`function`?e.nextPageUrl():void 0}:void 0,s=o?Object.entries(o).reduce((e,[t,n])=>(n!==void 0&&(e[t]=n),e),{}):void 0,c=e?.pagination||(a?{...e.meta,links:e.links||s}:void 0),l=e?.cursor,u={},d={};if(c){let e={to:c.to,from:c.from,links:c.links,path:c.path,total:c.total,per_page:c.perPage,last_page:c.lastPage,current_page:c.currentPage};for(let[t,n]of Object.entries(E())){if(!n)continue;let r=e[t];r!==void 0&&(u[n]=r)}let t={first:c.links&&Object.prototype.hasOwnProperty.call(c.links,`first`)?c.links.first:I(c.firstPage,c.path),last:c.links&&Object.prototype.hasOwnProperty.call(c.links,`last`)?c.links.last:I(c.lastPage,c.path),prev:c.links&&Object.prototype.hasOwnProperty.call(c.links,`prev`)?c.links.prev:I(c.prevPage,c.path),next:c.links&&Object.prototype.hasOwnProperty.call(c.links,`next`)?c.links.next:I(c.nextPage,c.path)};for(let[e,n]of Object.entries(ue())){if(!n)continue;let r=t[e];r!==void 0&&(d[n]=r)}}if(l){let e={},t={previous:l.previous,next:l.next};for(let[n,r]of Object.entries(O())){if(!r)continue;let i=t[n];i!==void 0&&(e[r]=i)}r&&Object.keys(e).length>0?i[r]=e:Object.keys(e).length>0&&(u.cursor=e)}return t&&Object.keys(u).length>0&&(i[t]=u),n&&Object.keys(d).length>0&&(i[n]=d),i},R=({payload:e,meta:t,metaKey:n=`meta`,wrap:r=!0,rootKey:i=`data`,factory:a,context:o})=>{if(a)return a(e,{...o,rootKey:i,meta:t});if(!r)return t===void 0?e:k(e)?{...e,[n]:t}:{[i]:e,[n]:t};let s={[i]:e};return t!==void 0&&(s[n]=t),s},z=(e,t)=>{let n=e?.with;if(typeof n!=`function`||n===t||n.length>0)return;let r=n.call(e);return k(r)?r:void 0},B=Symbol(`resora.conditional.missing`),V=(e,t)=>e?typeof t==`function`?t():t:B,H=e=>e??B,he=(e,t)=>{if(!e)return{};let n=typeof t==`function`?t():t;return k(n)?n:{}},U=e=>{if(e===B)return B;if(Array.isArray(e))return e.map(e=>U(e)).filter(e=>e!==B);if(k(e)){let t={};for(let[n,r]of Object.entries(e)){let e=U(r);e!==B&&(t[n]=e)}return t}return e},W=e=>e.replace(/([a-z0-9])([A-Z])/g,`$1 $2`).replace(/([A-Z]+)([A-Z][a-z])/g,`$1 $2`).replace(/[-_\s]+/g,` `).trim().toLowerCase().split(` `).filter(Boolean),ge=e=>W(e).map((e,t)=>t===0?e:e.charAt(0).toUpperCase()+e.slice(1)).join(``),_e=e=>W(e).join(`_`),ve=e=>W(e).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``),ye=e=>W(e).join(`-`),G=e=>{if(typeof e==`function`)return e;switch(e){case`camel`:return ge;case`snake`:return _e;case`pascal`:return ve;case`kebab`:return ye}},K=(e,t)=>e==null?e:Array.isArray(e)?e.map(e=>K(e,t)):e instanceof Date||e instanceof RegExp?e:typeof e==`object`?Object.fromEntries(Object.entries(e).map(([e,n])=>[t(e),K(n,t)])):e;let q=l.default.resolve(process.cwd(),`node_modules/resora/stubs`);(0,c.existsSync)(q)||(q=l.default.resolve(process.cwd(),`stubs`));const be=()=>({stubsDir:q,preferredCase:`camel`,responseStructure:{wrap:!0,rootKey:`data`},paginatedExtras:[`meta`,`links`],baseUrl:`https://localhost`,pageName:`page`,paginatedLinks:{first:`first`,last:`last`,prev:`prev`,next:`next`},paginatedMeta:{to:`to`,from:`from`,links:`links`,path:`path`,total:`total`,per_page:`per_page`,last_page:`last_page`,current_page:`current_page`},cursorMeta:{previous:`previous`,next:`next`},resourcesDir:`src/resources`,stubs:{config:`resora.config.stub`,resource:`resource.stub`,collection:`resource.collection.stub`}}),J=e=>{let t=be();return Object.assign(t,e,{stubs:Object.assign(t.stubs,e.stubs||{})},{cursorMeta:Object.assign(t.cursorMeta,e.cursorMeta||{})},{paginatedMeta:e.paginatedMeta||t.paginatedMeta},{paginatedLinks:Object.assign(t.paginatedLinks,e.paginatedLinks||{})},{responseStructure:Object.assign(t.responseStructure,e.responseStructure||{})})};let Y=!1,X;const xe=()=>{Y=!1,X=void 0},Se=e=>{e.preferredCase!==`camel`&&x(e.preferredCase),C(e.responseStructure),se(e.paginatedExtras),le(e.paginatedLinks),T(e.paginatedMeta),D(e.cursorMeta),de(e.baseUrl),pe(e.pageName)},Ce=async e=>await import(`${(0,d.pathToFileURL)(e).href}?resora_runtime=${Date.now()}`),we=e=>{Se(J((e?.default??e)||{})),Y=!0},Te=()=>{let e=(0,u.createRequire)(e(`url`).pathToFileURL(__filename).href),t=[l.default.join(process.cwd(),`resora.config.cjs`)];for(let n of t)if((0,c.existsSync)(n))try{return we(e(n)),!0}catch{continue}return!1},Z=async()=>{if(!Y){if(X)return await X;Te()||(X=(async()=>{let e=[l.default.join(process.cwd(),`resora.config.js`),l.default.join(process.cwd(),`resora.config.ts`)];for(let t of e)if((0,c.existsSync)(t))try{we(await Ce(t));return}catch{continue}Y=!0})(),await X)}};Z();var Ee=class{command;config={};constructor(e={}){this.config=J(e)}async loadConfig(e={}){this.config=J(e);let t=[(0,l.join)(process.cwd(),`resora.config.ts`),(0,l.join)(process.cwd(),`resora.config.js`),(0,l.join)(process.cwd(),`resora.config.cjs`)];for(let e of t)if((0,c.existsSync)(e))try{let{default:t}=await import(e);Object.assign(this.config,t);break}catch(t){console.error(`Error loading config file at ${e}:`,t)}return this}getConfig(){return this.config}init(){let e=(0,l.join)(process.cwd(),`resora.config.js`),t=(0,l.join)(this.config.stubsDir,this.config.stubs.config);return(0,c.existsSync)(e)&&!this.command.option(`force`)&&(this.command.error(`Error: ${e} already exists.`),process.exit(1)),this.ensureDirectory(e),(0,c.existsSync)(e)&&this.command.option(`force`)&&(0,c.copyFileSync)(e,e.replace(/\.js$/,`.backup.${Date.now()}.js`)),(0,c.writeFileSync)(e,(0,c.readFileSync)(t,`utf-8`)),{path:e}}ensureDirectory(e){let t=(0,l.dirname)(e);(0,c.existsSync)(t)||(0,c.mkdirSync)(t,{recursive:!0})}generateFile(e,t,n,r){(0,c.existsSync)(t)&&!r?.force?(this.command.error(`Error: ${t} already exists.`),process.exit(1)):(0,c.existsSync)(t)&&r?.force&&(0,c.rmSync)(t);let i=(0,c.readFileSync)(e,`utf-8`);for(let[e,t]of Object.entries(n))i=i.replace(RegExp(`{{${e}}}`,`g`),t);return this.ensureDirectory(t),(0,c.writeFileSync)(t,i),t}makeResource(e,t){let n=e;t?.collection&&!e.endsWith(`Collection`)&&!e.endsWith(`Resource`)?n+=`Collection`:!t?.collection&&!e.endsWith(`Resource`)&&!e.endsWith(`Collection`)&&(n+=`Resource`);let r=`${n}.ts`,i=(0,l.join)(this.config.resourcesDir,r),a=(0,l.join)(this.config.stubsDir,t?.collection||e.endsWith(`Collection`)?this.config.stubs.collection:this.config.stubs.resource);(0,c.existsSync)(a)||(this.command.error(`Error: Stub file ${a} not found.`),process.exit(1)),n=n.split(`/`).pop()?.split(`.`).shift();let o=n.replace(/(Resource|Collection)$/,``)+`Resource`,s=[`/**`,` * The resource that this collection collects.`,` */`,`collects = ${o}`].join(`
|
|
2
|
-
`),u=`import ${o} from './${o}'\n`,d=(!!t?.collection||e.endsWith(`Collection`))&&(0,c.existsSync)((0,l.join)(this.config.resourcesDir,`${o}.ts`)),f=this.generateFile(a,i,{ResourceName:n,CollectionResourceName:n.replace(/(Resource|Collection)$/,``)+`Resource`,"collects = Resource":d?s:``,"import = Resource":d?u:``},t);return{name:n,path:f}}},
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`fs`),l=require(`path`);l=s(l);let u=require(`module`),d=require(`url`),f=require(`@h3ravel/musket`);function ee(e){return e}let p,m,h=[`meta`,`links`],g={first:`first`,last:`last`,prev:`prev`,next:`next`},_=``,v=`page`,y={to:`to`,from:`from`,links:`links`,path:`path`,total:`total`,per_page:`per_page`,last_page:`last_page`,current_page:`current_page`},b={previous:`previous`,next:`next`};const te=e=>{p=e},ne=()=>p,re=e=>{m=e},ie=()=>m,ae=e=>{m={...m||{},rootKey:e}},oe=e=>{m={...m||{},wrap:e}},se=()=>m?.wrap,ce=()=>m?.rootKey,le=e=>{m={...m||{},factory:e}},ue=()=>m?.factory,de=e=>{h=e},fe=()=>h,pe=e=>{g={...g,...e}},me=()=>g,x=e=>{_=e},he=()=>_,S=e=>{v=e},C=()=>v,ge=e=>{y=e},_e=()=>y,ve=e=>{b={...b,...e}},w=()=>b;let T;const E=e=>{T=e},D=()=>T,O=e=>{if(!e||typeof e!=`object`)return typeof e==`string`?e:void 0;let t=e;return t.req&&typeof t.req==`object`?k(t.req):k(t)},k=e=>{if(typeof e.originalUrl==`string`)return e.originalUrl;if(typeof e.url==`string`)try{let t=new URL(e.url);return t.pathname+t.search}catch{return e.url}},A=e=>{if(!e||typeof e!=`object`)return;let t=e;return t.res&&typeof t.res==`object`?t.res:e},j=e=>{E(O(e))},M=e=>{if(typeof e!=`object`||!e||Array.isArray(e)||e instanceof Date||e instanceof RegExp)return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null},N=(e,t,n=`data`)=>!t||Object.keys(t).length===0?e:Array.isArray(e)?{[n]:e,...t}:M(e)?{...e,...t}:{[n]:e,...t},P=(e,t)=>{if(!t)return e;if(!e)return t;let n={...e};for(let[e,r]of Object.entries(t)){let t=n[e];M(t)&&M(r)?n[e]=P(t,r):n[e]=r}return n},F=e=>{if(!e||typeof e!=`object`)return!1;let t=e;return typeof t.toObject==`function`&&typeof t.getRawAttributes==`function`},I=e=>!e||typeof e!=`object`?!1:typeof e.all==`function`,L=e=>{if(Array.isArray(e))return e.map(e=>L(e));if(F(e))return L(e.toObject());if(I(e)){let t=e.all();return Array.isArray(t)?t.map(e=>L(e)):L(t)}return M(e)?Object.entries(e).reduce((e,[t,n])=>(e[t]=L(n),e),{}):e},R=()=>{let e=fe();return Array.isArray(e)?{metaKey:e.includes(`meta`)?`meta`:void 0,linksKey:e.includes(`links`)?`links`:void 0,cursorKey:e.includes(`cursor`)?`cursor`:void 0}:{metaKey:e.meta,linksKey:e.links,cursorKey:e.cursor}},z=(e,t)=>{if(e===void 0)return;let n=t||D()||``,r=he()||``,i=C()||`page`,a=n.indexOf(`?`),o=a>=0?n.slice(0,a):n,s=a>=0?n.slice(a+1):``;if(/^https?:\/\//i.test(o)){let t=new URL(n);return t.searchParams.set(i,String(e)),t.toString()}let c=r.replace(/\/$/,``),l=o.replace(/^\//,``);if(/^https?:\/\//i.test(c)){let t=l?`${c}/${l}`:c,n=new URL(t);if(s)for(let[e,t]of new URLSearchParams(s))n.searchParams.set(e,t);return n.searchParams.set(i,String(e)),n.toString()}let u=[c,l].filter(Boolean).join(`/`),d=u?`/${u}`:`/`,f=new URLSearchParams(s);return f.set(i,String(e)),`${d}?${f.toString()}`},B=e=>{let{metaKey:t,linksKey:n,cursorKey:r}=R(),i={},a=!!e&&typeof e==`object`&&!!e.meta&&typeof e.meta==`object`&&(Array.isArray(e.data)||I(e.data)),o=a?{first:typeof e.firstPageUrl==`function`?e.firstPageUrl():void 0,last:typeof e.lastPageUrl==`function`?e.lastPageUrl():void 0,prev:typeof e.previousPageUrl==`function`?e.previousPageUrl():void 0,next:typeof e.nextPageUrl==`function`?e.nextPageUrl():void 0}:void 0,s=o?Object.entries(o).reduce((e,[t,n])=>(n!==void 0&&(e[t]=n),e),{}):void 0,c=e?.pagination||(a?{...e.meta,links:e.links||s}:void 0),l=e?.cursor,u={},d={};if(c){let e={to:c.to,from:c.from,links:c.links,path:c.path,total:c.total,per_page:c.perPage,last_page:c.lastPage,current_page:c.currentPage};for(let[t,n]of Object.entries(_e())){if(!n)continue;let r=e[t];r!==void 0&&(u[n]=r)}let t={first:c.links&&Object.prototype.hasOwnProperty.call(c.links,`first`)?c.links.first:z(c.firstPage,c.path),last:c.links&&Object.prototype.hasOwnProperty.call(c.links,`last`)?c.links.last:z(c.lastPage,c.path),prev:c.links&&Object.prototype.hasOwnProperty.call(c.links,`prev`)?c.links.prev:z(c.prevPage,c.path),next:c.links&&Object.prototype.hasOwnProperty.call(c.links,`next`)?c.links.next:z(c.nextPage,c.path)};for(let[e,n]of Object.entries(me())){if(!n)continue;let r=t[e];r!==void 0&&(d[n]=r)}}if(l){let e={},t={previous:l.previous,next:l.next};for(let[n,r]of Object.entries(w())){if(!r)continue;let i=t[n];i!==void 0&&(e[r]=i)}r&&Object.keys(e).length>0?i[r]=e:Object.keys(e).length>0&&(u.cursor=e)}return t&&Object.keys(u).length>0&&(i[t]=u),n&&Object.keys(d).length>0&&(i[n]=d),i},V=({payload:e,meta:t,metaKey:n=`meta`,wrap:r=!0,rootKey:i=`data`,factory:a,context:o})=>{if(a)return a(e,{...o,rootKey:i,meta:t});if(!r)return t===void 0?e:M(e)?{...e,[n]:t}:{[i]:e,[n]:t};let s={[i]:e};return t!==void 0&&(s[n]=t),s},ye=(e,t)=>{let n=e?.with;if(typeof n!=`function`||n===t||n.length>0)return;let r=n.call(e);return M(r)?r:void 0},H=Symbol(`resora.conditional.missing`),be=(e,t)=>e?typeof t==`function`?t():t:H,xe=e=>e??H,Se=(e,t)=>{if(!e)return{};let n=typeof t==`function`?t():t;return M(n)?n:{}},U=e=>{if(e===H)return H;if(Array.isArray(e))return e.map(e=>U(e)).filter(e=>e!==H);if(M(e)){let t={};for(let[n,r]of Object.entries(e)){let e=U(r);e!==H&&(t[n]=e)}return t}return e},W=e=>e.replace(/([a-z0-9])([A-Z])/g,`$1 $2`).replace(/([A-Z]+)([A-Z][a-z])/g,`$1 $2`).replace(/[-_\s]+/g,` `).trim().toLowerCase().split(` `).filter(Boolean),Ce=e=>W(e).map((e,t)=>t===0?e:e.charAt(0).toUpperCase()+e.slice(1)).join(``),we=e=>W(e).join(`_`),Te=e=>W(e).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``),Ee=e=>W(e).join(`-`),G=e=>{if(typeof e==`function`)return e;switch(e){case`camel`:return Ce;case`snake`:return we;case`pascal`:return Te;case`kebab`:return Ee}},K=(e,t)=>e==null?e:Array.isArray(e)?e.map(e=>K(e,t)):e instanceof Date||e instanceof RegExp?e:typeof e==`object`?Object.fromEntries(Object.entries(e).map(([e,n])=>[t(e),K(n,t)])):e;let q=l.default.resolve(process.cwd(),`node_modules/resora/stubs`);(0,c.existsSync)(q)||(q=l.default.resolve(process.cwd(),`stubs`));const De=()=>({stubsDir:q,preferredCase:`camel`,responseStructure:{wrap:!0,rootKey:`data`},paginatedExtras:[`meta`,`links`],baseUrl:``,pageName:`page`,paginatedLinks:{first:`first`,last:`last`,prev:`prev`,next:`next`},paginatedMeta:{to:`to`,from:`from`,links:`links`,path:`path`,total:`total`,per_page:`per_page`,last_page:`last_page`,current_page:`current_page`},cursorMeta:{previous:`previous`,next:`next`},resourcesDir:`src/resources`,stubs:{config:`resora.config.stub`,resource:`resource.stub`,collection:`resource.collection.stub`}}),J=e=>{let t=De();return Object.assign(t,e,{stubs:Object.assign(t.stubs,e.stubs||{})},{cursorMeta:Object.assign(t.cursorMeta,e.cursorMeta||{})},{paginatedMeta:e.paginatedMeta||t.paginatedMeta},{paginatedLinks:Object.assign(t.paginatedLinks,e.paginatedLinks||{})},{responseStructure:Object.assign(t.responseStructure,e.responseStructure||{})})};let Y=!1,X;const Oe=()=>{Y=!1,X=void 0},ke=e=>{e.preferredCase!==`camel`&&te(e.preferredCase),re(e.responseStructure),de(e.paginatedExtras),pe(e.paginatedLinks),ge(e.paginatedMeta),ve(e.cursorMeta),x(e.baseUrl),S(e.pageName)},Ae=async e=>await import(`${(0,d.pathToFileURL)(e).href}?resora_runtime=${Date.now()}`),je=e=>{ke(J((e?.default??e)||{})),Y=!0},Me=()=>{let e=(0,u.createRequire)(e(`url`).pathToFileURL(__filename).href),t=[l.default.join(process.cwd(),`resora.config.cjs`)];for(let n of t)if((0,c.existsSync)(n))try{return je(e(n)),!0}catch{continue}return!1},Z=async()=>{if(!Y){if(X)return await X;Me()||(X=(async()=>{let e=[l.default.join(process.cwd(),`resora.config.js`),l.default.join(process.cwd(),`resora.config.ts`)];for(let t of e)if((0,c.existsSync)(t))try{je(await Ae(t));return}catch{continue}Y=!0})(),await X)}};Z();var Ne=class{command;config={};constructor(e={}){this.config=J(e)}async loadConfig(e={}){this.config=J(e);let t=[(0,l.join)(process.cwd(),`resora.config.ts`),(0,l.join)(process.cwd(),`resora.config.js`),(0,l.join)(process.cwd(),`resora.config.cjs`)];for(let e of t)if((0,c.existsSync)(e))try{let{default:t}=await import(e);Object.assign(this.config,t);break}catch(t){console.error(`Error loading config file at ${e}:`,t)}return this}getConfig(){return this.config}init(){let e=(0,l.join)(process.cwd(),`resora.config.js`),t=(0,l.join)(this.config.stubsDir,this.config.stubs.config);return(0,c.existsSync)(e)&&!this.command.option(`force`)&&(this.command.error(`Error: ${e} already exists.`),process.exit(1)),this.ensureDirectory(e),(0,c.existsSync)(e)&&this.command.option(`force`)&&(0,c.copyFileSync)(e,e.replace(/\.js$/,`.backup.${Date.now()}.js`)),(0,c.writeFileSync)(e,(0,c.readFileSync)(t,`utf-8`)),{path:e}}ensureDirectory(e){let t=(0,l.dirname)(e);(0,c.existsSync)(t)||(0,c.mkdirSync)(t,{recursive:!0})}generateFile(e,t,n,r){(0,c.existsSync)(t)&&!r?.force?(this.command.error(`Error: ${t} already exists.`),process.exit(1)):(0,c.existsSync)(t)&&r?.force&&(0,c.rmSync)(t);let i=(0,c.readFileSync)(e,`utf-8`);for(let[e,t]of Object.entries(n))i=i.replace(RegExp(`{{${e}}}`,`g`),t);return this.ensureDirectory(t),(0,c.writeFileSync)(t,i),t}makeResource(e,t){let n=e;t?.collection&&!e.endsWith(`Collection`)&&!e.endsWith(`Resource`)?n+=`Collection`:!t?.collection&&!e.endsWith(`Resource`)&&!e.endsWith(`Collection`)&&(n+=`Resource`);let r=`${n}.ts`,i=(0,l.join)(this.config.resourcesDir,r),a=(0,l.join)(this.config.stubsDir,t?.collection||e.endsWith(`Collection`)?this.config.stubs.collection:this.config.stubs.resource);(0,c.existsSync)(a)||(this.command.error(`Error: Stub file ${a} not found.`),process.exit(1)),n=n.split(`/`).pop()?.split(`.`).shift();let o=n.replace(/(Resource|Collection)$/,``)+`Resource`,s=[`/**`,` * The resource that this collection collects.`,` */`,`collects = ${o}`].join(`
|
|
2
|
+
`),u=`import ${o} from './${o}'\n`,d=(!!t?.collection||e.endsWith(`Collection`))&&(0,c.existsSync)((0,l.join)(this.config.resourcesDir,`${o}.ts`)),f=this.generateFile(a,i,{ResourceName:n,CollectionResourceName:n.replace(/(Resource|Collection)$/,``)+`Resource`,"collects = Resource":d?s:``,"import = Resource":d?u:``},t);return{name:n,path:f}}},Pe=class extends f.Command{signature=`init
|
|
3
3
|
{--force : Force overwrite if config file already exists (existing file will be backed up) }
|
|
4
|
-
`;description=`Initialize Resora`;async handle(){this.app.command=this,this.app.init(),this.success(`Resora initialized`)}},
|
|
4
|
+
`;description=`Initialize Resora`;async handle(){this.app.command=this,this.app.init(),this.success(`Resora initialized`)}},Fe=class extends f.Command{signature=`#create:
|
|
5
5
|
{resource : Generates a new resource file.
|
|
6
6
|
| {name : Name of the resource to create}
|
|
7
7
|
| {--c|collection : Make a resource collection}
|
|
@@ -22,4 +22,4 @@ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.
|
|
|
22
22
|
| _ // _ \/ __|/ _ \| '__/ _, |
|
|
23
23
|
| | \ \ __/\__ \ (_) | | | (_| |
|
|
24
24
|
|_| \_\___||___/\___/|_| \__,_|
|
|
25
|
-
`;var Q=class{_status=200;headers={};constructor(e,t){this.response=e,this.body=t}setStatusCode(e){return this._status=e,`status`in this.response&&typeof this.response.status==`function`?this.response.status(e):`status`in this.response&&(this.response.status=e),this}status(){return this._status}statusText(){if(`statusMessage`in this.response)return this.response.statusMessage;if(`statusText`in this.response)return this.response.statusText}setCookie(e,t,n){return this.#e(`Set-Cookie`,`${e}=${t}; ${Object.entries(n||{}).map(([e,t])=>`${e}=${t}`).join(`; `)}`),this}setHeaders(e){for(let[t,n]of Object.entries(e))this.#e(t,n);return this}header(e,t){return this.#e(e,t),this}#e(e,t){this.headers[e]=t,`headers`in this.response?this.response.headers.set(e,t):`setHeader`in this.response&&this.response.setHeader(e,t)}then(e,t){let n=Promise.resolve(this.body).then(e,t);return`send`in this.response&&this.response.send(this.body),n}catch(e){return this.then(void 0,e)}finally(e){return this.then(e,e)}},$=class{static preferredCase;static responseStructure;static config;instanceConfig;additionalMeta;called={};constructor(){Z()}when(e,t){return V(e,t)}whenNotNull(e){return H(e)}mergeWhen(e,t){return he(e,t)}with(e){if(this.called.with=!0,e===void 0)return this.additionalMeta||{};let t=typeof e==`function`?e(this.getResourceForMeta()):e;return this.additionalMeta=j(this.additionalMeta,t),this.called.json&&this.applyMetaToBody(t,this.resolveCurrentRootKey()),this}withMeta(e){return this.with(e),this}resolveMergedMeta(e){return j(z(this,e),this.additionalMeta)}runResponse(e){this.called.toResponse=!0,e.ensureJson();let t=e.body(),n=e.createServerResponse(e.rawResponse,t);return this.called.withResponse=!0,e.callWithResponse(n,e.rawResponse),n}runThen(e){this.called.then=!0,e.ensureJson();let t=e.body();if(e.rawResponse!==void 0){let n=e.createServerResponse(e.rawResponse,t);this.called.withResponse=!0,e.callWithResponse(n,e.rawResponse)}else this.called.withResponse=!0,e.callWithResponse();let n=e.body(),r=Promise.resolve(n).then(e.onfulfilled,e.onrejected);return e.rawResponse!==void 0&&e.sendRawResponse&&e.sendRawResponse(e.rawResponse,n),r}config(e){return e===void 0?this.instanceConfig||{}:(this.instanceConfig={...this.instanceConfig||{},...e,responseStructure:{...this.instanceConfig?.responseStructure||{},...e.responseStructure||{}}},this)}resolveSerializerConfig(e,t){let n=typeof e.config==`function`?e.config():{};return{preferredCase:this.instanceConfig?.preferredCase??n?.preferredCase,responseStructure:{...n?.responseStructure||{},...this.instanceConfig?.responseStructure||{}}}}resolveSerializerCaseStyle(e,t){return this.resolveSerializerConfig(e,t).preferredCase??e.preferredCase??t?.preferredCase??S()}resolveSerializerResponseStructure(e,t){let n=this.resolveSerializerConfig(e,t),r=w();return{wrap:n.responseStructure?.wrap??e.responseStructure?.wrap??t?.responseStructure?.wrap??r?.wrap??!0,rootKey:n.responseStructure?.rootKey??e.responseStructure?.rootKey??t?.responseStructure?.rootKey??r?.rootKey??`data`,factory:n.responseStructure?.factory??e.responseStructure?.factory??t?.responseStructure?.factory??r?.factory}}},ke=class e extends ${body={data:{}};resource;collects;withResponseContext;constructor(e,t){super(),this.res=t,this.resource=e;let n=!!this.resource&&typeof this.resource==`object`&&`data`in this.resource,r=n?this.resource.data:void 0,i=!!r&&!Array.isArray(r),a=n?r:this.resource;if(a&&typeof a==`object`&&!Array.isArray(a)&&!N(a)){let e=M(a)?Object.keys(a.toObject()):Object.keys(a);for(let t of e)t in this||Object.defineProperty(this,t,{enumerable:!0,configurable:!0,get:()=>M(a)&&typeof a.getAttribute==`function`?a.getAttribute(t):i?r[t]:this.resource[t],set:e=>{if(M(a)&&typeof a.setAttribute==`function`){a.setAttribute(t,e);return}if(i){r[t]=e;return}this.resource[t]=e}})}}data(){return this.resource}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveCollectsConfig(){let e=this.collects;if(!e)return;let t=typeof e.config==`function`?e.config():{};return{preferredCase:t.preferredCase??e.preferredCase,responseStructure:{...e.responseStructure||{},...t.responseStructure||{}}}}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor,this.resolveCollectsConfig())}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=A(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=P(this.data());Array.isArray(t)&&this.collects&&(t=t.map(e=>new this.collects(e).data())),!Array.isArray(t)&&t&&t.data!==void 0&&(t=t.data),t=U(t);let n=L(this.resource),{metaKey:r}=F(),i=r?n[r]:void 0;r&&delete n[r];let a=this.resolveSerializerCaseStyle(this.constructor,this.resolveCollectsConfig());if(a){let e=G(a);t=K(t,e)}let o=this.resolveMergedMeta(e.prototype.with),{wrap:s,rootKey:c,factory:l}=this.resolveResponseStructure();this.body=R({payload:t,meta:i,metaKey:r,wrap:s,rootKey:c,factory:l,context:{type:`generic`,resource:this.resource}}),this.body=A(this.body,{...n,...o||{}},c)}return this}toObject(){this.called.toObject=!0,this.json();let e=P(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json();let t=e.data;delete e.data,delete e.pagination;let n=this.getPayloadKey();return t&&n&&this.body[n]!==void 0&&(this.body[n]=Array.isArray(this.body[n])?[...this.body[n],...t]:{...this.body[n],...t}),this.body={...this.body,...e},this}response(e){let t=e??this.res;return this.runResponse({ensureJson:()=>this.json(),rawResponse:t,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}then(e,t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:t})}catch(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:e})}finally(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:e})}},Ae=class e extends ${body={data:[]};resource;collects;withResponseContext;isPaginatedCollectible(e){if(!e||typeof e!=`object`)return!1;let t=e;return t.pagination&&Array.isArray(t.data)?!0:t.meta&&typeof t.meta==`object`&&`currentPage`in t.meta?Array.isArray(t.data)||N(t.data):!1}constructor(e,t){super(),this.res=t,this.resource=e}data(){return this.toObject()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveCollectsConfig(){let e=this.collects;if(!e)return;let t=typeof e.config==`function`?e.config():{};return{preferredCase:t.preferredCase??e.preferredCase,responseStructure:{...e.responseStructure||{},...t.responseStructure||{}}}}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor,this.resolveCollectsConfig())}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=A(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=this.data();this.collects&&(t=t.map(e=>new this.collects(e).data())),t=P(t),t=U(t);let n=Array.isArray(this.resource)?{}:L(this.resource),{metaKey:r}=F(),i=r?n[r]:void 0;r&&delete n[r];let a=this.resolveSerializerCaseStyle(this.constructor,this.resolveCollectsConfig());if(a){let e=G(a);t=K(t,e)}let o=this.resolveMergedMeta(e.prototype.with),{wrap:s,rootKey:c,factory:l}=this.resolveResponseStructure();this.body=R({payload:t,meta:i,metaKey:r,wrap:s,rootKey:c,factory:l,context:{type:`collection`,resource:this.resource}}),this.body=A(this.body,{...n,...o||{}},c)}return this}toObject(){return this.called.toObject=!0,this.json(),P(Array.isArray(this.resource)?this.resource:N(this.resource)?this.resource.all():this.resource.data)}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json(),delete e.cursor,delete e.pagination;let t=this.getPayloadKey();return e.data&&t&&Array.isArray(this.body[t])&&(this.body[t]=[...this.body[t],...e.data]),this.body={...this.body,...e},this}response(e){let t=e??this.res;return this.runResponse({ensureJson:()=>this.json(),rawResponse:t,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}setCollects(e){return this.collects=e,this}then(e,t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:t})}catch(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:e})}finally(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:e})}},je=class e extends ${body={data:{}};resource;withResponseContext;constructor(e,t){super(),this.res=t,this.resource=e;let n=this.resource.data??this.resource;if(!Array.isArray(n)){let e=M(n)?Object.keys(n.toObject()):Object.keys(n);for(let t of e)t in this||Object.defineProperty(this,t,{enumerable:!0,configurable:!0,get:()=>M(n)&&typeof n.getAttribute==`function`?n.getAttribute(t):this.resource.data?.[t]??this.resource[t],set:e=>{if(M(n)&&typeof n.setAttribute==`function`){n.setAttribute(t,e);return}this.resource.data&&this.resource.data[t]?this.resource.data[t]=e:this.resource[t]=e}})}}static collection(e){return new Ae(e).setCollects(this)}data(){return this.toObject()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor)}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=A(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=P(this.data());!Array.isArray(t)&&t&&t.data!==void 0&&(t=t.data),t=U(t);let n=this.resolveSerializerCaseStyle(this.constructor);if(n){let e=G(n);t=K(t,e)}let r=this.resolveMergedMeta(e.prototype.with),{wrap:i,rootKey:a,factory:o}=this.resolveResponseStructure();this.body=R({payload:t,wrap:i,rootKey:a,factory:o,context:{type:`resource`,resource:this.resource}}),this.body=A(this.body,r,a)}return this}toObject(){this.called.toObject=!0,this.json();let e=P(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json();let t=this.getPayloadKey();return e.data&&t&&this.body[t]!==void 0&&(this.body[t]=Array.isArray(this.body[t])?[...this.body[t],...e.data]:{...this.body[t],...e.data}),this.body={...this.body,...e},this}response(e){let t=e??this.res;return this.runResponse({ensureJson:()=>this.json(),rawResponse:t,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}then(e,t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:t})}catch(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:e})}finally(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:e})}};exports.ApiResource=ee,exports.CONDITIONAL_ATTRIBUTE_MISSING=B,exports.CliApp=Ee,exports.GenericResource=ke,exports.InitCommand=De,exports.MakeResource=Oe,exports.Resource=je,exports.ResourceCollection=Ae,exports.ServerResponse=Q,exports.appendRootProperties=A,exports.applyRuntimeConfig=Se,exports.buildPaginationExtras=L,exports.buildResponseEnvelope=R,exports.defineConfig=J,exports.getCaseTransformer=G,exports.getDefaultConfig=be,exports.getGlobalBaseUrl=fe,exports.getGlobalCase=S,exports.getGlobalCursorMeta=O,exports.getGlobalPageName=me,exports.getGlobalPaginatedExtras=ce,exports.getGlobalPaginatedLinks=ue,exports.getGlobalPaginatedMeta=E,exports.getGlobalResponseFactory=oe,exports.getGlobalResponseRootKey=ie,exports.getGlobalResponseStructure=w,exports.getGlobalResponseWrap=re,exports.getPaginationExtraKeys=F,exports.isArkormLikeCollection=N,exports.isArkormLikeModel=M,exports.isPlainObject=k,exports.loadRuntimeConfig=Z,exports.mergeMetadata=j,exports.normalizeSerializableData=P,exports.resetRuntimeConfigForTests=xe,exports.resolveMergeWhen=he,exports.resolveWhen=V,exports.resolveWhenNotNull=H,exports.resolveWithHookMetadata=z,exports.sanitizeConditionalAttributes=U,exports.setGlobalBaseUrl=de,exports.setGlobalCase=x,exports.setGlobalCursorMeta=D,exports.setGlobalPageName=pe,exports.setGlobalPaginatedExtras=se,exports.setGlobalPaginatedLinks=le,exports.setGlobalPaginatedMeta=T,exports.setGlobalResponseFactory=ae,exports.setGlobalResponseRootKey=te,exports.setGlobalResponseStructure=C,exports.setGlobalResponseWrap=ne,exports.splitWords=W,exports.toCamelCase=ge,exports.toKebabCase=ye,exports.toPascalCase=ve,exports.toSnakeCase=_e,exports.transformKeys=K;
|
|
25
|
+
`;var Q=class{_status=200;headers={};constructor(e,t){this.response=e,this.body=t}setStatusCode(e){return this._status=e,`status`in this.response&&typeof this.response.status==`function`?this.response.status(e):`status`in this.response&&(this.response.status=e),this}status(){return this._status}statusText(){if(`statusMessage`in this.response)return this.response.statusMessage;if(`statusText`in this.response)return this.response.statusText}setCookie(e,t,n){return this.#e(`Set-Cookie`,`${e}=${t}; ${Object.entries(n||{}).map(([e,t])=>`${e}=${t}`).join(`; `)}`),this}setHeaders(e){for(let[t,n]of Object.entries(e))this.#e(t,n);return this}header(e,t){return this.#e(e,t),this}#e(e,t){this.headers[e]=t,`headers`in this.response?this.response.headers.set(e,t):`setHeader`in this.response&&this.response.setHeader(e,t)}then(e,t){let n=Promise.resolve(this.body).then(e,t);return`send`in this.response&&this.response.send(this.body),n}catch(e){return this.then(void 0,e)}finally(e){return this.then(e,e)}},$=class{static preferredCase;static responseStructure;static config;static ctx;instanceConfig;additionalMeta;called={};constructor(){Z()}static setCtx(e){j(e),this.ctx=e}when(e,t){return be(e,t)}whenNotNull(e){return xe(e)}mergeWhen(e,t){return Se(e,t)}with(e){if(this.called.with=!0,e===void 0)return this.additionalMeta||{};let t=typeof e==`function`?e(this.getResourceForMeta()):e;return this.additionalMeta=P(this.additionalMeta,t),this.called.json&&this.applyMetaToBody(t,this.resolveCurrentRootKey()),this}withMeta(e){return this.with(e),this}resolveMergedMeta(e){return P(ye(this,e),this.additionalMeta)}runResponse(e){this.called.toResponse=!0,e.ensureJson();let t=e.body(),n=e.createServerResponse(e.rawResponse,t);return this.called.withResponse=!0,e.callWithResponse(n,e.rawResponse),n}runThen(e){this.called.then=!0,e.ensureJson();let t=e.body();if(e.rawResponse!==void 0){let n=e.createServerResponse(e.rawResponse,t);this.called.withResponse=!0,e.callWithResponse(n,e.rawResponse)}else this.called.withResponse=!0,e.callWithResponse();let n=e.body(),r=Promise.resolve(n).then(e.onfulfilled,e.onrejected);return e.rawResponse!==void 0&&e.sendRawResponse&&e.sendRawResponse(e.rawResponse,n),r}config(e){return e===void 0?this.instanceConfig||{}:(this.instanceConfig={...this.instanceConfig||{},...e,responseStructure:{...this.instanceConfig?.responseStructure||{},...e.responseStructure||{}}},this)}resolveSerializerConfig(e,t){let n=typeof e.config==`function`?e.config():{};return{preferredCase:this.instanceConfig?.preferredCase??n?.preferredCase,responseStructure:{...n?.responseStructure||{},...this.instanceConfig?.responseStructure||{}}}}resolveSerializerCaseStyle(e,t){return this.resolveSerializerConfig(e,t).preferredCase??e.preferredCase??t?.preferredCase??ne()}resolveSerializerResponseStructure(e,t){let n=this.resolveSerializerConfig(e,t),r=ie();return{wrap:n.responseStructure?.wrap??e.responseStructure?.wrap??t?.responseStructure?.wrap??r?.wrap??!0,rootKey:n.responseStructure?.rootKey??e.responseStructure?.rootKey??t?.responseStructure?.rootKey??r?.rootKey??`data`,factory:n.responseStructure?.factory??e.responseStructure?.factory??t?.responseStructure?.factory??r?.factory}}},Ie=class e extends ${body={data:{}};res;resource;collects;withResponseContext;constructor(t,n){if(super(),n&&(e.ctx=n),this.resource=t,n){let e=O(n);e&&E(e),this.res=A(n)}let r=!!this.resource&&typeof this.resource==`object`&&`data`in this.resource,i=r?this.resource.data:void 0,a=!!i&&!Array.isArray(i),o=r?i:this.resource;if(o&&typeof o==`object`&&!Array.isArray(o)&&!I(o)){let e=F(o)?Object.keys(o.toObject()):Object.keys(o);for(let t of e)t in this||Object.defineProperty(this,t,{enumerable:!0,configurable:!0,get:()=>F(o)&&typeof o.getAttribute==`function`?o.getAttribute(t):a?i[t]:this.resource[t],set:e=>{if(F(o)&&typeof o.setAttribute==`function`){o.setAttribute(t,e);return}if(a){i[t]=e;return}this.resource[t]=e}})}}data(){return this.resource}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveCollectsConfig(){let e=this.collects;if(!e)return;let t=typeof e.config==`function`?e.config():{};return{preferredCase:t.preferredCase??e.preferredCase,responseStructure:{...e.responseStructure||{},...t.responseStructure||{}}}}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor,this.resolveCollectsConfig())}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=N(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=L(this.data());Array.isArray(t)&&this.collects&&(t=t.map(e=>new this.collects(e).data())),!Array.isArray(t)&&t&&t.data!==void 0&&(t=t.data),t=U(t);let n=B(this.resource),{metaKey:r}=R(),i=r?n[r]:void 0;r&&delete n[r];let a=this.resolveSerializerCaseStyle(this.constructor,this.resolveCollectsConfig());if(a){let e=G(a);t=K(t,e)}let o=this.resolveMergedMeta(e.prototype.with),{wrap:s,rootKey:c,factory:l}=this.resolveResponseStructure();this.body=V({payload:t,meta:i,metaKey:r,wrap:s,rootKey:c,factory:l,context:{type:`generic`,resource:this.resource}}),this.body=N(this.body,{...n,...o||{}},c)}return this}toObject(){this.called.toObject=!0,this.json();let e=L(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json();let t=e.data;delete e.data,delete e.pagination;let n=this.getPayloadKey();return t&&n&&this.body[n]!==void 0&&(this.body[n]=Array.isArray(this.body[n])?[...this.body[n],...t]:{...this.body[n],...t}),this.body={...this.body,...e},this}response(t){let n=t??this.res??e.ctx?.res??e.ctx;return this.runResponse({ensureJson:()=>this.json(),rawResponse:n,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}then(t,n){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:n})}catch(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:t})}finally(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:t})}},Le=class e extends ${body={data:[]};res;resource;collects;withResponseContext;isPaginatedCollectible(e){if(!e||typeof e!=`object`)return!1;let t=e;return t.pagination&&Array.isArray(t.data)?!0:t.meta&&typeof t.meta==`object`&&`currentPage`in t.meta?Array.isArray(t.data)||I(t.data):!1}constructor(t,n){if(super(),n&&(e.ctx=n),this.resource=t,n){let e=O(n);e&&E(e),this.res=A(n)}}data(){return this.toObject()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveCollectsConfig(){let e=this.collects;if(!e)return;let t=typeof e.config==`function`?e.config():{};return{preferredCase:t.preferredCase??e.preferredCase,responseStructure:{...e.responseStructure||{},...t.responseStructure||{}}}}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor,this.resolveCollectsConfig())}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=N(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=this.data();this.collects&&(t=t.map(e=>new this.collects(e).data())),t=L(t),t=U(t);let n=Array.isArray(this.resource)?{}:B(this.resource),{metaKey:r}=R(),i=r?n[r]:void 0;r&&delete n[r];let a=this.resolveSerializerCaseStyle(this.constructor,this.resolveCollectsConfig());if(a){let e=G(a);t=K(t,e)}let o=this.resolveMergedMeta(e.prototype.with),{wrap:s,rootKey:c,factory:l}=this.resolveResponseStructure();this.body=V({payload:t,meta:i,metaKey:r,wrap:s,rootKey:c,factory:l,context:{type:`collection`,resource:this.resource}}),this.body=N(this.body,{...n,...o||{}},c)}return this}toObject(){return this.called.toObject=!0,this.json(),L(Array.isArray(this.resource)?this.resource:I(this.resource)?this.resource.all():this.resource.data)}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json(),delete e.cursor,delete e.pagination;let t=this.getPayloadKey();return e.data&&t&&Array.isArray(this.body[t])&&(this.body[t]=[...this.body[t],...e.data]),this.body={...this.body,...e},this}response(t){let n=t??this.res??e.ctx?.res??e.ctx;return this.runResponse({ensureJson:()=>this.json(),rawResponse:n,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}setCollects(e){return this.collects=e,this}then(t,n){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:n})}catch(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:t})}finally(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:t})}},Re=class e extends ${body={data:{}};res;resource;withResponseContext;constructor(t,n){if(super(),n&&(e.ctx=n),this.resource=t,n){let e=O(n);e&&E(e),this.res=A(n)}let r=this.resource.data??this.resource;if(!Array.isArray(r)){let e=F(r)?Object.keys(r.toObject()):Object.keys(r);for(let t of e)t in this||Object.defineProperty(this,t,{enumerable:!0,configurable:!0,get:()=>F(r)&&typeof r.getAttribute==`function`?r.getAttribute(t):this.resource.data?.[t]??this.resource[t],set:e=>{if(F(r)&&typeof r.setAttribute==`function`){r.setAttribute(t,e);return}this.resource.data&&this.resource.data[t]?this.resource.data[t]=e:this.resource[t]=e}})}}static collection(e){return new Le(e).setCollects(this)}data(){return this.toObject()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor)}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=N(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=L(this.data());!Array.isArray(t)&&t&&t.data!==void 0&&(t=t.data),t=U(t);let n=this.resolveSerializerCaseStyle(this.constructor);if(n){let e=G(n);t=K(t,e)}let r=this.resolveMergedMeta(e.prototype.with),{wrap:i,rootKey:a,factory:o}=this.resolveResponseStructure();this.body=V({payload:t,wrap:i,rootKey:a,factory:o,context:{type:`resource`,resource:this.resource}}),this.body=N(this.body,r,a)}return this}toObject(){this.called.toObject=!0,this.json();let e=L(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json();let t=this.getPayloadKey();return e.data&&t&&this.body[t]!==void 0&&(this.body[t]=Array.isArray(this.body[t])?[...this.body[t],...e.data]:{...this.body[t],...e.data}),this.body={...this.body,...e},this}response(t){let n=t??this.res??e.ctx?.res??e.ctx;return this.runResponse({ensureJson:()=>this.json(),rawResponse:n,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}then(t,n){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:n})}catch(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:t})}finally(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:t})}};exports.ApiResource=ee,exports.CONDITIONAL_ATTRIBUTE_MISSING=H,exports.CliApp=Ne,exports.GenericResource=Ie,exports.InitCommand=Pe,exports.MakeResource=Fe,exports.Resource=Re,exports.ResourceCollection=Le,exports.ServerResponse=Q,exports.appendRootProperties=N,exports.applyRuntimeConfig=ke,exports.buildPaginationExtras=B,exports.buildResponseEnvelope=V,exports.defineConfig=J,exports.extractRequestUrl=O,exports.extractResponseFromCtx=A,exports.getCaseTransformer=G,exports.getDefaultConfig=De,exports.getGlobalBaseUrl=he,exports.getGlobalCase=ne,exports.getGlobalCursorMeta=w,exports.getGlobalPageName=C,exports.getGlobalPaginatedExtras=fe,exports.getGlobalPaginatedLinks=me,exports.getGlobalPaginatedMeta=_e,exports.getGlobalResponseFactory=ue,exports.getGlobalResponseRootKey=ce,exports.getGlobalResponseStructure=ie,exports.getGlobalResponseWrap=se,exports.getPaginationExtraKeys=R,exports.getRequestUrl=D,exports.isArkormLikeCollection=I,exports.isArkormLikeModel=F,exports.isPlainObject=M,exports.loadRuntimeConfig=Z,exports.mergeMetadata=P,exports.normalizeSerializableData=L,exports.resetRuntimeConfigForTests=Oe,exports.resolveMergeWhen=Se,exports.resolveWhen=be,exports.resolveWhenNotNull=xe,exports.resolveWithHookMetadata=ye,exports.sanitizeConditionalAttributes=U,exports.setCtx=j,exports.setGlobalBaseUrl=x,exports.setGlobalCase=te,exports.setGlobalCursorMeta=ve,exports.setGlobalPageName=S,exports.setGlobalPaginatedExtras=de,exports.setGlobalPaginatedLinks=pe,exports.setGlobalPaginatedMeta=ge,exports.setGlobalResponseFactory=le,exports.setGlobalResponseRootKey=ae,exports.setGlobalResponseStructure=re,exports.setGlobalResponseWrap=oe,exports.setRequestUrl=E,exports.splitWords=W,exports.toCamelCase=Ce,exports.toKebabCase=Ee,exports.toPascalCase=Te,exports.toSnakeCase=we,exports.transformKeys=K;
|
package/dist/index.d.cts
CHANGED
|
@@ -460,6 +460,7 @@ declare abstract class BaseSerializer<TResource = any> {
|
|
|
460
460
|
static preferredCase?: CaseStyle;
|
|
461
461
|
static responseStructure?: ResponseStructureConfig;
|
|
462
462
|
static config?: () => ResourceLevelConfig;
|
|
463
|
+
protected static ctx?: Response | H3Event | Record<string, any>;
|
|
463
464
|
protected instanceConfig?: ResourceLevelConfig;
|
|
464
465
|
protected additionalMeta?: MetaData;
|
|
465
466
|
protected called: {
|
|
@@ -475,6 +476,16 @@ declare abstract class BaseSerializer<TResource = any> {
|
|
|
475
476
|
toResponse?: boolean;
|
|
476
477
|
};
|
|
477
478
|
constructor();
|
|
479
|
+
/**
|
|
480
|
+
* Sets the current request context for pagination URL detection.
|
|
481
|
+
* Call from middleware to make the request path available to all
|
|
482
|
+
* resources created during the request lifecycle.
|
|
483
|
+
*
|
|
484
|
+
* Accepts an Express Request, H3Event, `{ req }` object, or a plain URL string.
|
|
485
|
+
*
|
|
486
|
+
* @param ctx The request context.
|
|
487
|
+
*/
|
|
488
|
+
static setCtx(ctx: unknown): void;
|
|
478
489
|
/**
|
|
479
490
|
* Helper method to conditionally resolve a value based on a condition.
|
|
480
491
|
*
|
|
@@ -615,9 +626,9 @@ declare abstract class BaseSerializer<TResource = any> {
|
|
|
615
626
|
* @see BaseSerializer for shared serialization logic and configuration handling
|
|
616
627
|
*/
|
|
617
628
|
declare class ResourceCollection<R extends ResourceData[] | Collectible | CollectionLike | PaginatorLike = ResourceData[] | Collectible | CollectionLike | PaginatorLike, T extends ResourceData = any> extends BaseSerializer<R> {
|
|
618
|
-
private res?;
|
|
619
629
|
[key: string]: any;
|
|
620
630
|
private body;
|
|
631
|
+
private res?;
|
|
621
632
|
resource: R;
|
|
622
633
|
collects?: typeof Resource<T>;
|
|
623
634
|
protected withResponseContext?: {
|
|
@@ -632,7 +643,7 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
632
643
|
*/
|
|
633
644
|
isPaginatedCollectible(value: unknown): value is Collectible;
|
|
634
645
|
constructor(rsc: R);
|
|
635
|
-
constructor(rsc: R,
|
|
646
|
+
constructor(rsc: R, ctx: Response | H3Event | Record<string, any>);
|
|
636
647
|
/**
|
|
637
648
|
* Get the original resource data
|
|
638
649
|
*/
|
|
@@ -702,8 +713,16 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
702
713
|
* @returns
|
|
703
714
|
*/
|
|
704
715
|
additional<X extends Record<string, any>>(extra: X): this;
|
|
716
|
+
/**
|
|
717
|
+
* Build a response object, optionally accepting a raw response to mutate in withResponse.
|
|
718
|
+
*/
|
|
705
719
|
response(): ServerResponse<CollectionBody<R>>;
|
|
720
|
+
/**
|
|
721
|
+
* Build a response object, optionally accepting a raw response to mutate in withResponse.
|
|
722
|
+
* @param res Optional raw response object (e.g. Express Response or H3Event res)
|
|
723
|
+
*/
|
|
706
724
|
response(res: H3Event['res']): ServerResponse<CollectionBody<R>>;
|
|
725
|
+
response(res: Response): ServerResponse<CollectionBody<R>>;
|
|
707
726
|
/**
|
|
708
727
|
* Customize the outgoing transport response right before dispatch.
|
|
709
728
|
*
|
|
@@ -745,15 +764,15 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
745
764
|
* @see BaseSerializer for shared serialization logic and configuration handling
|
|
746
765
|
*/
|
|
747
766
|
declare class Resource<R extends ResourceData | NonCollectible = ResourceData> extends BaseSerializer<R> {
|
|
748
|
-
private res?;
|
|
749
767
|
[key: string]: any;
|
|
750
768
|
private body;
|
|
769
|
+
private res?;
|
|
751
770
|
resource: R;
|
|
752
771
|
protected withResponseContext?: {
|
|
753
772
|
response: ServerResponse<ResourceBody<R>>;
|
|
754
773
|
raw: Response | H3Event['res'];
|
|
755
774
|
};
|
|
756
|
-
constructor(rsc: R,
|
|
775
|
+
constructor(rsc: R, ctx?: Response | H3Event | Record<string, any>);
|
|
757
776
|
/**
|
|
758
777
|
* Create a ResourceCollection from an array of resource data or a Collectible instance
|
|
759
778
|
*
|
|
@@ -819,6 +838,7 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> e
|
|
|
819
838
|
* @param res Optional raw response object (e.g. Express Response or H3Event res)
|
|
820
839
|
*/
|
|
821
840
|
response(res: H3Event['res']): ServerResponse<ResourceBody<R>>;
|
|
841
|
+
response(res: Response): ServerResponse<ResourceBody<R>>;
|
|
822
842
|
/**
|
|
823
843
|
* Customize the outgoing transport response right before dispatch.
|
|
824
844
|
*
|
|
@@ -858,16 +878,16 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> e
|
|
|
858
878
|
* @see BaseSerializer for shared serialization logic and configuration handling
|
|
859
879
|
*/
|
|
860
880
|
declare class GenericResource<R extends NonCollectible | Collectible | CollectionLike | PaginatorLike | ResourceData = ResourceData, T extends ResourceData = any> extends BaseSerializer<R> {
|
|
861
|
-
private res?;
|
|
862
881
|
[key: string]: any;
|
|
863
882
|
private body;
|
|
883
|
+
private res?;
|
|
864
884
|
resource: R;
|
|
865
885
|
collects?: typeof Resource<T>;
|
|
866
886
|
protected withResponseContext?: {
|
|
867
887
|
response: ServerResponse<GenericBody<R>>;
|
|
868
888
|
raw: Response | H3Event['res'];
|
|
869
889
|
};
|
|
870
|
-
constructor(rsc: R,
|
|
890
|
+
constructor(rsc: R, ctx?: Response | H3Event | Record<string, any>);
|
|
871
891
|
/**
|
|
872
892
|
* Get the original resource data
|
|
873
893
|
*/
|
|
@@ -939,6 +959,7 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
939
959
|
* @param res
|
|
940
960
|
*/
|
|
941
961
|
response(res: H3Event['res']): ServerResponse<GenericBody<R>>;
|
|
962
|
+
response(res: Response): ServerResponse<GenericBody<R>>;
|
|
942
963
|
/**
|
|
943
964
|
* Customize the outgoing transport response right before dispatch.
|
|
944
965
|
*
|
|
@@ -1362,5 +1383,53 @@ declare const setGlobalCursorMeta: (meta: Config["cursorMeta"]) => void;
|
|
|
1362
1383
|
* @returns The global cursor pagination metadata configuration.
|
|
1363
1384
|
*/
|
|
1364
1385
|
declare const getGlobalCursorMeta: () => Config["cursorMeta"];
|
|
1386
|
+
/**
|
|
1387
|
+
* Sets the current request URL, used as a fallback for pagination link generation
|
|
1388
|
+
* when no explicit path is provided in the pagination data.
|
|
1389
|
+
*
|
|
1390
|
+
* @param url The request URL or pathname to set.
|
|
1391
|
+
*/
|
|
1392
|
+
declare const setRequestUrl: (url: string | undefined) => void;
|
|
1393
|
+
/**
|
|
1394
|
+
* Retrieves the current request URL, used as a fallback path for pagination links.
|
|
1395
|
+
*
|
|
1396
|
+
* @returns The current request URL, or undefined if not set.
|
|
1397
|
+
*/
|
|
1398
|
+
declare const getRequestUrl: () => string | undefined;
|
|
1399
|
+
/**
|
|
1400
|
+
* Extracts the request URL pathname (with query string) from an HTTP context.
|
|
1401
|
+
*
|
|
1402
|
+
* Both Express middleware and H3 HTTPEvent expose `{ req, res }`. The `req`
|
|
1403
|
+
* object carries URL information:
|
|
1404
|
+
* - H3 / Web standard `Request`: `req.url` is a full URL string
|
|
1405
|
+
* - Express `Request`: `req.originalUrl` is the pathname + query string
|
|
1406
|
+
*
|
|
1407
|
+
* The function also accepts a plain `req` object directly or a string URL.
|
|
1408
|
+
*
|
|
1409
|
+
* @param ctx An HTTP context `{ req, res }`, a bare `Request` object, or a string URL.
|
|
1410
|
+
* @returns The pathname with query string, or undefined.
|
|
1411
|
+
*/
|
|
1412
|
+
declare const extractRequestUrl: (ctx: unknown) => string | undefined;
|
|
1413
|
+
/**
|
|
1414
|
+
* Extracts the response object from an HTTP context.
|
|
1415
|
+
*
|
|
1416
|
+
* If the context has a `res` property (Express middleware `{ req, res }` or
|
|
1417
|
+
* H3 HTTPEvent), returns `ctx.res`. Otherwise assumes the value is already
|
|
1418
|
+
* a bare response object and returns it directly.
|
|
1419
|
+
*
|
|
1420
|
+
* @param ctx The HTTP context or bare response.
|
|
1421
|
+
* @returns The response object, or undefined.
|
|
1422
|
+
*/
|
|
1423
|
+
declare const extractResponseFromCtx: (ctx: unknown) => any | undefined;
|
|
1424
|
+
/**
|
|
1425
|
+
* Sets the current request context. Extracts the request URL from the provided
|
|
1426
|
+
* context and stores it for use in pagination link generation.
|
|
1427
|
+
*
|
|
1428
|
+
* Can be called from middleware to make the request URL available to all
|
|
1429
|
+
* resources created during the request lifecycle.
|
|
1430
|
+
*
|
|
1431
|
+
* @param ctx An HTTP context `{ req, res }`, Express Request, H3 HTTPEvent, or bare request.
|
|
1432
|
+
*/
|
|
1433
|
+
declare const setCtx: (ctx: unknown) => void;
|
|
1365
1434
|
//#endregion
|
|
1366
|
-
export { ApiResource, CONDITIONAL_ATTRIBUTE_MISSING, CaseStyle, CliApp, Collectible, CollectionBody, CollectionLike, Config, Cursor, GenericBody, GenericResource, InitCommand, MakeResource, MetaData, NonCollectible, PaginatedMetaData, Pagination, PaginatorLike, ResoraConfig, Resource, ResourceBody, ResourceCollection, ResourceData, ResourceDef, ResourceLevelConfig, ResponseData, ResponseDataCollection, ResponseFactory, ResponseFactoryContext, ResponseKind, ResponseStructureConfig, ServerResponse, appendRootProperties, applyRuntimeConfig, buildPaginationExtras, buildResponseEnvelope, defineConfig, getCaseTransformer, getDefaultConfig, getGlobalBaseUrl, getGlobalCase, getGlobalCursorMeta, getGlobalPageName, getGlobalPaginatedExtras, getGlobalPaginatedLinks, getGlobalPaginatedMeta, getGlobalResponseFactory, getGlobalResponseRootKey, getGlobalResponseStructure, getGlobalResponseWrap, getPaginationExtraKeys, isArkormLikeCollection, isArkormLikeModel, isPlainObject, loadRuntimeConfig, mergeMetadata, normalizeSerializableData, resetRuntimeConfigForTests, resolveMergeWhen, resolveWhen, resolveWhenNotNull, resolveWithHookMetadata, sanitizeConditionalAttributes, setGlobalBaseUrl, setGlobalCase, setGlobalCursorMeta, setGlobalPageName, setGlobalPaginatedExtras, setGlobalPaginatedLinks, setGlobalPaginatedMeta, setGlobalResponseFactory, setGlobalResponseRootKey, setGlobalResponseStructure, setGlobalResponseWrap, splitWords, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, transformKeys };
|
|
1435
|
+
export { ApiResource, CONDITIONAL_ATTRIBUTE_MISSING, CaseStyle, CliApp, Collectible, CollectionBody, CollectionLike, Config, Cursor, GenericBody, GenericResource, InitCommand, MakeResource, MetaData, NonCollectible, PaginatedMetaData, Pagination, PaginatorLike, ResoraConfig, Resource, ResourceBody, ResourceCollection, ResourceData, ResourceDef, ResourceLevelConfig, ResponseData, ResponseDataCollection, ResponseFactory, ResponseFactoryContext, ResponseKind, ResponseStructureConfig, ServerResponse, appendRootProperties, applyRuntimeConfig, buildPaginationExtras, buildResponseEnvelope, defineConfig, extractRequestUrl, extractResponseFromCtx, getCaseTransformer, getDefaultConfig, getGlobalBaseUrl, getGlobalCase, getGlobalCursorMeta, getGlobalPageName, getGlobalPaginatedExtras, getGlobalPaginatedLinks, getGlobalPaginatedMeta, getGlobalResponseFactory, getGlobalResponseRootKey, getGlobalResponseStructure, getGlobalResponseWrap, getPaginationExtraKeys, getRequestUrl, isArkormLikeCollection, isArkormLikeModel, isPlainObject, loadRuntimeConfig, mergeMetadata, normalizeSerializableData, resetRuntimeConfigForTests, resolveMergeWhen, resolveWhen, resolveWhenNotNull, resolveWithHookMetadata, sanitizeConditionalAttributes, setCtx, setGlobalBaseUrl, setGlobalCase, setGlobalCursorMeta, setGlobalPageName, setGlobalPaginatedExtras, setGlobalPaginatedLinks, setGlobalPaginatedMeta, setGlobalResponseFactory, setGlobalResponseRootKey, setGlobalResponseStructure, setGlobalResponseWrap, setRequestUrl, splitWords, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, transformKeys };
|
package/dist/index.d.mts
CHANGED
|
@@ -460,6 +460,7 @@ declare abstract class BaseSerializer<TResource = any> {
|
|
|
460
460
|
static preferredCase?: CaseStyle;
|
|
461
461
|
static responseStructure?: ResponseStructureConfig;
|
|
462
462
|
static config?: () => ResourceLevelConfig;
|
|
463
|
+
protected static ctx?: Response | H3Event | Record<string, any>;
|
|
463
464
|
protected instanceConfig?: ResourceLevelConfig;
|
|
464
465
|
protected additionalMeta?: MetaData;
|
|
465
466
|
protected called: {
|
|
@@ -475,6 +476,16 @@ declare abstract class BaseSerializer<TResource = any> {
|
|
|
475
476
|
toResponse?: boolean;
|
|
476
477
|
};
|
|
477
478
|
constructor();
|
|
479
|
+
/**
|
|
480
|
+
* Sets the current request context for pagination URL detection.
|
|
481
|
+
* Call from middleware to make the request path available to all
|
|
482
|
+
* resources created during the request lifecycle.
|
|
483
|
+
*
|
|
484
|
+
* Accepts an Express Request, H3Event, `{ req }` object, or a plain URL string.
|
|
485
|
+
*
|
|
486
|
+
* @param ctx The request context.
|
|
487
|
+
*/
|
|
488
|
+
static setCtx(ctx: unknown): void;
|
|
478
489
|
/**
|
|
479
490
|
* Helper method to conditionally resolve a value based on a condition.
|
|
480
491
|
*
|
|
@@ -615,9 +626,9 @@ declare abstract class BaseSerializer<TResource = any> {
|
|
|
615
626
|
* @see BaseSerializer for shared serialization logic and configuration handling
|
|
616
627
|
*/
|
|
617
628
|
declare class ResourceCollection<R extends ResourceData[] | Collectible | CollectionLike | PaginatorLike = ResourceData[] | Collectible | CollectionLike | PaginatorLike, T extends ResourceData = any> extends BaseSerializer<R> {
|
|
618
|
-
private res?;
|
|
619
629
|
[key: string]: any;
|
|
620
630
|
private body;
|
|
631
|
+
private res?;
|
|
621
632
|
resource: R;
|
|
622
633
|
collects?: typeof Resource<T>;
|
|
623
634
|
protected withResponseContext?: {
|
|
@@ -632,7 +643,7 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
632
643
|
*/
|
|
633
644
|
isPaginatedCollectible(value: unknown): value is Collectible;
|
|
634
645
|
constructor(rsc: R);
|
|
635
|
-
constructor(rsc: R,
|
|
646
|
+
constructor(rsc: R, ctx: Response | H3Event | Record<string, any>);
|
|
636
647
|
/**
|
|
637
648
|
* Get the original resource data
|
|
638
649
|
*/
|
|
@@ -702,8 +713,16 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
702
713
|
* @returns
|
|
703
714
|
*/
|
|
704
715
|
additional<X extends Record<string, any>>(extra: X): this;
|
|
716
|
+
/**
|
|
717
|
+
* Build a response object, optionally accepting a raw response to mutate in withResponse.
|
|
718
|
+
*/
|
|
705
719
|
response(): ServerResponse<CollectionBody<R>>;
|
|
720
|
+
/**
|
|
721
|
+
* Build a response object, optionally accepting a raw response to mutate in withResponse.
|
|
722
|
+
* @param res Optional raw response object (e.g. Express Response or H3Event res)
|
|
723
|
+
*/
|
|
706
724
|
response(res: H3Event['res']): ServerResponse<CollectionBody<R>>;
|
|
725
|
+
response(res: Response): ServerResponse<CollectionBody<R>>;
|
|
707
726
|
/**
|
|
708
727
|
* Customize the outgoing transport response right before dispatch.
|
|
709
728
|
*
|
|
@@ -745,15 +764,15 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
745
764
|
* @see BaseSerializer for shared serialization logic and configuration handling
|
|
746
765
|
*/
|
|
747
766
|
declare class Resource<R extends ResourceData | NonCollectible = ResourceData> extends BaseSerializer<R> {
|
|
748
|
-
private res?;
|
|
749
767
|
[key: string]: any;
|
|
750
768
|
private body;
|
|
769
|
+
private res?;
|
|
751
770
|
resource: R;
|
|
752
771
|
protected withResponseContext?: {
|
|
753
772
|
response: ServerResponse<ResourceBody<R>>;
|
|
754
773
|
raw: Response | H3Event['res'];
|
|
755
774
|
};
|
|
756
|
-
constructor(rsc: R,
|
|
775
|
+
constructor(rsc: R, ctx?: Response | H3Event | Record<string, any>);
|
|
757
776
|
/**
|
|
758
777
|
* Create a ResourceCollection from an array of resource data or a Collectible instance
|
|
759
778
|
*
|
|
@@ -819,6 +838,7 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> e
|
|
|
819
838
|
* @param res Optional raw response object (e.g. Express Response or H3Event res)
|
|
820
839
|
*/
|
|
821
840
|
response(res: H3Event['res']): ServerResponse<ResourceBody<R>>;
|
|
841
|
+
response(res: Response): ServerResponse<ResourceBody<R>>;
|
|
822
842
|
/**
|
|
823
843
|
* Customize the outgoing transport response right before dispatch.
|
|
824
844
|
*
|
|
@@ -858,16 +878,16 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> e
|
|
|
858
878
|
* @see BaseSerializer for shared serialization logic and configuration handling
|
|
859
879
|
*/
|
|
860
880
|
declare class GenericResource<R extends NonCollectible | Collectible | CollectionLike | PaginatorLike | ResourceData = ResourceData, T extends ResourceData = any> extends BaseSerializer<R> {
|
|
861
|
-
private res?;
|
|
862
881
|
[key: string]: any;
|
|
863
882
|
private body;
|
|
883
|
+
private res?;
|
|
864
884
|
resource: R;
|
|
865
885
|
collects?: typeof Resource<T>;
|
|
866
886
|
protected withResponseContext?: {
|
|
867
887
|
response: ServerResponse<GenericBody<R>>;
|
|
868
888
|
raw: Response | H3Event['res'];
|
|
869
889
|
};
|
|
870
|
-
constructor(rsc: R,
|
|
890
|
+
constructor(rsc: R, ctx?: Response | H3Event | Record<string, any>);
|
|
871
891
|
/**
|
|
872
892
|
* Get the original resource data
|
|
873
893
|
*/
|
|
@@ -939,6 +959,7 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
939
959
|
* @param res
|
|
940
960
|
*/
|
|
941
961
|
response(res: H3Event['res']): ServerResponse<GenericBody<R>>;
|
|
962
|
+
response(res: Response): ServerResponse<GenericBody<R>>;
|
|
942
963
|
/**
|
|
943
964
|
* Customize the outgoing transport response right before dispatch.
|
|
944
965
|
*
|
|
@@ -1362,5 +1383,53 @@ declare const setGlobalCursorMeta: (meta: Config["cursorMeta"]) => void;
|
|
|
1362
1383
|
* @returns The global cursor pagination metadata configuration.
|
|
1363
1384
|
*/
|
|
1364
1385
|
declare const getGlobalCursorMeta: () => Config["cursorMeta"];
|
|
1386
|
+
/**
|
|
1387
|
+
* Sets the current request URL, used as a fallback for pagination link generation
|
|
1388
|
+
* when no explicit path is provided in the pagination data.
|
|
1389
|
+
*
|
|
1390
|
+
* @param url The request URL or pathname to set.
|
|
1391
|
+
*/
|
|
1392
|
+
declare const setRequestUrl: (url: string | undefined) => void;
|
|
1393
|
+
/**
|
|
1394
|
+
* Retrieves the current request URL, used as a fallback path for pagination links.
|
|
1395
|
+
*
|
|
1396
|
+
* @returns The current request URL, or undefined if not set.
|
|
1397
|
+
*/
|
|
1398
|
+
declare const getRequestUrl: () => string | undefined;
|
|
1399
|
+
/**
|
|
1400
|
+
* Extracts the request URL pathname (with query string) from an HTTP context.
|
|
1401
|
+
*
|
|
1402
|
+
* Both Express middleware and H3 HTTPEvent expose `{ req, res }`. The `req`
|
|
1403
|
+
* object carries URL information:
|
|
1404
|
+
* - H3 / Web standard `Request`: `req.url` is a full URL string
|
|
1405
|
+
* - Express `Request`: `req.originalUrl` is the pathname + query string
|
|
1406
|
+
*
|
|
1407
|
+
* The function also accepts a plain `req` object directly or a string URL.
|
|
1408
|
+
*
|
|
1409
|
+
* @param ctx An HTTP context `{ req, res }`, a bare `Request` object, or a string URL.
|
|
1410
|
+
* @returns The pathname with query string, or undefined.
|
|
1411
|
+
*/
|
|
1412
|
+
declare const extractRequestUrl: (ctx: unknown) => string | undefined;
|
|
1413
|
+
/**
|
|
1414
|
+
* Extracts the response object from an HTTP context.
|
|
1415
|
+
*
|
|
1416
|
+
* If the context has a `res` property (Express middleware `{ req, res }` or
|
|
1417
|
+
* H3 HTTPEvent), returns `ctx.res`. Otherwise assumes the value is already
|
|
1418
|
+
* a bare response object and returns it directly.
|
|
1419
|
+
*
|
|
1420
|
+
* @param ctx The HTTP context or bare response.
|
|
1421
|
+
* @returns The response object, or undefined.
|
|
1422
|
+
*/
|
|
1423
|
+
declare const extractResponseFromCtx: (ctx: unknown) => any | undefined;
|
|
1424
|
+
/**
|
|
1425
|
+
* Sets the current request context. Extracts the request URL from the provided
|
|
1426
|
+
* context and stores it for use in pagination link generation.
|
|
1427
|
+
*
|
|
1428
|
+
* Can be called from middleware to make the request URL available to all
|
|
1429
|
+
* resources created during the request lifecycle.
|
|
1430
|
+
*
|
|
1431
|
+
* @param ctx An HTTP context `{ req, res }`, Express Request, H3 HTTPEvent, or bare request.
|
|
1432
|
+
*/
|
|
1433
|
+
declare const setCtx: (ctx: unknown) => void;
|
|
1365
1434
|
//#endregion
|
|
1366
|
-
export { ApiResource, CONDITIONAL_ATTRIBUTE_MISSING, CaseStyle, CliApp, Collectible, CollectionBody, CollectionLike, Config, Cursor, GenericBody, GenericResource, InitCommand, MakeResource, MetaData, NonCollectible, PaginatedMetaData, Pagination, PaginatorLike, ResoraConfig, Resource, ResourceBody, ResourceCollection, ResourceData, ResourceDef, ResourceLevelConfig, ResponseData, ResponseDataCollection, ResponseFactory, ResponseFactoryContext, ResponseKind, ResponseStructureConfig, ServerResponse, appendRootProperties, applyRuntimeConfig, buildPaginationExtras, buildResponseEnvelope, defineConfig, getCaseTransformer, getDefaultConfig, getGlobalBaseUrl, getGlobalCase, getGlobalCursorMeta, getGlobalPageName, getGlobalPaginatedExtras, getGlobalPaginatedLinks, getGlobalPaginatedMeta, getGlobalResponseFactory, getGlobalResponseRootKey, getGlobalResponseStructure, getGlobalResponseWrap, getPaginationExtraKeys, isArkormLikeCollection, isArkormLikeModel, isPlainObject, loadRuntimeConfig, mergeMetadata, normalizeSerializableData, resetRuntimeConfigForTests, resolveMergeWhen, resolveWhen, resolveWhenNotNull, resolveWithHookMetadata, sanitizeConditionalAttributes, setGlobalBaseUrl, setGlobalCase, setGlobalCursorMeta, setGlobalPageName, setGlobalPaginatedExtras, setGlobalPaginatedLinks, setGlobalPaginatedMeta, setGlobalResponseFactory, setGlobalResponseRootKey, setGlobalResponseStructure, setGlobalResponseWrap, splitWords, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, transformKeys };
|
|
1435
|
+
export { ApiResource, CONDITIONAL_ATTRIBUTE_MISSING, CaseStyle, CliApp, Collectible, CollectionBody, CollectionLike, Config, Cursor, GenericBody, GenericResource, InitCommand, MakeResource, MetaData, NonCollectible, PaginatedMetaData, Pagination, PaginatorLike, ResoraConfig, Resource, ResourceBody, ResourceCollection, ResourceData, ResourceDef, ResourceLevelConfig, ResponseData, ResponseDataCollection, ResponseFactory, ResponseFactoryContext, ResponseKind, ResponseStructureConfig, ServerResponse, appendRootProperties, applyRuntimeConfig, buildPaginationExtras, buildResponseEnvelope, defineConfig, extractRequestUrl, extractResponseFromCtx, getCaseTransformer, getDefaultConfig, getGlobalBaseUrl, getGlobalCase, getGlobalCursorMeta, getGlobalPageName, getGlobalPaginatedExtras, getGlobalPaginatedLinks, getGlobalPaginatedMeta, getGlobalResponseFactory, getGlobalResponseRootKey, getGlobalResponseStructure, getGlobalResponseWrap, getPaginationExtraKeys, getRequestUrl, isArkormLikeCollection, isArkormLikeModel, isPlainObject, loadRuntimeConfig, mergeMetadata, normalizeSerializableData, resetRuntimeConfigForTests, resolveMergeWhen, resolveWhen, resolveWhenNotNull, resolveWithHookMetadata, sanitizeConditionalAttributes, setCtx, setGlobalBaseUrl, setGlobalCase, setGlobalCursorMeta, setGlobalPageName, setGlobalPaginatedExtras, setGlobalPaginatedLinks, setGlobalPaginatedMeta, setGlobalResponseFactory, setGlobalResponseRootKey, setGlobalResponseStructure, setGlobalResponseWrap, setRequestUrl, splitWords, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, transformKeys };
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import{copyFileSync as e,existsSync as t,mkdirSync as n,readFileSync as r,rmSync as i,writeFileSync as a}from"fs";import o,{dirname as s,join as c}from"path";import{createRequire as l}from"module";import{pathToFileURL as u}from"url";import{Command as d}from"@h3ravel/musket";function f(e){return e}let p,m,h=[`meta`,`links`],g={first:`first`,last:`last`,prev:`prev`,next:`next`},_=`
|
|
2
|
-
`),u=`import ${s} from './${s}'\n`,d=(!!n?.collection||e.endsWith(`Collection`))&&t(c(this.config.resourcesDir,`${s}.ts`)),f=this.generateFile(o,a,{ResourceName:r,CollectionResourceName:r.replace(/(Resource|Collection)$/,``)+`Resource`,"collects = Resource":d?l:``,"import = Resource":d?u:``},n);return{name:r,path:f}}},
|
|
1
|
+
import{copyFileSync as e,existsSync as t,mkdirSync as n,readFileSync as r,rmSync as i,writeFileSync as a}from"fs";import o,{dirname as s,join as c}from"path";import{createRequire as l}from"module";import{pathToFileURL as u}from"url";import{Command as d}from"@h3ravel/musket";function f(e){return e}let p,m,h=[`meta`,`links`],g={first:`first`,last:`last`,prev:`prev`,next:`next`},_=``,v=`page`,ee={to:`to`,from:`from`,links:`links`,path:`path`,total:`total`,per_page:`per_page`,last_page:`last_page`,current_page:`current_page`},y={previous:`previous`,next:`next`};const te=e=>{p=e},ne=()=>p,re=e=>{m=e},ie=()=>m,ae=e=>{m={...m||{},rootKey:e}},oe=e=>{m={...m||{},wrap:e}},se=()=>m?.wrap,ce=()=>m?.rootKey,le=e=>{m={...m||{},factory:e}},ue=()=>m?.factory,b=e=>{h=e},x=()=>h,S=e=>{g={...g,...e}},de=()=>g,fe=e=>{_=e},pe=()=>_,me=e=>{v=e},he=()=>v,ge=e=>{ee=e},_e=()=>ee,ve=e=>{y={...y,...e}},ye=()=>y;let C;const w=e=>{C=e},T=()=>C,E=e=>{if(!e||typeof e!=`object`)return typeof e==`string`?e:void 0;let t=e;return t.req&&typeof t.req==`object`?D(t.req):D(t)},D=e=>{if(typeof e.originalUrl==`string`)return e.originalUrl;if(typeof e.url==`string`)try{let t=new URL(e.url);return t.pathname+t.search}catch{return e.url}},O=e=>{if(!e||typeof e!=`object`)return;let t=e;return t.res&&typeof t.res==`object`?t.res:e},k=e=>{w(E(e))},A=e=>{if(typeof e!=`object`||!e||Array.isArray(e)||e instanceof Date||e instanceof RegExp)return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null},j=(e,t,n=`data`)=>!t||Object.keys(t).length===0?e:Array.isArray(e)?{[n]:e,...t}:A(e)?{...e,...t}:{[n]:e,...t},M=(e,t)=>{if(!t)return e;if(!e)return t;let n={...e};for(let[e,r]of Object.entries(t)){let t=n[e];A(t)&&A(r)?n[e]=M(t,r):n[e]=r}return n},N=e=>{if(!e||typeof e!=`object`)return!1;let t=e;return typeof t.toObject==`function`&&typeof t.getRawAttributes==`function`},P=e=>!e||typeof e!=`object`?!1:typeof e.all==`function`,F=e=>{if(Array.isArray(e))return e.map(e=>F(e));if(N(e))return F(e.toObject());if(P(e)){let t=e.all();return Array.isArray(t)?t.map(e=>F(e)):F(t)}return A(e)?Object.entries(e).reduce((e,[t,n])=>(e[t]=F(n),e),{}):e},I=()=>{let e=x();return Array.isArray(e)?{metaKey:e.includes(`meta`)?`meta`:void 0,linksKey:e.includes(`links`)?`links`:void 0,cursorKey:e.includes(`cursor`)?`cursor`:void 0}:{metaKey:e.meta,linksKey:e.links,cursorKey:e.cursor}},L=(e,t)=>{if(e===void 0)return;let n=t||T()||``,r=pe()||``,i=he()||`page`,a=n.indexOf(`?`),o=a>=0?n.slice(0,a):n,s=a>=0?n.slice(a+1):``;if(/^https?:\/\//i.test(o)){let t=new URL(n);return t.searchParams.set(i,String(e)),t.toString()}let c=r.replace(/\/$/,``),l=o.replace(/^\//,``);if(/^https?:\/\//i.test(c)){let t=l?`${c}/${l}`:c,n=new URL(t);if(s)for(let[e,t]of new URLSearchParams(s))n.searchParams.set(e,t);return n.searchParams.set(i,String(e)),n.toString()}let u=[c,l].filter(Boolean).join(`/`),d=u?`/${u}`:`/`,f=new URLSearchParams(s);return f.set(i,String(e)),`${d}?${f.toString()}`},R=e=>{let{metaKey:t,linksKey:n,cursorKey:r}=I(),i={},a=!!e&&typeof e==`object`&&!!e.meta&&typeof e.meta==`object`&&(Array.isArray(e.data)||P(e.data)),o=a?{first:typeof e.firstPageUrl==`function`?e.firstPageUrl():void 0,last:typeof e.lastPageUrl==`function`?e.lastPageUrl():void 0,prev:typeof e.previousPageUrl==`function`?e.previousPageUrl():void 0,next:typeof e.nextPageUrl==`function`?e.nextPageUrl():void 0}:void 0,s=o?Object.entries(o).reduce((e,[t,n])=>(n!==void 0&&(e[t]=n),e),{}):void 0,c=e?.pagination||(a?{...e.meta,links:e.links||s}:void 0),l=e?.cursor,u={},d={};if(c){let e={to:c.to,from:c.from,links:c.links,path:c.path,total:c.total,per_page:c.perPage,last_page:c.lastPage,current_page:c.currentPage};for(let[t,n]of Object.entries(_e())){if(!n)continue;let r=e[t];r!==void 0&&(u[n]=r)}let t={first:c.links&&Object.prototype.hasOwnProperty.call(c.links,`first`)?c.links.first:L(c.firstPage,c.path),last:c.links&&Object.prototype.hasOwnProperty.call(c.links,`last`)?c.links.last:L(c.lastPage,c.path),prev:c.links&&Object.prototype.hasOwnProperty.call(c.links,`prev`)?c.links.prev:L(c.prevPage,c.path),next:c.links&&Object.prototype.hasOwnProperty.call(c.links,`next`)?c.links.next:L(c.nextPage,c.path)};for(let[e,n]of Object.entries(de())){if(!n)continue;let r=t[e];r!==void 0&&(d[n]=r)}}if(l){let e={},t={previous:l.previous,next:l.next};for(let[n,r]of Object.entries(ye())){if(!r)continue;let i=t[n];i!==void 0&&(e[r]=i)}r&&Object.keys(e).length>0?i[r]=e:Object.keys(e).length>0&&(u.cursor=e)}return t&&Object.keys(u).length>0&&(i[t]=u),n&&Object.keys(d).length>0&&(i[n]=d),i},z=({payload:e,meta:t,metaKey:n=`meta`,wrap:r=!0,rootKey:i=`data`,factory:a,context:o})=>{if(a)return a(e,{...o,rootKey:i,meta:t});if(!r)return t===void 0?e:A(e)?{...e,[n]:t}:{[i]:e,[n]:t};let s={[i]:e};return t!==void 0&&(s[n]=t),s},B=(e,t)=>{let n=e?.with;if(typeof n!=`function`||n===t||n.length>0)return;let r=n.call(e);return A(r)?r:void 0},V=Symbol(`resora.conditional.missing`),H=(e,t)=>e?typeof t==`function`?t():t:V,be=e=>e??V,xe=(e,t)=>{if(!e)return{};let n=typeof t==`function`?t():t;return A(n)?n:{}},U=e=>{if(e===V)return V;if(Array.isArray(e))return e.map(e=>U(e)).filter(e=>e!==V);if(A(e)){let t={};for(let[n,r]of Object.entries(e)){let e=U(r);e!==V&&(t[n]=e)}return t}return e},W=e=>e.replace(/([a-z0-9])([A-Z])/g,`$1 $2`).replace(/([A-Z]+)([A-Z][a-z])/g,`$1 $2`).replace(/[-_\s]+/g,` `).trim().toLowerCase().split(` `).filter(Boolean),Se=e=>W(e).map((e,t)=>t===0?e:e.charAt(0).toUpperCase()+e.slice(1)).join(``),Ce=e=>W(e).join(`_`),we=e=>W(e).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``),Te=e=>W(e).join(`-`),G=e=>{if(typeof e==`function`)return e;switch(e){case`camel`:return Se;case`snake`:return Ce;case`pascal`:return we;case`kebab`:return Te}},K=(e,t)=>e==null?e:Array.isArray(e)?e.map(e=>K(e,t)):e instanceof Date||e instanceof RegExp?e:typeof e==`object`?Object.fromEntries(Object.entries(e).map(([e,n])=>[t(e),K(n,t)])):e;let q=o.resolve(process.cwd(),`node_modules/resora/stubs`);t(q)||(q=o.resolve(process.cwd(),`stubs`));const Ee=()=>({stubsDir:q,preferredCase:`camel`,responseStructure:{wrap:!0,rootKey:`data`},paginatedExtras:[`meta`,`links`],baseUrl:``,pageName:`page`,paginatedLinks:{first:`first`,last:`last`,prev:`prev`,next:`next`},paginatedMeta:{to:`to`,from:`from`,links:`links`,path:`path`,total:`total`,per_page:`per_page`,last_page:`last_page`,current_page:`current_page`},cursorMeta:{previous:`previous`,next:`next`},resourcesDir:`src/resources`,stubs:{config:`resora.config.stub`,resource:`resource.stub`,collection:`resource.collection.stub`}}),J=e=>{let t=Ee();return Object.assign(t,e,{stubs:Object.assign(t.stubs,e.stubs||{})},{cursorMeta:Object.assign(t.cursorMeta,e.cursorMeta||{})},{paginatedMeta:e.paginatedMeta||t.paginatedMeta},{paginatedLinks:Object.assign(t.paginatedLinks,e.paginatedLinks||{})},{responseStructure:Object.assign(t.responseStructure,e.responseStructure||{})})};let Y=!1,X;const De=()=>{Y=!1,X=void 0},Oe=e=>{e.preferredCase!==`camel`&&te(e.preferredCase),re(e.responseStructure),b(e.paginatedExtras),S(e.paginatedLinks),ge(e.paginatedMeta),ve(e.cursorMeta),fe(e.baseUrl),me(e.pageName)},ke=async e=>await import(`${u(e).href}?resora_runtime=${Date.now()}`),Ae=e=>{Oe(J((e?.default??e)||{})),Y=!0},je=()=>{let e=l(import.meta.url),n=[o.join(process.cwd(),`resora.config.cjs`)];for(let r of n)if(t(r))try{return Ae(e(r)),!0}catch{continue}return!1},Z=async()=>{if(!Y){if(X)return await X;je()||(X=(async()=>{let e=[o.join(process.cwd(),`resora.config.js`),o.join(process.cwd(),`resora.config.ts`)];for(let n of e)if(t(n))try{Ae(await ke(n));return}catch{continue}Y=!0})(),await X)}};Z();var Me=class{command;config={};constructor(e={}){this.config=J(e)}async loadConfig(e={}){this.config=J(e);let n=[c(process.cwd(),`resora.config.ts`),c(process.cwd(),`resora.config.js`),c(process.cwd(),`resora.config.cjs`)];for(let e of n)if(t(e))try{let{default:t}=await import(e);Object.assign(this.config,t);break}catch(t){console.error(`Error loading config file at ${e}:`,t)}return this}getConfig(){return this.config}init(){let n=c(process.cwd(),`resora.config.js`),i=c(this.config.stubsDir,this.config.stubs.config);return t(n)&&!this.command.option(`force`)&&(this.command.error(`Error: ${n} already exists.`),process.exit(1)),this.ensureDirectory(n),t(n)&&this.command.option(`force`)&&e(n,n.replace(/\.js$/,`.backup.${Date.now()}.js`)),a(n,r(i,`utf-8`)),{path:n}}ensureDirectory(e){let r=s(e);t(r)||n(r,{recursive:!0})}generateFile(e,n,o,s){t(n)&&!s?.force?(this.command.error(`Error: ${n} already exists.`),process.exit(1)):t(n)&&s?.force&&i(n);let c=r(e,`utf-8`);for(let[e,t]of Object.entries(o))c=c.replace(RegExp(`{{${e}}}`,`g`),t);return this.ensureDirectory(n),a(n,c),n}makeResource(e,n){let r=e;n?.collection&&!e.endsWith(`Collection`)&&!e.endsWith(`Resource`)?r+=`Collection`:!n?.collection&&!e.endsWith(`Resource`)&&!e.endsWith(`Collection`)&&(r+=`Resource`);let i=`${r}.ts`,a=c(this.config.resourcesDir,i),o=c(this.config.stubsDir,n?.collection||e.endsWith(`Collection`)?this.config.stubs.collection:this.config.stubs.resource);t(o)||(this.command.error(`Error: Stub file ${o} not found.`),process.exit(1)),r=r.split(`/`).pop()?.split(`.`).shift();let s=r.replace(/(Resource|Collection)$/,``)+`Resource`,l=[`/**`,` * The resource that this collection collects.`,` */`,`collects = ${s}`].join(`
|
|
2
|
+
`),u=`import ${s} from './${s}'\n`,d=(!!n?.collection||e.endsWith(`Collection`))&&t(c(this.config.resourcesDir,`${s}.ts`)),f=this.generateFile(o,a,{ResourceName:r,CollectionResourceName:r.replace(/(Resource|Collection)$/,``)+`Resource`,"collects = Resource":d?l:``,"import = Resource":d?u:``},n);return{name:r,path:f}}},Ne=class extends d{signature=`init
|
|
3
3
|
{--force : Force overwrite if config file already exists (existing file will be backed up) }
|
|
4
|
-
`;description=`Initialize Resora`;async handle(){this.app.command=this,this.app.init(),this.success(`Resora initialized`)}},
|
|
4
|
+
`;description=`Initialize Resora`;async handle(){this.app.command=this,this.app.init(),this.success(`Resora initialized`)}},Pe=class extends d{signature=`#create:
|
|
5
5
|
{resource : Generates a new resource file.
|
|
6
6
|
| {name : Name of the resource to create}
|
|
7
7
|
| {--c|collection : Make a resource collection}
|
|
@@ -22,4 +22,4 @@ import{copyFileSync as e,existsSync as t,mkdirSync as n,readFileSync as r,rmSync
|
|
|
22
22
|
| _ // _ \/ __|/ _ \| '__/ _, |
|
|
23
23
|
| | \ \ __/\__ \ (_) | | | (_| |
|
|
24
24
|
|_| \_\___||___/\___/|_| \__,_|
|
|
25
|
-
`;var Q=class{_status=200;headers={};constructor(e,t){this.response=e,this.body=t}setStatusCode(e){return this._status=e,`status`in this.response&&typeof this.response.status==`function`?this.response.status(e):`status`in this.response&&(this.response.status=e),this}status(){return this._status}statusText(){if(`statusMessage`in this.response)return this.response.statusMessage;if(`statusText`in this.response)return this.response.statusText}setCookie(e,t,n){return this.#e(`Set-Cookie`,`${e}=${t}; ${Object.entries(n||{}).map(([e,t])=>`${e}=${t}`).join(`; `)}`),this}setHeaders(e){for(let[t,n]of Object.entries(e))this.#e(t,n);return this}header(e,t){return this.#e(e,t),this}#e(e,t){this.headers[e]=t,`headers`in this.response?this.response.headers.set(e,t):`setHeader`in this.response&&this.response.setHeader(e,t)}then(e,t){let n=Promise.resolve(this.body).then(e,t);return`send`in this.response&&this.response.send(this.body),n}catch(e){return this.then(void 0,e)}finally(e){return this.then(e,e)}},$=class{static preferredCase;static responseStructure;static config;instanceConfig;additionalMeta;called={};constructor(){Z()}when(e,t){return B(e,t)}whenNotNull(e){return V(e)}mergeWhen(e,t){return H(e,t)}with(e){if(this.called.with=!0,e===void 0)return this.additionalMeta||{};let t=typeof e==`function`?e(this.getResourceForMeta()):e;return this.additionalMeta=A(this.additionalMeta,t),this.called.json&&this.applyMetaToBody(t,this.resolveCurrentRootKey()),this}withMeta(e){return this.with(e),this}resolveMergedMeta(e){return A(R(this,e),this.additionalMeta)}runResponse(e){this.called.toResponse=!0,e.ensureJson();let t=e.body(),n=e.createServerResponse(e.rawResponse,t);return this.called.withResponse=!0,e.callWithResponse(n,e.rawResponse),n}runThen(e){this.called.then=!0,e.ensureJson();let t=e.body();if(e.rawResponse!==void 0){let n=e.createServerResponse(e.rawResponse,t);this.called.withResponse=!0,e.callWithResponse(n,e.rawResponse)}else this.called.withResponse=!0,e.callWithResponse();let n=e.body(),r=Promise.resolve(n).then(e.onfulfilled,e.onrejected);return e.rawResponse!==void 0&&e.sendRawResponse&&e.sendRawResponse(e.rawResponse,n),r}config(e){return e===void 0?this.instanceConfig||{}:(this.instanceConfig={...this.instanceConfig||{},...e,responseStructure:{...this.instanceConfig?.responseStructure||{},...e.responseStructure||{}}},this)}resolveSerializerConfig(e,t){let n=typeof e.config==`function`?e.config():{};return{preferredCase:this.instanceConfig?.preferredCase??n?.preferredCase,responseStructure:{...n?.responseStructure||{},...this.instanceConfig?.responseStructure||{}}}}resolveSerializerCaseStyle(e,t){return this.resolveSerializerConfig(e,t).preferredCase??e.preferredCase??t?.preferredCase??x()}resolveSerializerResponseStructure(e,t){let n=this.resolveSerializerConfig(e,t),r=C();return{wrap:n.responseStructure?.wrap??e.responseStructure?.wrap??t?.responseStructure?.wrap??r?.wrap??!0,rootKey:n.responseStructure?.rootKey??e.responseStructure?.rootKey??t?.responseStructure?.rootKey??r?.rootKey??`data`,factory:n.responseStructure?.factory??e.responseStructure?.factory??t?.responseStructure?.factory??r?.factory}}},Oe=class e extends ${body={data:{}};resource;collects;withResponseContext;constructor(e,t){super(),this.res=t,this.resource=e;let n=!!this.resource&&typeof this.resource==`object`&&`data`in this.resource,r=n?this.resource.data:void 0,i=!!r&&!Array.isArray(r),a=n?r:this.resource;if(a&&typeof a==`object`&&!Array.isArray(a)&&!M(a)){let e=j(a)?Object.keys(a.toObject()):Object.keys(a);for(let t of e)t in this||Object.defineProperty(this,t,{enumerable:!0,configurable:!0,get:()=>j(a)&&typeof a.getAttribute==`function`?a.getAttribute(t):i?r[t]:this.resource[t],set:e=>{if(j(a)&&typeof a.setAttribute==`function`){a.setAttribute(t,e);return}if(i){r[t]=e;return}this.resource[t]=e}})}}data(){return this.resource}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveCollectsConfig(){let e=this.collects;if(!e)return;let t=typeof e.config==`function`?e.config():{};return{preferredCase:t.preferredCase??e.preferredCase,responseStructure:{...e.responseStructure||{},...t.responseStructure||{}}}}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor,this.resolveCollectsConfig())}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=k(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=N(this.data());Array.isArray(t)&&this.collects&&(t=t.map(e=>new this.collects(e).data())),!Array.isArray(t)&&t&&t.data!==void 0&&(t=t.data),t=U(t);let n=I(this.resource),{metaKey:r}=P(),i=r?n[r]:void 0;r&&delete n[r];let a=this.resolveSerializerCaseStyle(this.constructor,this.resolveCollectsConfig());if(a){let e=G(a);t=K(t,e)}let o=this.resolveMergedMeta(e.prototype.with),{wrap:s,rootKey:c,factory:l}=this.resolveResponseStructure();this.body=L({payload:t,meta:i,metaKey:r,wrap:s,rootKey:c,factory:l,context:{type:`generic`,resource:this.resource}}),this.body=k(this.body,{...n,...o||{}},c)}return this}toObject(){this.called.toObject=!0,this.json();let e=N(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json();let t=e.data;delete e.data,delete e.pagination;let n=this.getPayloadKey();return t&&n&&this.body[n]!==void 0&&(this.body[n]=Array.isArray(this.body[n])?[...this.body[n],...t]:{...this.body[n],...t}),this.body={...this.body,...e},this}response(e){let t=e??this.res;return this.runResponse({ensureJson:()=>this.json(),rawResponse:t,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}then(e,t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:t})}catch(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:e})}finally(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:e})}},ke=class e extends ${body={data:[]};resource;collects;withResponseContext;isPaginatedCollectible(e){if(!e||typeof e!=`object`)return!1;let t=e;return t.pagination&&Array.isArray(t.data)?!0:t.meta&&typeof t.meta==`object`&&`currentPage`in t.meta?Array.isArray(t.data)||M(t.data):!1}constructor(e,t){super(),this.res=t,this.resource=e}data(){return this.toObject()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveCollectsConfig(){let e=this.collects;if(!e)return;let t=typeof e.config==`function`?e.config():{};return{preferredCase:t.preferredCase??e.preferredCase,responseStructure:{...e.responseStructure||{},...t.responseStructure||{}}}}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor,this.resolveCollectsConfig())}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=k(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=this.data();this.collects&&(t=t.map(e=>new this.collects(e).data())),t=N(t),t=U(t);let n=Array.isArray(this.resource)?{}:I(this.resource),{metaKey:r}=P(),i=r?n[r]:void 0;r&&delete n[r];let a=this.resolveSerializerCaseStyle(this.constructor,this.resolveCollectsConfig());if(a){let e=G(a);t=K(t,e)}let o=this.resolveMergedMeta(e.prototype.with),{wrap:s,rootKey:c,factory:l}=this.resolveResponseStructure();this.body=L({payload:t,meta:i,metaKey:r,wrap:s,rootKey:c,factory:l,context:{type:`collection`,resource:this.resource}}),this.body=k(this.body,{...n,...o||{}},c)}return this}toObject(){return this.called.toObject=!0,this.json(),N(Array.isArray(this.resource)?this.resource:M(this.resource)?this.resource.all():this.resource.data)}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json(),delete e.cursor,delete e.pagination;let t=this.getPayloadKey();return e.data&&t&&Array.isArray(this.body[t])&&(this.body[t]=[...this.body[t],...e.data]),this.body={...this.body,...e},this}response(e){let t=e??this.res;return this.runResponse({ensureJson:()=>this.json(),rawResponse:t,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}setCollects(e){return this.collects=e,this}then(e,t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:t})}catch(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:e})}finally(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:e})}},Ae=class e extends ${body={data:{}};resource;withResponseContext;constructor(e,t){super(),this.res=t,this.resource=e;let n=this.resource.data??this.resource;if(!Array.isArray(n)){let e=j(n)?Object.keys(n.toObject()):Object.keys(n);for(let t of e)t in this||Object.defineProperty(this,t,{enumerable:!0,configurable:!0,get:()=>j(n)&&typeof n.getAttribute==`function`?n.getAttribute(t):this.resource.data?.[t]??this.resource[t],set:e=>{if(j(n)&&typeof n.setAttribute==`function`){n.setAttribute(t,e);return}this.resource.data&&this.resource.data[t]?this.resource.data[t]=e:this.resource[t]=e}})}}static collection(e){return new ke(e).setCollects(this)}data(){return this.toObject()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor)}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=k(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=N(this.data());!Array.isArray(t)&&t&&t.data!==void 0&&(t=t.data),t=U(t);let n=this.resolveSerializerCaseStyle(this.constructor);if(n){let e=G(n);t=K(t,e)}let r=this.resolveMergedMeta(e.prototype.with),{wrap:i,rootKey:a,factory:o}=this.resolveResponseStructure();this.body=L({payload:t,wrap:i,rootKey:a,factory:o,context:{type:`resource`,resource:this.resource}}),this.body=k(this.body,r,a)}return this}toObject(){this.called.toObject=!0,this.json();let e=N(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json();let t=this.getPayloadKey();return e.data&&t&&this.body[t]!==void 0&&(this.body[t]=Array.isArray(this.body[t])?[...this.body[t],...e.data]:{...this.body[t],...e.data}),this.body={...this.body,...e},this}response(e){let t=e??this.res;return this.runResponse({ensureJson:()=>this.json(),rawResponse:t,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}then(e,t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:t})}catch(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:e})}finally(e){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:e,onrejected:e})}};export{f as ApiResource,z as CONDITIONAL_ATTRIBUTE_MISSING,Te as CliApp,Oe as GenericResource,Ee as InitCommand,De as MakeResource,Ae as Resource,ke as ResourceCollection,Q as ServerResponse,k as appendRootProperties,xe as applyRuntimeConfig,I as buildPaginationExtras,L as buildResponseEnvelope,J as defineConfig,G as getCaseTransformer,ye as getDefaultConfig,fe as getGlobalBaseUrl,x as getGlobalCase,D as getGlobalCursorMeta,me as getGlobalPageName,ce as getGlobalPaginatedExtras,ue as getGlobalPaginatedLinks,T as getGlobalPaginatedMeta,oe as getGlobalResponseFactory,ie as getGlobalResponseRootKey,C as getGlobalResponseStructure,re as getGlobalResponseWrap,P as getPaginationExtraKeys,M as isArkormLikeCollection,j as isArkormLikeModel,O as isPlainObject,Z as loadRuntimeConfig,A as mergeMetadata,N as normalizeSerializableData,be as resetRuntimeConfigForTests,H as resolveMergeWhen,B as resolveWhen,V as resolveWhenNotNull,R as resolveWithHookMetadata,U as sanitizeConditionalAttributes,de as setGlobalBaseUrl,b as setGlobalCase,E as setGlobalCursorMeta,pe as setGlobalPageName,se as setGlobalPaginatedExtras,le as setGlobalPaginatedLinks,w as setGlobalPaginatedMeta,ae as setGlobalResponseFactory,te as setGlobalResponseRootKey,S as setGlobalResponseStructure,ne as setGlobalResponseWrap,W as splitWords,he as toCamelCase,ve as toKebabCase,_e as toPascalCase,ge as toSnakeCase,K as transformKeys};
|
|
25
|
+
`;var Q=class{_status=200;headers={};constructor(e,t){this.response=e,this.body=t}setStatusCode(e){return this._status=e,`status`in this.response&&typeof this.response.status==`function`?this.response.status(e):`status`in this.response&&(this.response.status=e),this}status(){return this._status}statusText(){if(`statusMessage`in this.response)return this.response.statusMessage;if(`statusText`in this.response)return this.response.statusText}setCookie(e,t,n){return this.#e(`Set-Cookie`,`${e}=${t}; ${Object.entries(n||{}).map(([e,t])=>`${e}=${t}`).join(`; `)}`),this}setHeaders(e){for(let[t,n]of Object.entries(e))this.#e(t,n);return this}header(e,t){return this.#e(e,t),this}#e(e,t){this.headers[e]=t,`headers`in this.response?this.response.headers.set(e,t):`setHeader`in this.response&&this.response.setHeader(e,t)}then(e,t){let n=Promise.resolve(this.body).then(e,t);return`send`in this.response&&this.response.send(this.body),n}catch(e){return this.then(void 0,e)}finally(e){return this.then(e,e)}},$=class{static preferredCase;static responseStructure;static config;static ctx;instanceConfig;additionalMeta;called={};constructor(){Z()}static setCtx(e){k(e),this.ctx=e}when(e,t){return H(e,t)}whenNotNull(e){return be(e)}mergeWhen(e,t){return xe(e,t)}with(e){if(this.called.with=!0,e===void 0)return this.additionalMeta||{};let t=typeof e==`function`?e(this.getResourceForMeta()):e;return this.additionalMeta=M(this.additionalMeta,t),this.called.json&&this.applyMetaToBody(t,this.resolveCurrentRootKey()),this}withMeta(e){return this.with(e),this}resolveMergedMeta(e){return M(B(this,e),this.additionalMeta)}runResponse(e){this.called.toResponse=!0,e.ensureJson();let t=e.body(),n=e.createServerResponse(e.rawResponse,t);return this.called.withResponse=!0,e.callWithResponse(n,e.rawResponse),n}runThen(e){this.called.then=!0,e.ensureJson();let t=e.body();if(e.rawResponse!==void 0){let n=e.createServerResponse(e.rawResponse,t);this.called.withResponse=!0,e.callWithResponse(n,e.rawResponse)}else this.called.withResponse=!0,e.callWithResponse();let n=e.body(),r=Promise.resolve(n).then(e.onfulfilled,e.onrejected);return e.rawResponse!==void 0&&e.sendRawResponse&&e.sendRawResponse(e.rawResponse,n),r}config(e){return e===void 0?this.instanceConfig||{}:(this.instanceConfig={...this.instanceConfig||{},...e,responseStructure:{...this.instanceConfig?.responseStructure||{},...e.responseStructure||{}}},this)}resolveSerializerConfig(e,t){let n=typeof e.config==`function`?e.config():{};return{preferredCase:this.instanceConfig?.preferredCase??n?.preferredCase,responseStructure:{...n?.responseStructure||{},...this.instanceConfig?.responseStructure||{}}}}resolveSerializerCaseStyle(e,t){return this.resolveSerializerConfig(e,t).preferredCase??e.preferredCase??t?.preferredCase??ne()}resolveSerializerResponseStructure(e,t){let n=this.resolveSerializerConfig(e,t),r=ie();return{wrap:n.responseStructure?.wrap??e.responseStructure?.wrap??t?.responseStructure?.wrap??r?.wrap??!0,rootKey:n.responseStructure?.rootKey??e.responseStructure?.rootKey??t?.responseStructure?.rootKey??r?.rootKey??`data`,factory:n.responseStructure?.factory??e.responseStructure?.factory??t?.responseStructure?.factory??r?.factory}}},Fe=class e extends ${body={data:{}};res;resource;collects;withResponseContext;constructor(t,n){if(super(),n&&(e.ctx=n),this.resource=t,n){let e=E(n);e&&w(e),this.res=O(n)}let r=!!this.resource&&typeof this.resource==`object`&&`data`in this.resource,i=r?this.resource.data:void 0,a=!!i&&!Array.isArray(i),o=r?i:this.resource;if(o&&typeof o==`object`&&!Array.isArray(o)&&!P(o)){let e=N(o)?Object.keys(o.toObject()):Object.keys(o);for(let t of e)t in this||Object.defineProperty(this,t,{enumerable:!0,configurable:!0,get:()=>N(o)&&typeof o.getAttribute==`function`?o.getAttribute(t):a?i[t]:this.resource[t],set:e=>{if(N(o)&&typeof o.setAttribute==`function`){o.setAttribute(t,e);return}if(a){i[t]=e;return}this.resource[t]=e}})}}data(){return this.resource}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveCollectsConfig(){let e=this.collects;if(!e)return;let t=typeof e.config==`function`?e.config():{};return{preferredCase:t.preferredCase??e.preferredCase,responseStructure:{...e.responseStructure||{},...t.responseStructure||{}}}}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor,this.resolveCollectsConfig())}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=j(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=F(this.data());Array.isArray(t)&&this.collects&&(t=t.map(e=>new this.collects(e).data())),!Array.isArray(t)&&t&&t.data!==void 0&&(t=t.data),t=U(t);let n=R(this.resource),{metaKey:r}=I(),i=r?n[r]:void 0;r&&delete n[r];let a=this.resolveSerializerCaseStyle(this.constructor,this.resolveCollectsConfig());if(a){let e=G(a);t=K(t,e)}let o=this.resolveMergedMeta(e.prototype.with),{wrap:s,rootKey:c,factory:l}=this.resolveResponseStructure();this.body=z({payload:t,meta:i,metaKey:r,wrap:s,rootKey:c,factory:l,context:{type:`generic`,resource:this.resource}}),this.body=j(this.body,{...n,...o||{}},c)}return this}toObject(){this.called.toObject=!0,this.json();let e=F(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json();let t=e.data;delete e.data,delete e.pagination;let n=this.getPayloadKey();return t&&n&&this.body[n]!==void 0&&(this.body[n]=Array.isArray(this.body[n])?[...this.body[n],...t]:{...this.body[n],...t}),this.body={...this.body,...e},this}response(t){let n=t??this.res??e.ctx?.res??e.ctx;return this.runResponse({ensureJson:()=>this.json(),rawResponse:n,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}then(t,n){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:n})}catch(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:t})}finally(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:t})}},Ie=class e extends ${body={data:[]};res;resource;collects;withResponseContext;isPaginatedCollectible(e){if(!e||typeof e!=`object`)return!1;let t=e;return t.pagination&&Array.isArray(t.data)?!0:t.meta&&typeof t.meta==`object`&&`currentPage`in t.meta?Array.isArray(t.data)||P(t.data):!1}constructor(t,n){if(super(),n&&(e.ctx=n),this.resource=t,n){let e=E(n);e&&w(e),this.res=O(n)}}data(){return this.toObject()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveCollectsConfig(){let e=this.collects;if(!e)return;let t=typeof e.config==`function`?e.config():{};return{preferredCase:t.preferredCase??e.preferredCase,responseStructure:{...e.responseStructure||{},...t.responseStructure||{}}}}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor,this.resolveCollectsConfig())}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=j(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=this.data();this.collects&&(t=t.map(e=>new this.collects(e).data())),t=F(t),t=U(t);let n=Array.isArray(this.resource)?{}:R(this.resource),{metaKey:r}=I(),i=r?n[r]:void 0;r&&delete n[r];let a=this.resolveSerializerCaseStyle(this.constructor,this.resolveCollectsConfig());if(a){let e=G(a);t=K(t,e)}let o=this.resolveMergedMeta(e.prototype.with),{wrap:s,rootKey:c,factory:l}=this.resolveResponseStructure();this.body=z({payload:t,meta:i,metaKey:r,wrap:s,rootKey:c,factory:l,context:{type:`collection`,resource:this.resource}}),this.body=j(this.body,{...n,...o||{}},c)}return this}toObject(){return this.called.toObject=!0,this.json(),F(Array.isArray(this.resource)?this.resource:P(this.resource)?this.resource.all():this.resource.data)}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json(),delete e.cursor,delete e.pagination;let t=this.getPayloadKey();return e.data&&t&&Array.isArray(this.body[t])&&(this.body[t]=[...this.body[t],...e.data]),this.body={...this.body,...e},this}response(t){let n=t??this.res??e.ctx?.res??e.ctx;return this.runResponse({ensureJson:()=>this.json(),rawResponse:n,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}setCollects(e){return this.collects=e,this}then(t,n){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:n})}catch(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:t})}finally(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:t})}},Le=class e extends ${body={data:{}};res;resource;withResponseContext;constructor(t,n){if(super(),n&&(e.ctx=n),this.resource=t,n){let e=E(n);e&&w(e),this.res=O(n)}let r=this.resource.data??this.resource;if(!Array.isArray(r)){let e=N(r)?Object.keys(r.toObject()):Object.keys(r);for(let t of e)t in this||Object.defineProperty(this,t,{enumerable:!0,configurable:!0,get:()=>N(r)&&typeof r.getAttribute==`function`?r.getAttribute(t):this.resource.data?.[t]??this.resource[t],set:e=>{if(N(r)&&typeof r.setAttribute==`function`){r.setAttribute(t,e);return}this.resource.data&&this.resource.data[t]?this.resource.data[t]=e:this.resource[t]=e}})}}static collection(e){return new Ie(e).setCollects(this)}data(){return this.toObject()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}resolveResponseStructure(){return this.resolveSerializerResponseStructure(this.constructor)}resolveCurrentRootKey(){return this.resolveResponseStructure().rootKey}applyMetaToBody(e,t){this.body=j(this.body,e,t)}getResourceForMeta(){return this.resource}getPayloadKey(){let{wrap:e,rootKey:t,factory:n}=this.resolveResponseStructure();return n||!e?void 0:t}json(){if(!this.called.json){this.called.json=!0;let t=F(this.data());!Array.isArray(t)&&t&&t.data!==void 0&&(t=t.data),t=U(t);let n=this.resolveSerializerCaseStyle(this.constructor);if(n){let e=G(n);t=K(t,e)}let r=this.resolveMergedMeta(e.prototype.with),{wrap:i,rootKey:a,factory:o}=this.resolveResponseStructure();this.body=z({payload:t,wrap:i,rootKey:a,factory:o,context:{type:`resource`,resource:this.resource}}),this.body=j(this.body,r,a)}return this}toObject(){this.called.toObject=!0,this.json();let e=F(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}toArray(){return this.called.toArray=!0,this.toObject()}additional(e){this.called.additional=!0,this.json();let t=this.getPayloadKey();return e.data&&t&&this.body[t]!==void 0&&(this.body[t]=Array.isArray(this.body[t])?[...this.body[t],...e.data]:{...this.body[t],...e.data}),this.body={...this.body,...e},this}response(t){let n=t??this.res??e.ctx?.res??e.ctx;return this.runResponse({ensureJson:()=>this.json(),rawResponse:n,body:()=>this.body,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)}})}withResponse(e,t){return this}then(t,n){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:n})}catch(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onrejected:t})}finally(t){return this.runThen({ensureJson:()=>this.json(),body:()=>this.body,rawResponse:this.res??e.ctx?.res??e.ctx,createServerResponse:(e,t)=>{let n=new Q(e,t);return this.withResponseContext={response:n,raw:e},n},callWithResponse:(e,t)=>{this.withResponse(e,t)},sendRawResponse:(e,t)=>{e.send(t)},onfulfilled:t,onrejected:t})}};export{f as ApiResource,V as CONDITIONAL_ATTRIBUTE_MISSING,Me as CliApp,Fe as GenericResource,Ne as InitCommand,Pe as MakeResource,Le as Resource,Ie as ResourceCollection,Q as ServerResponse,j as appendRootProperties,Oe as applyRuntimeConfig,R as buildPaginationExtras,z as buildResponseEnvelope,J as defineConfig,E as extractRequestUrl,O as extractResponseFromCtx,G as getCaseTransformer,Ee as getDefaultConfig,pe as getGlobalBaseUrl,ne as getGlobalCase,ye as getGlobalCursorMeta,he as getGlobalPageName,x as getGlobalPaginatedExtras,de as getGlobalPaginatedLinks,_e as getGlobalPaginatedMeta,ue as getGlobalResponseFactory,ce as getGlobalResponseRootKey,ie as getGlobalResponseStructure,se as getGlobalResponseWrap,I as getPaginationExtraKeys,T as getRequestUrl,P as isArkormLikeCollection,N as isArkormLikeModel,A as isPlainObject,Z as loadRuntimeConfig,M as mergeMetadata,F as normalizeSerializableData,De as resetRuntimeConfigForTests,xe as resolveMergeWhen,H as resolveWhen,be as resolveWhenNotNull,B as resolveWithHookMetadata,U as sanitizeConditionalAttributes,k as setCtx,fe as setGlobalBaseUrl,te as setGlobalCase,ve as setGlobalCursorMeta,me as setGlobalPageName,b as setGlobalPaginatedExtras,S as setGlobalPaginatedLinks,ge as setGlobalPaginatedMeta,le as setGlobalResponseFactory,ae as setGlobalResponseRootKey,re as setGlobalResponseStructure,oe as setGlobalResponseWrap,w as setRequestUrl,W as splitWords,Se as toCamelCase,Te as toKebabCase,we as toPascalCase,Ce as toSnakeCase,K as transformKeys};
|
package/package.json
CHANGED