resora 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +4 -4
- package/dist/index.d.cts +102 -117
- package/dist/index.d.mts +102 -117
- package/dist/index.mjs +4 -4
- package/package.json +1 -1
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,
|
|
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`},_=`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={...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,U=(e,t)=>{if(!e)return{};let n=typeof t==`function`?t():t;return k(n)?n:{}},W=e=>{if(e===B)return B;if(Array.isArray(e))return e.map(e=>W(e)).filter(e=>e!==B);if(k(e)){let t={};for(let[n,r]of Object.entries(e)){let e=W(r);e!==B&&(t[n]=e)}return t}return e},G=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),he=e=>G(e).map((e,t)=>t===0?e:e.charAt(0).toUpperCase()+e.slice(1)).join(``),ge=e=>G(e).join(`_`),_e=e=>G(e).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``),ve=e=>G(e).join(`-`),K=e=>{if(typeof e==`function`)return e;switch(e){case`camel`:return he;case`snake`:return ge;case`pascal`:return _e;case`kebab`:return ve}},q=(e,t)=>e==null?e:Array.isArray(e)?e.map(e=>q(e,t)):e instanceof Date||e instanceof RegExp?e:typeof e==`object`?Object.fromEntries(Object.entries(e).map(([e,n])=>[t(e),q(n,t)])):e;let J=l.default.resolve(process.cwd(),`node_modules/resora/stubs`);(0,c.existsSync)(J)||(J=l.default.resolve(process.cwd(),`stubs`));const ye=()=>({stubsDir:J,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`}}),Y=e=>{let t=ye();return Object.assign(t,e,{stubs:Object.assign(t.stubs,e.stubs||{})},{cursorMeta:Object.assign(t.cursorMeta,e.cursorMeta||{})},{paginatedMeta:Object.assign(t.paginatedMeta,e.paginatedMeta||{})},{paginatedLinks:Object.assign(t.paginatedLinks,e.paginatedLinks||{})},{responseStructure:Object.assign(t.responseStructure,e.responseStructure||{})})};let X=!1,Z;const be=()=>{X=!1,Z=void 0},xe=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)},Se=async e=>await import(`${(0,d.pathToFileURL)(e).href}?resora_runtime=${Date.now()}`),Ce=e=>{xe(Y((e?.default??e)||{})),X=!0},we=()=>{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 Ce(e(n)),!0}catch{continue}return!1},Te=async()=>{if(!X){if(Z)return await Z;we()||(Z=(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{Ce(await Se(t));return}catch{continue}X=!0})(),await Z)}};Te();var Ee=class{command;config={};constructor(e={}){this.config=Y(e)}async loadConfig(e={}){this.config=Y(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}}},De=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`)}},Oe=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 $=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)}},Oe=class e{body={data:{}};resource;collects;additionalMeta;withResponseContext;static preferredCase;static responseStructure;called={};constructor(e,t){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}when(e,t){return B(e,t)}whenNotNull(e){return V(e)}mergeWhen(e,t){return H(e,t)}resolveResponseStructure(){let e=this.constructor.responseStructure,t=this.collects?.responseStructure,n=S();return{wrap:e?.wrap??t?.wrap??n?.wrap??!0,rootKey:e?.rootKey??t?.rootKey??n?.rootKey??`data`,factory:e?.factory??t?.factory??n?.factory}}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.constructor.preferredCase??b();if(a){let e=K(a);t=q(t,e)}let o=A(R(this,e.prototype.with),this.additionalMeta),{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}with(e){if(this.called.with=!0,e===void 0)return this.additionalMeta||{};let t=typeof e==`function`?e(this.resource):e;if(this.additionalMeta=A(this.additionalMeta,t),this.called.json){let{rootKey:e}=this.resolveResponseStructure();this.body=k(this.body,t,e)}return this}withMeta(e){return this.with(e),this}toArray(){this.called.toArray=!0,this.json();let e=N(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}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){this.called.toResponse=!0,this.json();let t=e??this.res,n=new $(t,this.body);return this.withResponseContext={response:n,raw:t},this.called.withResponse=!0,this.withResponse(n,t),n}withResponse(e,t){return this}then(e,t){if(this.called.then=!0,this.json(),this.res){let e=new $(this.res,this.body);this.withResponseContext={response:e,raw:this.res},this.called.withResponse=!0,this.withResponse(e,this.res)}else this.called.withResponse=!0,this.withResponse();let n=Promise.resolve(this.body).then(e,t);return this.res&&this.res.send(this.body),n}},ke=class e{body={data:[]};resource;collects;additionalMeta;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}static preferredCase;static responseStructure;called={};constructor(e,t){this.res=t,this.resource=e}data(){return this.toArray()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}when(e,t){return B(e,t)}whenNotNull(e){return V(e)}mergeWhen(e,t){return H(e,t)}resolveResponseStructure(){let e=this.constructor.responseStructure,t=this.collects?.responseStructure,n=S();return{wrap:e?.wrap??t?.wrap??n?.wrap??!0,rootKey:e?.rootKey??t?.rootKey??n?.rootKey??`data`,factory:e?.factory??t?.factory??n?.factory}}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.constructor.preferredCase??this.collects?.preferredCase??b();if(a){let e=K(a);t=q(t,e)}let o=A(R(this,e.prototype.with),this.additionalMeta),{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}with(e){if(this.called.with=!0,e===void 0)return this.additionalMeta||{};let t=typeof e==`function`?e(this.resource):e;if(this.additionalMeta=A(this.additionalMeta,t),this.called.json){let{rootKey:e}=this.resolveResponseStructure();this.body=k(this.body,t,e)}return this}withMeta(e){return this.with(e),this}toArray(){return this.called.toArray=!0,this.json(),N(Array.isArray(this.resource)?this.resource:M(this.resource)?this.resource.all():this.resource.data)}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){this.called.toResponse=!0,this.json();let t=e??this.res,n=new $(t,this.body);return this.withResponseContext={response:n,raw:t},this.called.withResponse=!0,this.withResponse(n,t),n}withResponse(e,t){return this}setCollects(e){return this.collects=e,this}then(e,t){if(this.called.then=!0,this.json(),this.res){let e=new $(this.res,this.body);this.withResponseContext={response:e,raw:this.res},this.called.withResponse=!0,this.withResponse(e,this.res)}else this.called.withResponse=!0,this.withResponse();let n=Promise.resolve(this.body).then(e,t);return this.res&&this.res.send(this.body),n}catch(e){return this.then(void 0,e)}finally(e){return this.then(e,e)}},Ae=class e{body={data:{}};resource;additionalMeta;withResponseContext;static preferredCase;static responseStructure;called={};constructor(e,t){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.toArray()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}when(e,t){return B(e,t)}whenNotNull(e){return V(e)}mergeWhen(e,t){return H(e,t)}resolveResponseStructure(){let e=this.constructor.responseStructure,t=S();return{wrap:e?.wrap??t?.wrap??!0,rootKey:e?.rootKey??t?.rootKey??`data`,factory:e?.factory??t?.factory}}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.constructor.preferredCase??b();if(n){let e=K(n);t=q(t,e)}let r=A(R(this,e.prototype.with),this.additionalMeta),{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}with(e){if(this.called.with=!0,e===void 0)return this.additionalMeta||{};let t=typeof e==`function`?e(this.resource):e;if(this.additionalMeta=A(this.additionalMeta,t),this.called.json){let{rootKey:e}=this.resolveResponseStructure();this.body=k(this.body,t,e)}return this}withMeta(e){return this.with(e),this}toArray(){this.called.toArray=!0,this.json();let e=N(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}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){this.called.toResponse=!0,this.json();let t=e??this.res,n=new $(t,this.body);return this.withResponseContext={response:n,raw:t},this.called.withResponse=!0,this.withResponse(n,t),n}withResponse(e,t){return this}then(e,t){if(this.called.then=!0,this.json(),this.res){let e=new $(this.res,this.body);this.withResponseContext={response:e,raw:this.res},this.called.withResponse=!0,this.withResponse(e,this.res)}else this.called.withResponse=!0,this.withResponse();let n=Promise.resolve(this.body).then(e,t);return this.res&&this.res.send(this.body),n}catch(e){return this.then(void 0,e)}finally(e){return this.then(e,e)}};exports.ApiResource=ee,exports.CONDITIONAL_ATTRIBUTE_MISSING=z,exports.CliApp=Te,exports.GenericResource=Oe,exports.InitCommand=Ee,exports.MakeResource=De,exports.Resource=Ae,exports.ResourceCollection=ke,exports.ServerResponse=$,exports.appendRootProperties=k,exports.applyRuntimeConfig=xe,exports.buildPaginationExtras=I,exports.buildResponseEnvelope=L,exports.defineConfig=Y,exports.getCaseTransformer=K,exports.getDefaultConfig=ye,exports.getGlobalBaseUrl=le,exports.getGlobalCase=b,exports.getGlobalCursorMeta=he,exports.getGlobalPageName=de,exports.getGlobalPaginatedExtras=w,exports.getGlobalPaginatedLinks=E,exports.getGlobalPaginatedMeta=pe,exports.getGlobalResponseFactory=ce,exports.getGlobalResponseRootKey=oe,exports.getGlobalResponseStructure=S,exports.getGlobalResponseWrap=ae,exports.getPaginationExtraKeys=P,exports.isArkormLikeCollection=M,exports.isArkormLikeModel=j,exports.isPlainObject=O,exports.loadRuntimeConfig=we,exports.mergeMetadata=A,exports.normalizeSerializableData=N,exports.resetRuntimeConfigForTests=be,exports.resolveMergeWhen=H,exports.resolveWhen=B,exports.resolveWhenNotNull=V,exports.resolveWithHookMetadata=R,exports.sanitizeConditionalAttributes=U,exports.setGlobalBaseUrl=D,exports.setGlobalCase=ne,exports.setGlobalCursorMeta=me,exports.setGlobalPageName=ue,exports.setGlobalPaginatedExtras=C,exports.setGlobalPaginatedLinks=T,exports.setGlobalPaginatedMeta=fe,exports.setGlobalResponseFactory=se,exports.setGlobalResponseRootKey=re,exports.setGlobalResponseStructure=x,exports.setGlobalResponseWrap=ie,exports.splitWords=W,exports.toCamelCase=G,exports.toKebabCase=ve,exports.toPascalCase=_e,exports.toSnakeCase=ge,exports.transformKeys=q;
|
|
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={};when(e,t){return V(e,t)}whenNotNull(e){return H(e)}mergeWhen(e,t){return U(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=W(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=K(a);t=q(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}toArray(){this.called.toArray=!0,this.json();let e=P(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}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.toArray()}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=W(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=K(a);t=q(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}toArray(){return this.called.toArray=!0,this.json(),P(Array.isArray(this.resource)?this.resource:N(this.resource)?this.resource.all():this.resource.data)}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.toArray()}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=W(t);let n=this.resolveSerializerCaseStyle(this.constructor);if(n){let e=K(n);t=q(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}toArray(){this.called.toArray=!0,this.json();let e=P(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}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=xe,exports.buildPaginationExtras=L,exports.buildResponseEnvelope=R,exports.defineConfig=Y,exports.getCaseTransformer=K,exports.getDefaultConfig=ye,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=Te,exports.mergeMetadata=j,exports.normalizeSerializableData=P,exports.resetRuntimeConfigForTests=be,exports.resolveMergeWhen=U,exports.resolveWhen=V,exports.resolveWhenNotNull=H,exports.resolveWithHookMetadata=z,exports.sanitizeConditionalAttributes=W,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=G,exports.toCamelCase=he,exports.toKebabCase=ve,exports.toPascalCase=_e,exports.toSnakeCase=ge,exports.transformKeys=q;
|
package/dist/index.d.cts
CHANGED
|
@@ -81,6 +81,10 @@ interface Cursor {
|
|
|
81
81
|
}
|
|
82
82
|
//#endregion
|
|
83
83
|
//#region src/types/resource.d.ts
|
|
84
|
+
interface ResourceLevelConfig {
|
|
85
|
+
preferredCase?: CaseStyle | undefined;
|
|
86
|
+
responseStructure?: ResponseStructureConfig | undefined;
|
|
87
|
+
}
|
|
84
88
|
interface MetaData {
|
|
85
89
|
[key: string]: any;
|
|
86
90
|
}
|
|
@@ -412,32 +416,88 @@ declare class ServerResponse<R extends NonCollectible | Collectible | ResourceDa
|
|
|
412
416
|
finally(onfinally?: (() => void) | null): Promise<void>;
|
|
413
417
|
}
|
|
414
418
|
//#endregion
|
|
419
|
+
//#region src/BaseSerializer.d.ts
|
|
420
|
+
interface SerializerConstructor {
|
|
421
|
+
preferredCase?: CaseStyle;
|
|
422
|
+
responseStructure?: ResponseStructureConfig;
|
|
423
|
+
config?: () => ResourceLevelConfig;
|
|
424
|
+
}
|
|
425
|
+
declare abstract class BaseSerializer<TResource = any> {
|
|
426
|
+
static preferredCase?: CaseStyle;
|
|
427
|
+
static responseStructure?: ResponseStructureConfig;
|
|
428
|
+
static config?: () => ResourceLevelConfig;
|
|
429
|
+
protected instanceConfig?: ResourceLevelConfig;
|
|
430
|
+
protected additionalMeta?: MetaData;
|
|
431
|
+
protected called: {
|
|
432
|
+
json?: boolean;
|
|
433
|
+
data?: boolean;
|
|
434
|
+
toArray?: boolean;
|
|
435
|
+
additional?: boolean;
|
|
436
|
+
with?: boolean;
|
|
437
|
+
withResponse?: boolean;
|
|
438
|
+
status?: boolean;
|
|
439
|
+
then?: boolean;
|
|
440
|
+
toResponse?: boolean;
|
|
441
|
+
};
|
|
442
|
+
when<T>(condition: any, value: T | (() => T)): T | undefined;
|
|
443
|
+
whenNotNull<T>(value: T | null | undefined): T | undefined;
|
|
444
|
+
mergeWhen<T extends Record<string, any>>(condition: any, value: T | (() => T)): Partial<T>;
|
|
445
|
+
protected abstract resolveCurrentRootKey(): string;
|
|
446
|
+
protected abstract applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
447
|
+
protected abstract getResourceForMeta(): TResource;
|
|
448
|
+
with(meta?: any): any;
|
|
449
|
+
withMeta<M extends MetaData>(meta: M | ((resource: TResource) => M)): this;
|
|
450
|
+
protected resolveMergedMeta(withMethod: (meta?: any) => any): Record<string, any> | undefined;
|
|
451
|
+
protected runResponse<TBody, TRawResponse, TServerResponse>(input: {
|
|
452
|
+
ensureJson: () => void;
|
|
453
|
+
rawResponse: TRawResponse;
|
|
454
|
+
body: () => TBody;
|
|
455
|
+
createServerResponse: (raw: TRawResponse, body: TBody) => TServerResponse;
|
|
456
|
+
callWithResponse: (response: TServerResponse, raw: TRawResponse) => void;
|
|
457
|
+
}): TServerResponse;
|
|
458
|
+
protected runThen<TBody, TRawResponse, TServerResponse, TResult1, TResult2>(input: {
|
|
459
|
+
ensureJson: () => void;
|
|
460
|
+
body: () => TBody;
|
|
461
|
+
rawResponse?: TRawResponse;
|
|
462
|
+
createServerResponse: (raw: TRawResponse, body: TBody) => TServerResponse;
|
|
463
|
+
callWithResponse: (response?: TServerResponse, raw?: TRawResponse) => void;
|
|
464
|
+
sendRawResponse?: (raw: TRawResponse, body: TBody) => void;
|
|
465
|
+
onfulfilled?: ((value: TBody) => TResult1 | PromiseLike<TResult1>) | null;
|
|
466
|
+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null;
|
|
467
|
+
}): Promise<TResult1 | TResult2>;
|
|
468
|
+
config(): ResourceLevelConfig;
|
|
469
|
+
config(config: ResourceLevelConfig): this;
|
|
470
|
+
protected resolveSerializerConfig(localConstructor: SerializerConstructor, _fallbackConfig?: ResourceLevelConfig): {
|
|
471
|
+
preferredCase: CaseStyle | undefined;
|
|
472
|
+
responseStructure: {
|
|
473
|
+
wrap?: boolean | undefined;
|
|
474
|
+
rootKey?: string | undefined;
|
|
475
|
+
factory?: ResponseFactory | undefined;
|
|
476
|
+
};
|
|
477
|
+
};
|
|
478
|
+
protected resolveSerializerCaseStyle(localConstructor: SerializerConstructor, fallbackConfig?: ResourceLevelConfig): CaseStyle | undefined;
|
|
479
|
+
protected resolveSerializerResponseStructure(localConstructor: SerializerConstructor, fallbackConfig?: ResourceLevelConfig): {
|
|
480
|
+
wrap: boolean;
|
|
481
|
+
rootKey: string;
|
|
482
|
+
factory: ResponseFactory | undefined;
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
//#endregion
|
|
415
486
|
//#region src/ResourceCollection.d.ts
|
|
416
487
|
/**
|
|
417
488
|
* ResourceCollection class to handle API resource transformation and response building for collections
|
|
418
489
|
*/
|
|
419
|
-
declare class ResourceCollection<R extends ResourceData[] | Collectible | CollectionLike | PaginatorLike = ResourceData[] | Collectible | CollectionLike | PaginatorLike, T extends ResourceData = any> {
|
|
490
|
+
declare class ResourceCollection<R extends ResourceData[] | Collectible | CollectionLike | PaginatorLike = ResourceData[] | Collectible | CollectionLike | PaginatorLike, T extends ResourceData = any> extends BaseSerializer<R> {
|
|
420
491
|
private res?;
|
|
421
492
|
[key: string]: any;
|
|
422
493
|
private body;
|
|
423
494
|
resource: R;
|
|
424
495
|
collects?: typeof Resource<T>;
|
|
425
|
-
private additionalMeta?;
|
|
426
496
|
protected withResponseContext?: {
|
|
427
497
|
response: ServerResponse<CollectionBody<R>>;
|
|
428
498
|
raw: Response | H3Event['res'];
|
|
429
499
|
};
|
|
430
500
|
private isPaginatedCollectible;
|
|
431
|
-
/**
|
|
432
|
-
* Preferred case style for this collection's output keys.
|
|
433
|
-
* Set on a subclass to override the global default.
|
|
434
|
-
*/
|
|
435
|
-
static preferredCase?: CaseStyle;
|
|
436
|
-
/**
|
|
437
|
-
* Response structure override for this collection class.
|
|
438
|
-
*/
|
|
439
|
-
static responseStructure?: ResponseStructureConfig;
|
|
440
|
-
private called;
|
|
441
501
|
constructor(rsc: R);
|
|
442
502
|
constructor(rsc: R, res: Response);
|
|
443
503
|
/**
|
|
@@ -452,19 +512,11 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
452
512
|
* Replace the current serialized output body.
|
|
453
513
|
*/
|
|
454
514
|
protected setBody(body: CollectionBody<R>): this;
|
|
455
|
-
|
|
456
|
-
* Conditionally include a value in serialized output.
|
|
457
|
-
*/
|
|
458
|
-
when<T>(condition: any, value: T | (() => T)): T | undefined;
|
|
459
|
-
/**
|
|
460
|
-
* Include a value only when it is not null/undefined.
|
|
461
|
-
*/
|
|
462
|
-
whenNotNull<T>(value: T | null | undefined): T | undefined;
|
|
463
|
-
/**
|
|
464
|
-
* Conditionally merge object attributes into serialized output.
|
|
465
|
-
*/
|
|
466
|
-
mergeWhen<T extends Record<string, any>>(condition: any, value: T | (() => T)): Partial<T>;
|
|
515
|
+
private resolveCollectsConfig;
|
|
467
516
|
private resolveResponseStructure;
|
|
517
|
+
protected resolveCurrentRootKey(): string;
|
|
518
|
+
protected applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
519
|
+
protected getResourceForMeta(): R;
|
|
468
520
|
private getPayloadKey;
|
|
469
521
|
/**
|
|
470
522
|
* Convert resource to JSON response format
|
|
@@ -472,20 +524,6 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
472
524
|
* @returns
|
|
473
525
|
*/
|
|
474
526
|
json(): this;
|
|
475
|
-
/**
|
|
476
|
-
* Append structured metadata to the response body.
|
|
477
|
-
*
|
|
478
|
-
* @param meta Metadata object or metadata factory
|
|
479
|
-
* @returns
|
|
480
|
-
*/
|
|
481
|
-
with(meta?: any): any;
|
|
482
|
-
/**
|
|
483
|
-
* Typed fluent metadata helper.
|
|
484
|
-
*
|
|
485
|
-
* @param meta Metadata object or metadata factory
|
|
486
|
-
* @returns
|
|
487
|
-
*/
|
|
488
|
-
withMeta<M extends MetaData>(meta: M | ((resource: R) => M)): this;
|
|
489
527
|
/**
|
|
490
528
|
* Flatten resource to return original data
|
|
491
529
|
*
|
|
@@ -529,33 +567,22 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
529
567
|
* @param onfinally
|
|
530
568
|
* @returns
|
|
531
569
|
*/
|
|
532
|
-
finally(onfinally?: (() => void) | null): Promise<
|
|
570
|
+
finally(onfinally?: (() => void) | null): Promise<unknown>;
|
|
533
571
|
}
|
|
534
572
|
//#endregion
|
|
535
573
|
//#region src/Resource.d.ts
|
|
536
574
|
/**
|
|
537
575
|
* Resource class to handle API resource transformation and response building
|
|
538
576
|
*/
|
|
539
|
-
declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
577
|
+
declare class Resource<R extends ResourceData | NonCollectible = ResourceData> extends BaseSerializer<R> {
|
|
540
578
|
private res?;
|
|
541
579
|
[key: string]: any;
|
|
542
580
|
private body;
|
|
543
581
|
resource: R;
|
|
544
|
-
private additionalMeta?;
|
|
545
582
|
protected withResponseContext?: {
|
|
546
583
|
response: ServerResponse<ResourceBody<R>>;
|
|
547
584
|
raw: Response | H3Event['res'];
|
|
548
585
|
};
|
|
549
|
-
/**
|
|
550
|
-
* Preferred case style for this resource's output keys.
|
|
551
|
-
* Set on a subclass to override the global default.
|
|
552
|
-
*/
|
|
553
|
-
static preferredCase?: CaseStyle;
|
|
554
|
-
/**
|
|
555
|
-
* Response structure override for this resource class.
|
|
556
|
-
*/
|
|
557
|
-
static responseStructure?: ResponseStructureConfig;
|
|
558
|
-
private called;
|
|
559
586
|
constructor(rsc: R, res?: Response | undefined);
|
|
560
587
|
/**
|
|
561
588
|
* Create a ResourceCollection from an array of resource data or a Collectible instance
|
|
@@ -576,19 +603,10 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
|
576
603
|
* Replace the current serialized output body.
|
|
577
604
|
*/
|
|
578
605
|
protected setBody(body: ResourceBody<R>): this;
|
|
579
|
-
/**
|
|
580
|
-
* Conditionally include a value in serialized output.
|
|
581
|
-
*/
|
|
582
|
-
when<T>(condition: any, value: T | (() => T)): T | undefined;
|
|
583
|
-
/**
|
|
584
|
-
* Include a value only when it is not null/undefined.
|
|
585
|
-
*/
|
|
586
|
-
whenNotNull<T>(value: T | null | undefined): T | undefined;
|
|
587
|
-
/**
|
|
588
|
-
* Conditionally merge object attributes into serialized output.
|
|
589
|
-
*/
|
|
590
|
-
mergeWhen<T extends Record<string, any>>(condition: any, value: T | (() => T)): Partial<T>;
|
|
591
606
|
private resolveResponseStructure;
|
|
607
|
+
protected resolveCurrentRootKey(): string;
|
|
608
|
+
protected applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
609
|
+
protected getResourceForMeta(): R;
|
|
592
610
|
private getPayloadKey;
|
|
593
611
|
/**
|
|
594
612
|
* Convert resource to JSON response format
|
|
@@ -596,20 +614,6 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
|
596
614
|
* @returns
|
|
597
615
|
*/
|
|
598
616
|
json(): this;
|
|
599
|
-
/**
|
|
600
|
-
* Append structured metadata to the response body.
|
|
601
|
-
*
|
|
602
|
-
* @param meta Metadata object or metadata factory
|
|
603
|
-
* @returns
|
|
604
|
-
*/
|
|
605
|
-
with(meta?: any): any;
|
|
606
|
-
/**
|
|
607
|
-
* Typed fluent metadata helper.
|
|
608
|
-
*
|
|
609
|
-
* @param meta Metadata object or metadata factory
|
|
610
|
-
* @returns
|
|
611
|
-
*/
|
|
612
|
-
withMeta<M extends MetaData>(meta: M | ((resource: R) => M)): this;
|
|
613
617
|
/**
|
|
614
618
|
* Flatten resource to array format (for collections) or return original data for single resources
|
|
615
619
|
*
|
|
@@ -652,34 +656,23 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
|
652
656
|
* @param onfinally
|
|
653
657
|
* @returns
|
|
654
658
|
*/
|
|
655
|
-
finally(onfinally?: (() => void) | null): Promise<
|
|
659
|
+
finally(onfinally?: (() => void) | null): Promise<unknown>;
|
|
656
660
|
}
|
|
657
661
|
//#endregion
|
|
658
662
|
//#region src/GenericResource.d.ts
|
|
659
663
|
/**
|
|
660
664
|
* GenericResource class to handle API resource transformation and response building
|
|
661
665
|
*/
|
|
662
|
-
declare class GenericResource<R extends NonCollectible | Collectible | CollectionLike | PaginatorLike | ResourceData = ResourceData, T extends ResourceData = any> {
|
|
666
|
+
declare class GenericResource<R extends NonCollectible | Collectible | CollectionLike | PaginatorLike | ResourceData = ResourceData, T extends ResourceData = any> extends BaseSerializer<R> {
|
|
663
667
|
private res?;
|
|
664
668
|
[key: string]: any;
|
|
665
669
|
private body;
|
|
666
670
|
resource: R;
|
|
667
671
|
collects?: typeof Resource<T>;
|
|
668
|
-
private additionalMeta?;
|
|
669
672
|
protected withResponseContext?: {
|
|
670
673
|
response: ServerResponse<GenericBody<R>>;
|
|
671
674
|
raw: Response | H3Event['res'];
|
|
672
675
|
};
|
|
673
|
-
/**
|
|
674
|
-
* Preferred case style for this resource's output keys.
|
|
675
|
-
* Set on a subclass to override the global default.
|
|
676
|
-
*/
|
|
677
|
-
static preferredCase?: CaseStyle;
|
|
678
|
-
/**
|
|
679
|
-
* Response structure override for this generic resource class.
|
|
680
|
-
*/
|
|
681
|
-
static responseStructure?: ResponseStructureConfig;
|
|
682
|
-
private called;
|
|
683
676
|
constructor(rsc: R, res?: Response | undefined);
|
|
684
677
|
/**
|
|
685
678
|
* Get the original resource data
|
|
@@ -693,19 +686,11 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
693
686
|
* Replace the current serialized output body.
|
|
694
687
|
*/
|
|
695
688
|
protected setBody(body: GenericBody<R>): this;
|
|
696
|
-
|
|
697
|
-
* Conditionally include a value in serialized output.
|
|
698
|
-
*/
|
|
699
|
-
when<T>(condition: any, value: T | (() => T)): T | undefined;
|
|
700
|
-
/**
|
|
701
|
-
* Include a value only when it is not null/undefined.
|
|
702
|
-
*/
|
|
703
|
-
whenNotNull<T>(value: T | null | undefined): T | undefined;
|
|
704
|
-
/**
|
|
705
|
-
* Conditionally merge object attributes into serialized output.
|
|
706
|
-
*/
|
|
707
|
-
mergeWhen<T extends Record<string, any>>(condition: any, value: T | (() => T)): Partial<T>;
|
|
689
|
+
private resolveCollectsConfig;
|
|
708
690
|
private resolveResponseStructure;
|
|
691
|
+
protected resolveCurrentRootKey(): string;
|
|
692
|
+
protected applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
693
|
+
protected getResourceForMeta(): R;
|
|
709
694
|
private getPayloadKey;
|
|
710
695
|
/**
|
|
711
696
|
* Convert resource to JSON response format
|
|
@@ -713,20 +698,6 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
713
698
|
* @returns
|
|
714
699
|
*/
|
|
715
700
|
json(): this;
|
|
716
|
-
/**
|
|
717
|
-
* Append structured metadata to the response body.
|
|
718
|
-
*
|
|
719
|
-
* @param meta Metadata object or metadata factory
|
|
720
|
-
* @returns
|
|
721
|
-
*/
|
|
722
|
-
with(meta?: any): any;
|
|
723
|
-
/**
|
|
724
|
-
* Typed fluent metadata helper.
|
|
725
|
-
*
|
|
726
|
-
* @param meta Metadata object or metadata factory
|
|
727
|
-
* @returns
|
|
728
|
-
*/
|
|
729
|
-
withMeta<M extends MetaData>(meta: M | ((resource: R) => M)): this;
|
|
730
701
|
/**
|
|
731
702
|
* Convert resource to array format (for collections)
|
|
732
703
|
*
|
|
@@ -756,6 +727,20 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
756
727
|
* @returns A promise that resolves to the result of the onfulfilled or onrejected callback
|
|
757
728
|
*/
|
|
758
729
|
then<TResult1 = GenericBody<R>, TResult2 = never>(onfulfilled?: ((value: GenericBody<R>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
730
|
+
/**
|
|
731
|
+
* Promise-like catch method to handle rejected state of the promise
|
|
732
|
+
*
|
|
733
|
+
* @param onrejected
|
|
734
|
+
* @returns
|
|
735
|
+
*/
|
|
736
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<GenericBody<R> | TResult>;
|
|
737
|
+
/**
|
|
738
|
+
* Promise-like finally method to handle cleanup after promise is settled
|
|
739
|
+
*
|
|
740
|
+
* @param onfinally
|
|
741
|
+
* @returns
|
|
742
|
+
*/
|
|
743
|
+
finally(onfinally?: (() => void) | null): Promise<unknown>;
|
|
759
744
|
}
|
|
760
745
|
//#endregion
|
|
761
746
|
//#region src/utilities/case.d.ts
|
|
@@ -1126,4 +1111,4 @@ declare const setGlobalCursorMeta: (meta: Config["cursorMeta"]) => void;
|
|
|
1126
1111
|
*/
|
|
1127
1112
|
declare const getGlobalCursorMeta: () => Config["cursorMeta"];
|
|
1128
1113
|
//#endregion
|
|
1129
|
-
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, 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 };
|
|
1114
|
+
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 };
|
package/dist/index.d.mts
CHANGED
|
@@ -81,6 +81,10 @@ interface Cursor {
|
|
|
81
81
|
}
|
|
82
82
|
//#endregion
|
|
83
83
|
//#region src/types/resource.d.ts
|
|
84
|
+
interface ResourceLevelConfig {
|
|
85
|
+
preferredCase?: CaseStyle | undefined;
|
|
86
|
+
responseStructure?: ResponseStructureConfig | undefined;
|
|
87
|
+
}
|
|
84
88
|
interface MetaData {
|
|
85
89
|
[key: string]: any;
|
|
86
90
|
}
|
|
@@ -412,32 +416,88 @@ declare class ServerResponse<R extends NonCollectible | Collectible | ResourceDa
|
|
|
412
416
|
finally(onfinally?: (() => void) | null): Promise<void>;
|
|
413
417
|
}
|
|
414
418
|
//#endregion
|
|
419
|
+
//#region src/BaseSerializer.d.ts
|
|
420
|
+
interface SerializerConstructor {
|
|
421
|
+
preferredCase?: CaseStyle;
|
|
422
|
+
responseStructure?: ResponseStructureConfig;
|
|
423
|
+
config?: () => ResourceLevelConfig;
|
|
424
|
+
}
|
|
425
|
+
declare abstract class BaseSerializer<TResource = any> {
|
|
426
|
+
static preferredCase?: CaseStyle;
|
|
427
|
+
static responseStructure?: ResponseStructureConfig;
|
|
428
|
+
static config?: () => ResourceLevelConfig;
|
|
429
|
+
protected instanceConfig?: ResourceLevelConfig;
|
|
430
|
+
protected additionalMeta?: MetaData;
|
|
431
|
+
protected called: {
|
|
432
|
+
json?: boolean;
|
|
433
|
+
data?: boolean;
|
|
434
|
+
toArray?: boolean;
|
|
435
|
+
additional?: boolean;
|
|
436
|
+
with?: boolean;
|
|
437
|
+
withResponse?: boolean;
|
|
438
|
+
status?: boolean;
|
|
439
|
+
then?: boolean;
|
|
440
|
+
toResponse?: boolean;
|
|
441
|
+
};
|
|
442
|
+
when<T>(condition: any, value: T | (() => T)): T | undefined;
|
|
443
|
+
whenNotNull<T>(value: T | null | undefined): T | undefined;
|
|
444
|
+
mergeWhen<T extends Record<string, any>>(condition: any, value: T | (() => T)): Partial<T>;
|
|
445
|
+
protected abstract resolveCurrentRootKey(): string;
|
|
446
|
+
protected abstract applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
447
|
+
protected abstract getResourceForMeta(): TResource;
|
|
448
|
+
with(meta?: any): any;
|
|
449
|
+
withMeta<M extends MetaData>(meta: M | ((resource: TResource) => M)): this;
|
|
450
|
+
protected resolveMergedMeta(withMethod: (meta?: any) => any): Record<string, any> | undefined;
|
|
451
|
+
protected runResponse<TBody, TRawResponse, TServerResponse>(input: {
|
|
452
|
+
ensureJson: () => void;
|
|
453
|
+
rawResponse: TRawResponse;
|
|
454
|
+
body: () => TBody;
|
|
455
|
+
createServerResponse: (raw: TRawResponse, body: TBody) => TServerResponse;
|
|
456
|
+
callWithResponse: (response: TServerResponse, raw: TRawResponse) => void;
|
|
457
|
+
}): TServerResponse;
|
|
458
|
+
protected runThen<TBody, TRawResponse, TServerResponse, TResult1, TResult2>(input: {
|
|
459
|
+
ensureJson: () => void;
|
|
460
|
+
body: () => TBody;
|
|
461
|
+
rawResponse?: TRawResponse;
|
|
462
|
+
createServerResponse: (raw: TRawResponse, body: TBody) => TServerResponse;
|
|
463
|
+
callWithResponse: (response?: TServerResponse, raw?: TRawResponse) => void;
|
|
464
|
+
sendRawResponse?: (raw: TRawResponse, body: TBody) => void;
|
|
465
|
+
onfulfilled?: ((value: TBody) => TResult1 | PromiseLike<TResult1>) | null;
|
|
466
|
+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null;
|
|
467
|
+
}): Promise<TResult1 | TResult2>;
|
|
468
|
+
config(): ResourceLevelConfig;
|
|
469
|
+
config(config: ResourceLevelConfig): this;
|
|
470
|
+
protected resolveSerializerConfig(localConstructor: SerializerConstructor, _fallbackConfig?: ResourceLevelConfig): {
|
|
471
|
+
preferredCase: CaseStyle | undefined;
|
|
472
|
+
responseStructure: {
|
|
473
|
+
wrap?: boolean | undefined;
|
|
474
|
+
rootKey?: string | undefined;
|
|
475
|
+
factory?: ResponseFactory | undefined;
|
|
476
|
+
};
|
|
477
|
+
};
|
|
478
|
+
protected resolveSerializerCaseStyle(localConstructor: SerializerConstructor, fallbackConfig?: ResourceLevelConfig): CaseStyle | undefined;
|
|
479
|
+
protected resolveSerializerResponseStructure(localConstructor: SerializerConstructor, fallbackConfig?: ResourceLevelConfig): {
|
|
480
|
+
wrap: boolean;
|
|
481
|
+
rootKey: string;
|
|
482
|
+
factory: ResponseFactory | undefined;
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
//#endregion
|
|
415
486
|
//#region src/ResourceCollection.d.ts
|
|
416
487
|
/**
|
|
417
488
|
* ResourceCollection class to handle API resource transformation and response building for collections
|
|
418
489
|
*/
|
|
419
|
-
declare class ResourceCollection<R extends ResourceData[] | Collectible | CollectionLike | PaginatorLike = ResourceData[] | Collectible | CollectionLike | PaginatorLike, T extends ResourceData = any> {
|
|
490
|
+
declare class ResourceCollection<R extends ResourceData[] | Collectible | CollectionLike | PaginatorLike = ResourceData[] | Collectible | CollectionLike | PaginatorLike, T extends ResourceData = any> extends BaseSerializer<R> {
|
|
420
491
|
private res?;
|
|
421
492
|
[key: string]: any;
|
|
422
493
|
private body;
|
|
423
494
|
resource: R;
|
|
424
495
|
collects?: typeof Resource<T>;
|
|
425
|
-
private additionalMeta?;
|
|
426
496
|
protected withResponseContext?: {
|
|
427
497
|
response: ServerResponse<CollectionBody<R>>;
|
|
428
498
|
raw: Response | H3Event['res'];
|
|
429
499
|
};
|
|
430
500
|
private isPaginatedCollectible;
|
|
431
|
-
/**
|
|
432
|
-
* Preferred case style for this collection's output keys.
|
|
433
|
-
* Set on a subclass to override the global default.
|
|
434
|
-
*/
|
|
435
|
-
static preferredCase?: CaseStyle;
|
|
436
|
-
/**
|
|
437
|
-
* Response structure override for this collection class.
|
|
438
|
-
*/
|
|
439
|
-
static responseStructure?: ResponseStructureConfig;
|
|
440
|
-
private called;
|
|
441
501
|
constructor(rsc: R);
|
|
442
502
|
constructor(rsc: R, res: Response);
|
|
443
503
|
/**
|
|
@@ -452,19 +512,11 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
452
512
|
* Replace the current serialized output body.
|
|
453
513
|
*/
|
|
454
514
|
protected setBody(body: CollectionBody<R>): this;
|
|
455
|
-
|
|
456
|
-
* Conditionally include a value in serialized output.
|
|
457
|
-
*/
|
|
458
|
-
when<T>(condition: any, value: T | (() => T)): T | undefined;
|
|
459
|
-
/**
|
|
460
|
-
* Include a value only when it is not null/undefined.
|
|
461
|
-
*/
|
|
462
|
-
whenNotNull<T>(value: T | null | undefined): T | undefined;
|
|
463
|
-
/**
|
|
464
|
-
* Conditionally merge object attributes into serialized output.
|
|
465
|
-
*/
|
|
466
|
-
mergeWhen<T extends Record<string, any>>(condition: any, value: T | (() => T)): Partial<T>;
|
|
515
|
+
private resolveCollectsConfig;
|
|
467
516
|
private resolveResponseStructure;
|
|
517
|
+
protected resolveCurrentRootKey(): string;
|
|
518
|
+
protected applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
519
|
+
protected getResourceForMeta(): R;
|
|
468
520
|
private getPayloadKey;
|
|
469
521
|
/**
|
|
470
522
|
* Convert resource to JSON response format
|
|
@@ -472,20 +524,6 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
472
524
|
* @returns
|
|
473
525
|
*/
|
|
474
526
|
json(): this;
|
|
475
|
-
/**
|
|
476
|
-
* Append structured metadata to the response body.
|
|
477
|
-
*
|
|
478
|
-
* @param meta Metadata object or metadata factory
|
|
479
|
-
* @returns
|
|
480
|
-
*/
|
|
481
|
-
with(meta?: any): any;
|
|
482
|
-
/**
|
|
483
|
-
* Typed fluent metadata helper.
|
|
484
|
-
*
|
|
485
|
-
* @param meta Metadata object or metadata factory
|
|
486
|
-
* @returns
|
|
487
|
-
*/
|
|
488
|
-
withMeta<M extends MetaData>(meta: M | ((resource: R) => M)): this;
|
|
489
527
|
/**
|
|
490
528
|
* Flatten resource to return original data
|
|
491
529
|
*
|
|
@@ -529,33 +567,22 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
529
567
|
* @param onfinally
|
|
530
568
|
* @returns
|
|
531
569
|
*/
|
|
532
|
-
finally(onfinally?: (() => void) | null): Promise<
|
|
570
|
+
finally(onfinally?: (() => void) | null): Promise<unknown>;
|
|
533
571
|
}
|
|
534
572
|
//#endregion
|
|
535
573
|
//#region src/Resource.d.ts
|
|
536
574
|
/**
|
|
537
575
|
* Resource class to handle API resource transformation and response building
|
|
538
576
|
*/
|
|
539
|
-
declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
577
|
+
declare class Resource<R extends ResourceData | NonCollectible = ResourceData> extends BaseSerializer<R> {
|
|
540
578
|
private res?;
|
|
541
579
|
[key: string]: any;
|
|
542
580
|
private body;
|
|
543
581
|
resource: R;
|
|
544
|
-
private additionalMeta?;
|
|
545
582
|
protected withResponseContext?: {
|
|
546
583
|
response: ServerResponse<ResourceBody<R>>;
|
|
547
584
|
raw: Response | H3Event['res'];
|
|
548
585
|
};
|
|
549
|
-
/**
|
|
550
|
-
* Preferred case style for this resource's output keys.
|
|
551
|
-
* Set on a subclass to override the global default.
|
|
552
|
-
*/
|
|
553
|
-
static preferredCase?: CaseStyle;
|
|
554
|
-
/**
|
|
555
|
-
* Response structure override for this resource class.
|
|
556
|
-
*/
|
|
557
|
-
static responseStructure?: ResponseStructureConfig;
|
|
558
|
-
private called;
|
|
559
586
|
constructor(rsc: R, res?: Response | undefined);
|
|
560
587
|
/**
|
|
561
588
|
* Create a ResourceCollection from an array of resource data or a Collectible instance
|
|
@@ -576,19 +603,10 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
|
576
603
|
* Replace the current serialized output body.
|
|
577
604
|
*/
|
|
578
605
|
protected setBody(body: ResourceBody<R>): this;
|
|
579
|
-
/**
|
|
580
|
-
* Conditionally include a value in serialized output.
|
|
581
|
-
*/
|
|
582
|
-
when<T>(condition: any, value: T | (() => T)): T | undefined;
|
|
583
|
-
/**
|
|
584
|
-
* Include a value only when it is not null/undefined.
|
|
585
|
-
*/
|
|
586
|
-
whenNotNull<T>(value: T | null | undefined): T | undefined;
|
|
587
|
-
/**
|
|
588
|
-
* Conditionally merge object attributes into serialized output.
|
|
589
|
-
*/
|
|
590
|
-
mergeWhen<T extends Record<string, any>>(condition: any, value: T | (() => T)): Partial<T>;
|
|
591
606
|
private resolveResponseStructure;
|
|
607
|
+
protected resolveCurrentRootKey(): string;
|
|
608
|
+
protected applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
609
|
+
protected getResourceForMeta(): R;
|
|
592
610
|
private getPayloadKey;
|
|
593
611
|
/**
|
|
594
612
|
* Convert resource to JSON response format
|
|
@@ -596,20 +614,6 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
|
596
614
|
* @returns
|
|
597
615
|
*/
|
|
598
616
|
json(): this;
|
|
599
|
-
/**
|
|
600
|
-
* Append structured metadata to the response body.
|
|
601
|
-
*
|
|
602
|
-
* @param meta Metadata object or metadata factory
|
|
603
|
-
* @returns
|
|
604
|
-
*/
|
|
605
|
-
with(meta?: any): any;
|
|
606
|
-
/**
|
|
607
|
-
* Typed fluent metadata helper.
|
|
608
|
-
*
|
|
609
|
-
* @param meta Metadata object or metadata factory
|
|
610
|
-
* @returns
|
|
611
|
-
*/
|
|
612
|
-
withMeta<M extends MetaData>(meta: M | ((resource: R) => M)): this;
|
|
613
617
|
/**
|
|
614
618
|
* Flatten resource to array format (for collections) or return original data for single resources
|
|
615
619
|
*
|
|
@@ -652,34 +656,23 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
|
652
656
|
* @param onfinally
|
|
653
657
|
* @returns
|
|
654
658
|
*/
|
|
655
|
-
finally(onfinally?: (() => void) | null): Promise<
|
|
659
|
+
finally(onfinally?: (() => void) | null): Promise<unknown>;
|
|
656
660
|
}
|
|
657
661
|
//#endregion
|
|
658
662
|
//#region src/GenericResource.d.ts
|
|
659
663
|
/**
|
|
660
664
|
* GenericResource class to handle API resource transformation and response building
|
|
661
665
|
*/
|
|
662
|
-
declare class GenericResource<R extends NonCollectible | Collectible | CollectionLike | PaginatorLike | ResourceData = ResourceData, T extends ResourceData = any> {
|
|
666
|
+
declare class GenericResource<R extends NonCollectible | Collectible | CollectionLike | PaginatorLike | ResourceData = ResourceData, T extends ResourceData = any> extends BaseSerializer<R> {
|
|
663
667
|
private res?;
|
|
664
668
|
[key: string]: any;
|
|
665
669
|
private body;
|
|
666
670
|
resource: R;
|
|
667
671
|
collects?: typeof Resource<T>;
|
|
668
|
-
private additionalMeta?;
|
|
669
672
|
protected withResponseContext?: {
|
|
670
673
|
response: ServerResponse<GenericBody<R>>;
|
|
671
674
|
raw: Response | H3Event['res'];
|
|
672
675
|
};
|
|
673
|
-
/**
|
|
674
|
-
* Preferred case style for this resource's output keys.
|
|
675
|
-
* Set on a subclass to override the global default.
|
|
676
|
-
*/
|
|
677
|
-
static preferredCase?: CaseStyle;
|
|
678
|
-
/**
|
|
679
|
-
* Response structure override for this generic resource class.
|
|
680
|
-
*/
|
|
681
|
-
static responseStructure?: ResponseStructureConfig;
|
|
682
|
-
private called;
|
|
683
676
|
constructor(rsc: R, res?: Response | undefined);
|
|
684
677
|
/**
|
|
685
678
|
* Get the original resource data
|
|
@@ -693,19 +686,11 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
693
686
|
* Replace the current serialized output body.
|
|
694
687
|
*/
|
|
695
688
|
protected setBody(body: GenericBody<R>): this;
|
|
696
|
-
|
|
697
|
-
* Conditionally include a value in serialized output.
|
|
698
|
-
*/
|
|
699
|
-
when<T>(condition: any, value: T | (() => T)): T | undefined;
|
|
700
|
-
/**
|
|
701
|
-
* Include a value only when it is not null/undefined.
|
|
702
|
-
*/
|
|
703
|
-
whenNotNull<T>(value: T | null | undefined): T | undefined;
|
|
704
|
-
/**
|
|
705
|
-
* Conditionally merge object attributes into serialized output.
|
|
706
|
-
*/
|
|
707
|
-
mergeWhen<T extends Record<string, any>>(condition: any, value: T | (() => T)): Partial<T>;
|
|
689
|
+
private resolveCollectsConfig;
|
|
708
690
|
private resolveResponseStructure;
|
|
691
|
+
protected resolveCurrentRootKey(): string;
|
|
692
|
+
protected applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
693
|
+
protected getResourceForMeta(): R;
|
|
709
694
|
private getPayloadKey;
|
|
710
695
|
/**
|
|
711
696
|
* Convert resource to JSON response format
|
|
@@ -713,20 +698,6 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
713
698
|
* @returns
|
|
714
699
|
*/
|
|
715
700
|
json(): this;
|
|
716
|
-
/**
|
|
717
|
-
* Append structured metadata to the response body.
|
|
718
|
-
*
|
|
719
|
-
* @param meta Metadata object or metadata factory
|
|
720
|
-
* @returns
|
|
721
|
-
*/
|
|
722
|
-
with(meta?: any): any;
|
|
723
|
-
/**
|
|
724
|
-
* Typed fluent metadata helper.
|
|
725
|
-
*
|
|
726
|
-
* @param meta Metadata object or metadata factory
|
|
727
|
-
* @returns
|
|
728
|
-
*/
|
|
729
|
-
withMeta<M extends MetaData>(meta: M | ((resource: R) => M)): this;
|
|
730
701
|
/**
|
|
731
702
|
* Convert resource to array format (for collections)
|
|
732
703
|
*
|
|
@@ -756,6 +727,20 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
756
727
|
* @returns A promise that resolves to the result of the onfulfilled or onrejected callback
|
|
757
728
|
*/
|
|
758
729
|
then<TResult1 = GenericBody<R>, TResult2 = never>(onfulfilled?: ((value: GenericBody<R>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
730
|
+
/**
|
|
731
|
+
* Promise-like catch method to handle rejected state of the promise
|
|
732
|
+
*
|
|
733
|
+
* @param onrejected
|
|
734
|
+
* @returns
|
|
735
|
+
*/
|
|
736
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<GenericBody<R> | TResult>;
|
|
737
|
+
/**
|
|
738
|
+
* Promise-like finally method to handle cleanup after promise is settled
|
|
739
|
+
*
|
|
740
|
+
* @param onfinally
|
|
741
|
+
* @returns
|
|
742
|
+
*/
|
|
743
|
+
finally(onfinally?: (() => void) | null): Promise<unknown>;
|
|
759
744
|
}
|
|
760
745
|
//#endregion
|
|
761
746
|
//#region src/utilities/case.d.ts
|
|
@@ -1126,4 +1111,4 @@ declare const setGlobalCursorMeta: (meta: Config["cursorMeta"]) => void;
|
|
|
1126
1111
|
*/
|
|
1127
1112
|
declare const getGlobalCursorMeta: () => Config["cursorMeta"];
|
|
1128
1113
|
//#endregion
|
|
1129
|
-
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, 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 };
|
|
1114
|
+
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 };
|
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
|
|
2
|
-
`),u=`import ${s} from './${s}'\n`,d=(!!n?.collection||e.endsWith(`Collection`))&&t(c(this.config.resourcesDir,`${s}.ts`)),
|
|
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`},_=`https://localhost`,ee=`page`,v={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 b=e=>{p=e},x=()=>p,S=e=>{m=e},C=()=>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=>{ee=e},me=()=>ee,w=e=>{v={...v,...e}},T=()=>v,E=e=>{y={...y,...e}},D=()=>y,O=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},k=(e,t,n=`data`)=>!t||Object.keys(t).length===0?e:Array.isArray(e)?{[n]:e,...t}:O(e)?{...e,...t}:{[n]:e,...t},A=(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];O(t)&&O(r)?n[e]=A(t,r):n[e]=r}return n},j=e=>{if(!e||typeof e!=`object`)return!1;let t=e;return typeof t.toObject==`function`&&typeof t.getRawAttributes==`function`},M=e=>!e||typeof e!=`object`?!1:typeof e.all==`function`,N=e=>{if(Array.isArray(e))return e.map(e=>N(e));if(j(e))return N(e.toObject());if(M(e)){let t=e.all();return Array.isArray(t)?t.map(e=>N(e)):N(t)}return O(e)?Object.entries(e).reduce((e,[t,n])=>(e[t]=N(n),e),{}):e},P=()=>{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}},F=(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()},I=e=>{let{metaKey:t,linksKey:n,cursorKey:r}=P(),i={},a=!!e&&typeof e==`object`&&!!e.meta&&typeof e.meta==`object`&&(Array.isArray(e.data)||M(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(T())){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:F(c.firstPage,c.path),last:c.links&&Object.prototype.hasOwnProperty.call(c.links,`last`)?c.links.last:F(c.lastPage,c.path),prev:c.links&&Object.prototype.hasOwnProperty.call(c.links,`prev`)?c.links.prev:F(c.prevPage,c.path),next:c.links&&Object.prototype.hasOwnProperty.call(c.links,`next`)?c.links.next:F(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(D())){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},L=({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:O(e)?{...e,[n]:t}:{[i]:e,[n]:t};let s={[i]:e};return t!==void 0&&(s[n]=t),s},R=(e,t)=>{let n=e?.with;if(typeof n!=`function`||n===t||n.length>0)return;let r=n.call(e);return O(r)?r:void 0},z=Symbol(`resora.conditional.missing`),B=(e,t)=>e?typeof t==`function`?t():t:z,V=e=>e??z,H=(e,t)=>{if(!e)return{};let n=typeof t==`function`?t():t;return O(n)?n:{}},U=e=>{if(e===z)return z;if(Array.isArray(e))return e.map(e=>U(e)).filter(e=>e!==z);if(O(e)){let t={};for(let[n,r]of Object.entries(e)){let e=U(r);e!==z&&(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),G=e=>W(e).map((e,t)=>t===0?e:e.charAt(0).toUpperCase()+e.slice(1)).join(``),he=e=>W(e).join(`_`),ge=e=>W(e).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``),_e=e=>W(e).join(`-`),K=e=>{if(typeof e==`function`)return e;switch(e){case`camel`:return G;case`snake`:return he;case`pascal`:return ge;case`kebab`:return _e}},q=(e,t)=>e==null?e:Array.isArray(e)?e.map(e=>q(e,t)):e instanceof Date||e instanceof RegExp?e:typeof e==`object`?Object.fromEntries(Object.entries(e).map(([e,n])=>[t(e),q(n,t)])):e;let J=o.resolve(process.cwd(),`node_modules/resora/stubs`);t(J)||(J=o.resolve(process.cwd(),`stubs`));const ve=()=>({stubsDir:J,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`}}),Y=e=>{let t=ve();return Object.assign(t,e,{stubs:Object.assign(t.stubs,e.stubs||{})},{cursorMeta:Object.assign(t.cursorMeta,e.cursorMeta||{})},{paginatedMeta:Object.assign(t.paginatedMeta,e.paginatedMeta||{})},{paginatedLinks:Object.assign(t.paginatedLinks,e.paginatedLinks||{})},{responseStructure:Object.assign(t.responseStructure,e.responseStructure||{})})};let X=!1,Z;const ye=()=>{X=!1,Z=void 0},be=e=>{e.preferredCase!==`camel`&&b(e.preferredCase),S(e.responseStructure),se(e.paginatedExtras),le(e.paginatedLinks),w(e.paginatedMeta),E(e.cursorMeta),de(e.baseUrl),pe(e.pageName)},xe=async e=>await import(`${u(e).href}?resora_runtime=${Date.now()}`),Se=e=>{be(Y((e?.default??e)||{})),X=!0},Ce=()=>{let e=l(import.meta.url),n=[o.join(process.cwd(),`resora.config.cjs`)];for(let r of n)if(t(r))try{return Se(e(r)),!0}catch{continue}return!1},we=async()=>{if(!X){if(Z)return await Z;Ce()||(Z=(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{Se(await xe(n));return}catch{continue}X=!0})(),await Z)}};we();var Te=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(`
|
|
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}}},Ee=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`)}},De=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 $=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)}},De=class e{body={data:{}};resource;collects;additionalMeta;withResponseContext;static preferredCase;static responseStructure;called={};constructor(e,t){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}when(e,t){return V(e,t)}whenNotNull(e){return H(e)}mergeWhen(e,t){return U(e,t)}resolveResponseStructure(){let e=this.constructor.responseStructure,t=this.collects?.responseStructure,n=C();return{wrap:e?.wrap??t?.wrap??n?.wrap??!0,rootKey:e?.rootKey??t?.rootKey??n?.rootKey??`data`,factory:e?.factory??t?.factory??n?.factory}}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=W(t);let n=L(this.resource),{metaKey:r}=F(),i=r?n[r]:void 0;r&&delete n[r];let a=this.constructor.preferredCase??x();if(a){let e=q(a);t=J(t,e)}let o=j(z(this,e.prototype.with),this.additionalMeta),{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}with(e){if(this.called.with=!0,e===void 0)return this.additionalMeta||{};let t=typeof e==`function`?e(this.resource):e;if(this.additionalMeta=j(this.additionalMeta,t),this.called.json){let{rootKey:e}=this.resolveResponseStructure();this.body=A(this.body,t,e)}return this}withMeta(e){return this.with(e),this}toArray(){this.called.toArray=!0,this.json();let e=P(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}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){this.called.toResponse=!0,this.json();let t=e??this.res,n=new $(t,this.body);return this.withResponseContext={response:n,raw:t},this.called.withResponse=!0,this.withResponse(n,t),n}withResponse(e,t){return this}then(e,t){if(this.called.then=!0,this.json(),this.res){let e=new $(this.res,this.body);this.withResponseContext={response:e,raw:this.res},this.called.withResponse=!0,this.withResponse(e,this.res)}else this.called.withResponse=!0,this.withResponse();let n=Promise.resolve(this.body).then(e,t);return this.res&&this.res.send(this.body),n}},Oe=class e{body={data:[]};resource;collects;additionalMeta;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}static preferredCase;static responseStructure;called={};constructor(e,t){this.res=t,this.resource=e}data(){return this.toArray()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}when(e,t){return V(e,t)}whenNotNull(e){return H(e)}mergeWhen(e,t){return U(e,t)}resolveResponseStructure(){let e=this.constructor.responseStructure,t=this.collects?.responseStructure,n=C();return{wrap:e?.wrap??t?.wrap??n?.wrap??!0,rootKey:e?.rootKey??t?.rootKey??n?.rootKey??`data`,factory:e?.factory??t?.factory??n?.factory}}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=W(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.constructor.preferredCase??this.collects?.preferredCase??x();if(a){let e=q(a);t=J(t,e)}let o=j(z(this,e.prototype.with),this.additionalMeta),{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}with(e){if(this.called.with=!0,e===void 0)return this.additionalMeta||{};let t=typeof e==`function`?e(this.resource):e;if(this.additionalMeta=j(this.additionalMeta,t),this.called.json){let{rootKey:e}=this.resolveResponseStructure();this.body=A(this.body,t,e)}return this}withMeta(e){return this.with(e),this}toArray(){return this.called.toArray=!0,this.json(),P(Array.isArray(this.resource)?this.resource:N(this.resource)?this.resource.all():this.resource.data)}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){this.called.toResponse=!0,this.json();let t=e??this.res,n=new $(t,this.body);return this.withResponseContext={response:n,raw:t},this.called.withResponse=!0,this.withResponse(n,t),n}withResponse(e,t){return this}setCollects(e){return this.collects=e,this}then(e,t){if(this.called.then=!0,this.json(),this.res){let e=new $(this.res,this.body);this.withResponseContext={response:e,raw:this.res},this.called.withResponse=!0,this.withResponse(e,this.res)}else this.called.withResponse=!0,this.withResponse();let n=Promise.resolve(this.body).then(e,t);return this.res&&this.res.send(this.body),n}catch(e){return this.then(void 0,e)}finally(e){return this.then(e,e)}},ke=class e{body={data:{}};resource;additionalMeta;withResponseContext;static preferredCase;static responseStructure;called={};constructor(e,t){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 Oe(e).setCollects(this)}data(){return this.toArray()}getBody(){return this.json(),this.body}setBody(e){return this.body=e,this}when(e,t){return V(e,t)}whenNotNull(e){return H(e)}mergeWhen(e,t){return U(e,t)}resolveResponseStructure(){let e=this.constructor.responseStructure,t=C();return{wrap:e?.wrap??t?.wrap??!0,rootKey:e?.rootKey??t?.rootKey??`data`,factory:e?.factory??t?.factory}}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=W(t);let n=this.constructor.preferredCase??x();if(n){let e=q(n);t=J(t,e)}let r=j(z(this,e.prototype.with),this.additionalMeta),{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}with(e){if(this.called.with=!0,e===void 0)return this.additionalMeta||{};let t=typeof e==`function`?e(this.resource):e;if(this.additionalMeta=j(this.additionalMeta,t),this.called.json){let{rootKey:e}=this.resolveResponseStructure();this.body=A(this.body,t,e)}return this}withMeta(e){return this.with(e),this}toArray(){this.called.toArray=!0,this.json();let e=P(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}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){this.called.toResponse=!0,this.json();let t=e??this.res,n=new $(t,this.body);return this.withResponseContext={response:n,raw:t},this.called.withResponse=!0,this.withResponse(n,t),n}withResponse(e,t){return this}then(e,t){if(this.called.then=!0,this.json(),this.res){let e=new $(this.res,this.body);this.withResponseContext={response:e,raw:this.res},this.called.withResponse=!0,this.withResponse(e,this.res)}else this.called.withResponse=!0,this.withResponse();let n=Promise.resolve(this.body).then(e,t);return this.res&&this.res.send(this.body),n}catch(e){return this.then(void 0,e)}finally(e){return this.then(e,e)}};export{ee as ApiResource,B as CONDITIONAL_ATTRIBUTE_MISSING,we as CliApp,De as GenericResource,Te as InitCommand,Ee as MakeResource,ke as Resource,Oe as ResourceCollection,$ as ServerResponse,A as appendRootProperties,ye as applyRuntimeConfig,L as buildPaginationExtras,R as buildResponseEnvelope,X as defineConfig,q as getCaseTransformer,_e as getDefaultConfig,le as getGlobalBaseUrl,x as getGlobalCase,pe as getGlobalCursorMeta,de as getGlobalPageName,T as getGlobalPaginatedExtras,se as getGlobalPaginatedLinks,D as getGlobalPaginatedMeta,oe as getGlobalResponseFactory,ie as getGlobalResponseRootKey,C as getGlobalResponseStructure,re as getGlobalResponseWrap,F as getPaginationExtraKeys,N as isArkormLikeCollection,M as isArkormLikeModel,k as isPlainObject,Ce as loadRuntimeConfig,j as mergeMetadata,P as normalizeSerializableData,ve as resetRuntimeConfigForTests,U as resolveMergeWhen,V as resolveWhen,H as resolveWhenNotNull,z as resolveWithHookMetadata,W as sanitizeConditionalAttributes,ce as setGlobalBaseUrl,b as setGlobalCase,O as setGlobalCursorMeta,ue as setGlobalPageName,w as setGlobalPaginatedExtras,E as setGlobalPaginatedLinks,fe as setGlobalPaginatedMeta,ae as setGlobalResponseFactory,te as setGlobalResponseRootKey,S as setGlobalResponseStructure,ne as setGlobalResponseWrap,G as splitWords,K as toCamelCase,ge as toKebabCase,he as toPascalCase,me as toSnakeCase,J 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;instanceConfig;additionalMeta;called={};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=K(a);t=q(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}toArray(){this.called.toArray=!0,this.json();let e=N(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}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.toArray()}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=K(a);t=q(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}toArray(){return this.called.toArray=!0,this.json(),N(Array.isArray(this.resource)?this.resource:M(this.resource)?this.resource.all():this.resource.data)}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.toArray()}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=K(n);t=q(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}toArray(){this.called.toArray=!0,this.json();let e=N(this.resource);return!Array.isArray(e)&&e&&e.data!==void 0&&(e=e.data),e}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,be as applyRuntimeConfig,I as buildPaginationExtras,L as buildResponseEnvelope,Y as defineConfig,K as getCaseTransformer,ve 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,we as loadRuntimeConfig,A as mergeMetadata,N as normalizeSerializableData,ye 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,G as toCamelCase,_e as toKebabCase,ge as toPascalCase,he as toSnakeCase,q as transformKeys};
|
package/package.json
CHANGED