@cyberskill/shared 2.3.1 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/node/mongo/mongo.util.cjs +5 -5
- package/dist/node/mongo/mongo.util.d.ts +16 -0
- package/dist/node/mongo/mongo.util.js +110 -76
- package/dist/node/upload/index.cjs +1 -1
- package/dist/node/upload/index.d.ts +1 -0
- package/dist/node/upload/index.js +12 -5
- package/dist/node/upload/upload.constant.cjs +1 -0
- package/dist/node/upload/upload.constant.d.ts +1 -0
- package/dist/node/upload/upload.constant.js +4 -0
- package/dist/node/upload/upload.type.d.ts +8 -11
- package/dist/node/upload/upload.util.cjs +1 -1
- package/dist/node/upload/upload.util.d.ts +72 -5
- package/dist/node/upload/upload.util.js +120 -78
- package/dist/util/object/object.util.cjs +1 -1
- package/dist/util/object/object.util.d.ts +36 -5
- package/dist/util/object/object.util.js +59 -39
- package/package.json +11 -11
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=require("lodash-es"),
|
|
2
|
-
module.exports = ${JSON.stringify(s,null,4)}`;P.writeFileSync(
|
|
3
|
-
${
|
|
4
|
-
`;P.pathExistsSync(
|
|
5
|
-
`).includes(T.MIGRATE_MONGO_CONFIG)||P.appendFileSync(T.PATH.GIT_IGNORE,e):P.writeFileSync(T.PATH.GIT_IGNORE,e)}}),regexify(s,t){if(!s)return{};let e=I.cloneDeep(s);if(!t||t.length===0)return e;for(const r of t){const n=r.toString().split("."),c=$.getNestedValue(e,n);if(typeof c=="string"&&c.length>0){const u={$regex:`.*${Q.regexSearchMapper(c)}.*`,$options:"i"};e=$.setNestedValue(e,n,u)}}return e},isDynamicVirtual(s){return!!(s&&typeof s.ref=="function")}};function se(s){return s!==null&&typeof s=="object"&&"toObject"in s&&typeof s.toObject=="function"}function F(s,t){if(!s||!t||t.length===0)return s;const e=new Set(t.map(r=>r.name));if(Array.isArray(s)){const r=s.filter(n=>{if(typeof n=="string")return!e.has(n);if(typeof n=="object"&&n!==null){const c=n;return!e.has(c.path||c.populate||"")}return!0});return r.length>0?r:void 0}if(typeof s=="string")return e.has(s)?void 0:s;if(typeof s=="object"&&s!==null){const r=s,n=r.path||r.populate||"";return e.has(n)?void 0:s}return s}function ne(s,t,e){if(!s.length||!t||!(e!=null&&e.ref))return[];const r=new Map;return s.forEach(n=>{try{const c=e.ref(n);if(c==null)return;const u=typeof c=="string"?c:String(c);if(u&&u.trim()!==""){const i=re(u);r.has(i)||r.set(i,[]),r.get(i).push(n)}}catch(c){d.catchError(new Error(`Dynamic ref function failed for virtual "${t}": ${c instanceof Error?c.message:String(c)}`))}}),Array.from(r.entries()).map(([n,c])=>({model:n,docs:c}))}function L(s,t,e,r,n){return l(this,null,function*(){if(!t.length||!e.length||!r)return t;const c=e.filter(o=>{if(Array.isArray(r))return r.length>0&&r.some(a=>typeof a=="string"?a===o.name:a.path===o.name);if(typeof r=="string")return r===o.name;if(typeof r=="object"&&r!==null){const a=r;return a.path&&a.path===o.name||a.populate&&a.populate===o.name}return!1});if(c.length===0)return t;const u=I.cloneDeep(t.map(o=>se(o)?o.toObject():o));u.forEach(o=>{c.forEach(({name:a,options:f})=>{if(!(a in o)){const h=o;h[a]=f.count?0:f.justOne?null:[]}})});const i=new Map;for(const o of c){const{name:a,options:f}=o,h=ne(u,a,f);for(const S of h){i.has(S.model)||i.set(S.model,{virtuals:[],localValueSets:new Map,docsByLocalValue:new Map});const m=i.get(S.model);m.virtuals.find(N=>N.name===a)||(m.virtuals.push(o),m.localValueSets.set(a,new Set));const M=m.localValueSets.get(a);S.docs.forEach(N=>{const w=N[f.localField];if(w!=null){const _=String(w);M.add(_);let D=-1;const R=N;R.id!==void 0?D=u.findIndex(A=>A.id===R.id):R._id!==void 0&&(D=u.findIndex(A=>{var E,y,p,g;return((y=(E=A._id)==null?void 0:E.toString)==null?void 0:y.call(E))===((g=(p=R._id)==null?void 0:p.toString)==null?void 0:g.call(p))})),D!==-1&&(m.docsByLocalValue.has(_)||m.docsByLocalValue.set(_,[]),m.docsByLocalValue.get(_).push(D))}})}}return yield Promise.all(Array.from(i.entries()).map(o=>l(null,[o],function*([a,f]){const h=s.models[a];if(!h)return;const S=new Set;if(f.localValueSets.forEach(_=>{_.forEach(D=>S.add(D))}),S.size===0)return;const m=[...new Set(f.virtuals.map(_=>_.options.foreignField))],M=Array.from(S);let N;m.length===1?N={[String(m[0])]:{$in:M}}:N={$or:m.map(_=>({[_]:{$in:M}}))};const w=yield h.find(N,n).lean();for(const _ of f.virtuals){const{name:D,options:R}=_,A=w.filter(E=>{const y=E[R.foreignField];return y!=null&&S.has(String(y))});if(R.count){const E=new Map;A.forEach(y=>{var p;const g=(p=y[R.foreignField])==null?void 0:p.toString();g&&E.set(g,(E.get(g)||0)+1)}),f.localValueSets.get(D).forEach(y=>{const p=f.docsByLocalValue.get(y)||[],g=E.get(y)||0;p.forEach(C=>{const x=u[C];x[D]===void 0&&(x[D]=g)})})}else{const E=new Map;A.forEach(y=>{var p;const g=(p=y[R.foreignField])==null?void 0:p.toString();g&&(E.has(g)||E.set(g,[]),E.get(g).push(y))}),f.localValueSets.get(D).forEach(y=>{const p=f.docsByLocalValue.get(y)||[],g=E.get(y)||[],C=R.justOne?g[0]||null:g;p.forEach(x=>{const B=u[x];B[D]=C})})}}}))),u})}class ce{constructor(t,e){te(this,"collection"),this.collection=t.collection(e)}createOne(t){return l(this,null,function*(){try{const e=O(O({},V.createGenericFields()),t);return(yield this.collection.insertOne(e)).acknowledged?{success:!0,message:"Document created successfully",result:e}:{success:!1,message:"Document creation failed",code:v.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(e){return d.catchError(e)}})}createMany(t){return l(this,null,function*(){try{const e=t.map(n=>O(O({},V.createGenericFields()),n)),r=yield this.collection.insertMany(e);return r.insertedCount===0?{success:!1,message:"No documents were inserted",code:v.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:`${r.insertedCount} documents created successfully`,result:e}}catch(e){return d.catchError(e)}})}findOne(t){return l(this,null,function*(){try{const e=yield this.collection.findOne(t);return e?{success:!0,message:"Document found",result:e}:{success:!1,message:"Document not found",code:v.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(e){return d.catchError(e)}})}findAll(){return l(this,arguments,function*(t={}){try{return{success:!0,message:"Documents retrieved successfully",result:yield this.collection.find(t).toArray()}}catch(e){return d.catchError(e)}})}count(){return l(this,arguments,function*(t={}){try{const e=yield this.collection.countDocuments(t);return{success:!0,message:`${e} documents counted successfully`,result:e}}catch(e){return d.catchError(e)}})}updateOne(t,e){return l(this,null,function*(){try{const r=yield this.collection.updateOne(t,{$set:e});return r.matchedCount===0?{success:!1,message:"No documents matched the filter",code:v.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document updated successfully",result:r}}catch(r){return d.catchError(r)}})}updateMany(t,e){return l(this,null,function*(){try{const r=yield this.collection.updateMany(t,{$set:e});return r.matchedCount===0?{success:!1,message:"No documents matched the filter",code:v.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents updated successfully",result:r}}catch(r){return d.catchError(r)}})}deleteOne(t){return l(this,null,function*(){try{const e=yield this.collection.deleteOne(t);return e.deletedCount===0?{success:!1,message:"No documents matched the filter",code:v.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document deleted successfully",result:e}}catch(e){return d.catchError(e)}})}deleteMany(t){return l(this,null,function*(){try{const e=yield this.collection.deleteMany(t);return e.deletedCount===0?{success:!1,message:"No documents matched the filter",code:v.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents deleted successfully",result:e}}catch(e){return d.catchError(e)}})}}class ue{constructor(t){this.model=t}getModelName(){return this.model.modelName}getDynamicVirtuals(){return this.model.schema.statics._dynamicVirtuals}populateDynamicVirtualsForDocument(t,e){return l(this,null,function*(){const r=this.getDynamicVirtuals();if(r&&r.length>0){const n=yield L(this.model.base,[t],r,e);return n&&n[0]?n[0]:t}return t})}populateDynamicVirtualsForDocuments(t,e){return l(this,null,function*(){const r=this.getDynamicVirtuals();return r&&r.length>0&&t.length>0?yield L(this.model.base,t,r,e):t})}findOne(){return l(this,arguments,function*(t={},e={},r={},n){try{const c=this.model.findOne(t,e,r),u=this.getDynamicVirtuals(),i=F(n,u);i&&c.populate(i);const o=yield c.exec();return o?{success:!0,result:yield this.populateDynamicVirtualsForDocument(o,n)}:{success:!1,message:`No ${this.getModelName()} found.`,code:v.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(c){return d.catchError(c)}})}findAll(){return l(this,arguments,function*(t={},e={},r={},n){try{const c=this.model.find(t,e,r),u=this.getDynamicVirtuals(),i=F(n,u);i&&c.populate(i);const o=yield c.exec();return{success:!0,result:yield this.populateDynamicVirtualsForDocuments(o,n)}}catch(c){return d.catchError(c)}})}findPaging(){return l(this,arguments,function*(t={},e={}){try{const r=this.getDynamicVirtuals(),n=O({},e);e.populate&&(n.populate=F(e.populate,r));const c=yield this.model.paginate(t,n),u=yield this.populateDynamicVirtualsForDocuments(c.docs,e.populate);return{success:!0,result:b(O({},c),{docs:u})}}catch(r){return d.catchError(r)}})}findPagingAggregate(t){return l(this,arguments,function*(e,r={}){try{const n=this.getDynamicVirtuals(),c=O({},r);r.populate&&(c.populate=F(r.populate,n));const u=yield this.model.aggregatePaginate(this.model.aggregate(e),c),i=yield this.populateDynamicVirtualsForDocuments(u.docs,r.populate);return{success:!0,result:b(O({},u),{docs:i})}}catch(n){return d.catchError(n)}})}count(){return l(this,arguments,function*(t={}){try{return{success:!0,result:yield this.model.countDocuments(t)}}catch(e){return d.catchError(e)}})}createOne(t){return l(this,null,function*(){try{return{success:!0,result:yield this.model.create(t)}}catch(e){return d.catchError(e)}})}createMany(t){return l(this,arguments,function*(e,r={}){try{return{success:!0,result:(yield this.model.insertMany(e,r)).map(u=>u instanceof H.Document?u.toObject():null).filter(u=>u!==null)}}catch(n){return d.catchError(n)}})}updateOne(){return l(this,arguments,function*(t={},e={},r={}){try{const n=yield this.model.findOneAndUpdate(t,e,O({new:!0},r)).exec();return n?{success:!0,result:n}:{success:!1,message:`Failed to update ${this.getModelName()}.`,code:v.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(n){return d.catchError(n)}})}updateMany(){return l(this,arguments,function*(t={},e={},r={}){try{return{success:!0,result:yield this.model.updateMany(t,e,r).exec()}}catch(n){return d.catchError(n)}})}deleteOne(){return l(this,arguments,function*(t={},e={}){try{const r=yield this.model.findOneAndDelete(t,e).exec();return r?{success:!0,result:r}:{success:!1,message:`No ${this.getModelName()} found to delete.`,code:v.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(r){return d.catchError(r)}})}deleteMany(){return l(this,arguments,function*(t={},e={}){try{const r=yield this.model.deleteMany(t,e).exec();return r.deletedCount===0?{success:!1,message:"No documents found to delete.",code:v.RESPONSE_STATUS.NOT_FOUND.CODE}:{success:!0,result:r}}catch(r){return d.catchError(r)}})}createShortId(t,e=4){return l(this,null,function*(){try{const n=Array.from({length:10},(i,o)=>j.generateShortId(t,o+e)),u=(yield Promise.all(n.map(i=>this.model.exists({shortId:i})))).findIndex(i=>!i);if(u!==-1){const i=n[u];if(i)return{success:!0,result:i}}return{success:!1,message:"Failed to create a unique shortId",code:v.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(r){return d.catchError(r)}})}createSlugQuery({slug:t,field:e,isObject:r,filter:n}){const c=O({},n!=null?n:{});return r?b(O({},c),{$or:[{[`slug.${e}`]:t},{slugHistory:{$elemMatch:{[`slug.${e}`]:t}}}]}):b(O({},c),{$or:[{slug:t},{slugHistory:t}]})}createUniqueSlug(t){return l(this,arguments,function*({slug:e,field:r,isObject:n,filter:c}){const u=j.generateSlug(e),o=Array.from({length:100},(h,S)=>S===0?u:`${u}-${S}`),f=(yield Promise.all(o.map(h=>this.model.exists(this.createSlugQuery({slug:h,field:r,isObject:n,filter:c}))))).findIndex(h=>!h);if(f!==-1){const h=o[f];if(h)return h}return`${u}-${Date.now()}`})}createSlug(t){return l(this,arguments,function*({field:e,from:r,filter:n}){try{const c=r[e];return I.isObject(c)?{success:!0,result:Object.fromEntries(yield Promise.all(Object.entries(c).map(a=>l(this,[a],function*([f,h]){const S=yield this.createUniqueSlug({slug:h,field:f,isObject:!0,filter:n});return[f,S]}))))}:{success:!0,result:yield this.createUniqueSlug({slug:c,field:e,isObject:!1,filter:n})}}catch(c){return d.catchError(c)}})}checkSlug(t){return l(this,arguments,function*({slug:e,field:r,from:n,filter:c}){try{const u=n[r];if(I.isObject(u)){const h=Object.values(u).map(m=>j.generateSlug(m));return(yield Promise.all(h.map(m=>this.model.exists(this.createSlugQuery({slug:m,field:r,isObject:!0,filter:c}))))).some(m=>m)?{success:!0,result:!0}:{success:!0,result:!1}}const o=j.generateSlug(e);return{success:!0,result:(yield this.model.exists(this.createSlugQuery({slug:o,field:r,isObject:!1,filter:c})))!==null}}catch(u){return d.catchError(u)}})}aggregate(t){return l(this,null,function*(){try{return{success:!0,result:yield this.model.aggregate(t)}}catch(e){return d.catchError(e)}})}}exports.MongoController=ce;exports.MongooseController=ue;exports.mongo=V;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=require("lodash-es"),H=require("migrate-mongo"),k=require("mongoose"),K=require("mongoose-aggregate-paginate-v2"),W=require("mongoose-paginate-v2"),U=require("uuid"),$=require("../../util/object/object.util.cjs"),Q=require("../../util/common/common.util.cjs"),P=require("../fs/fs.util.cjs"),A=require("../path/path.constant.cjs"),J=require("../../util/validate/validate.util.cjs"),j=require("../../util/string/string.util.cjs"),D=require("../../constant/response-status.cjs"),d=require("../log/log.util.cjs");var z=Object.defineProperty,X=Object.defineProperties,Y=Object.getOwnPropertyDescriptors,G=Object.getOwnPropertySymbols,Z=Object.prototype.hasOwnProperty,ee=Object.prototype.propertyIsEnumerable,q=(s,t,e)=>t in s?z(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,O=(s,t)=>{for(var e in t||(t={}))Z.call(t,e)&&q(s,e,t[e]);if(G)for(var e of G(t))ee.call(t,e)&&q(s,e,t[e]);return s},b=(s,t)=>X(s,Y(t)),te=(s,t,e)=>q(s,typeof t!="symbol"?t+"":t,e),l=(s,t,e)=>new Promise((r,n)=>{var c=o=>{try{i(e.next(o))}catch(a){n(a)}},u=o=>{try{i(e.throw(o))}catch(a){n(a)}},i=o=>o.done?r(o.value):Promise.resolve(o.value).then(c,u);i((e=e.apply(s,t)).next())});function re(s){return s===s.toUpperCase()?s.charAt(0).toUpperCase()+s.slice(1).toLowerCase():s}const V={createGenericFields(){return{id:U.v4(),isDel:!1,createdAt:new Date,updatedAt:new Date}},applyPlugins(s,t){t.filter(e=>typeof e=="function").forEach(e=>s.plugin(e))},applyMiddlewares(s,t){t.forEach(({method:e,pre:r,post:n})=>{e&&r&&s.pre(e,r),e&&n&&s.post(e,n)})},createGenericSchema(s){return new s.Schema({id:{type:String,default:U.v4,unique:!0},isDel:{type:Boolean,default:!1}},{timestamps:!0})},createSchema({mongoose:s,schema:t,virtuals:e=[],standalone:r=!1}){const n=new s.Schema(t,{toJSON:{virtuals:!0},toObject:{virtuals:!0}});return e.forEach(({name:c,options:u,get:i})=>{if(V.isDynamicVirtual(u)){const o=n.statics;o._dynamicVirtuals||(o._dynamicVirtuals=[]),o._dynamicVirtuals.push({name:c,options:u});const a=n.virtual(c);i?a.get(i):a.get(function(){var f;return((f=this._populated)==null?void 0:f[c])||(u!=null&&u.count?0:u!=null&&u.justOne?null:[])})}else{const o=n.virtual(c,u);i&&o.get(i)}}),r||n.add(V.createGenericSchema(s)),n},createModel({mongoose:s,name:t,schema:e,virtuals:r=[],pagination:n=!0,aggregate:c=!0,middlewares:u=[]}){if(!t)throw new Error("Model name is required.");if(s.models[t])return s.models[t];const i=V.createSchema({mongoose:s,schema:e,virtuals:r});return(n||c)&&V.applyPlugins(i,[n&&W,c&&K]),V.applyMiddlewares(i,u),s.model(t,i)},validator:{isRequired(){return function(s){return l(this,null,function*(){return!J.validate.isEmpty(s)})}},isUnique(s){return function(t){return l(this,null,function*(){if(!Array.isArray(s)||s.length===0)throw new Error("Fields must be a non-empty array of strings.");const e={$or:s.map(n=>({[n]:t}))};return!(yield this.constructor.exists(e))})}},matchesRegex(s){return function(t){return l(this,null,function*(){if(!Array.isArray(s)||s.some(e=>!(e instanceof RegExp)))throw new Error("regexArray must be an array of valid RegExp objects.");return s.every(e=>e.test(t))})}}},migrate:b(O({},H),{setConfig:s=>{const t=`// This file is automatically generated by the Cyberskill CLI.
|
|
2
|
+
module.exports = ${JSON.stringify(s,null,4)}`;P.writeFileSync(A.PATH.MIGRATE_MONGO_CONFIG,t);const e=`
|
|
3
|
+
${A.MIGRATE_MONGO_CONFIG}
|
|
4
|
+
`;P.pathExistsSync(A.PATH.GIT_IGNORE)?P.readFileSync(A.PATH.GIT_IGNORE,"utf-8").split(`
|
|
5
|
+
`).includes(A.MIGRATE_MONGO_CONFIG)||P.appendFileSync(A.PATH.GIT_IGNORE,e):P.writeFileSync(A.PATH.GIT_IGNORE,e)}}),regexify(s,t){if(!s)return{};let e=I.cloneDeep(s);if(!t||t.length===0)return e;for(const r of t){const n=r.toString().split("."),c=$.getNestedValue(e,n);if(typeof c=="string"&&c.length>0){const u={$regex:`.*${Q.regexSearchMapper(c)}.*`,$options:"i"};e=$.setNestedValue(e,n,u)}}return e},isDynamicVirtual(s){return!!(s&&typeof s.ref=="function")},getNewRecords(s,t,e){return l(this,null,function*(){const r=yield s.findAll({});return r.success?t.filter(c=>!r.result.some(u=>e(u,c))):t})},getExistingRecords(s,t,e){return l(this,null,function*(){const r=yield s.findAll({});return r.success?r.result.filter(c=>t.some(u=>e(c,u))):[]})}};function se(s){return s!==null&&typeof s=="object"&&"toObject"in s&&typeof s.toObject=="function"}function F(s,t){if(!s||!t||t.length===0)return s;const e=new Set(t.map(r=>r.name));if(Array.isArray(s)){const r=s.filter(n=>{if(typeof n=="string")return!e.has(n);if(typeof n=="object"&&n!==null){const c=n;return!e.has(c.path||c.populate||"")}return!0});return r.length>0?r:void 0}if(typeof s=="string")return e.has(s)?void 0:s;if(typeof s=="object"&&s!==null){const r=s,n=r.path||r.populate||"";return e.has(n)?void 0:s}return s}function ne(s,t,e){if(!s.length||!t||!(e!=null&&e.ref))return[];const r=new Map;return s.forEach(n=>{try{const c=e.ref(n);if(c==null)return;const u=typeof c=="string"?c:String(c);if(u&&u.trim()!==""){const i=re(u);r.has(i)||r.set(i,[]),r.get(i).push(n)}}catch(c){d.catchError(new Error(`Dynamic ref function failed for virtual "${t}": ${c instanceof Error?c.message:String(c)}`))}}),Array.from(r.entries()).map(([n,c])=>({model:n,docs:c}))}function L(s,t,e,r,n){return l(this,null,function*(){if(!t.length||!e.length||!r)return t;const c=e.filter(o=>{if(Array.isArray(r))return r.length>0&&r.some(a=>typeof a=="string"?a===o.name:a.path===o.name);if(typeof r=="string")return r===o.name;if(typeof r=="object"&&r!==null){const a=r;return a.path&&a.path===o.name||a.populate&&a.populate===o.name}return!1});if(c.length===0)return t;const u=I.cloneDeep(t.map(o=>se(o)?o.toObject():o));u.forEach(o=>{c.forEach(({name:a,options:f})=>{if(!(a in o)){const h=o;h[a]=f.count?0:f.justOne?null:[]}})});const i=new Map;for(const o of c){const{name:a,options:f}=o,h=ne(u,a,f);for(const E of h){i.has(E.model)||i.set(E.model,{virtuals:[],localValueSets:new Map,docsByLocalValue:new Map});const m=i.get(E.model);m.virtuals.find(N=>N.name===a)||(m.virtuals.push(o),m.localValueSets.set(a,new Set));const M=m.localValueSets.get(a);E.docs.forEach(N=>{const x=N[f.localField];if(x!=null){const _=String(x);M.add(_);let R=-1;const v=N;v.id!==void 0?R=u.findIndex(T=>T.id===v.id):v._id!==void 0&&(R=u.findIndex(T=>{var S,y,p,g;return((y=(S=T._id)==null?void 0:S.toString)==null?void 0:y.call(S))===((g=(p=v._id)==null?void 0:p.toString)==null?void 0:g.call(p))})),R!==-1&&(m.docsByLocalValue.has(_)||m.docsByLocalValue.set(_,[]),m.docsByLocalValue.get(_).push(R))}})}}return yield Promise.all(Array.from(i.entries()).map(o=>l(null,[o],function*([a,f]){const h=s.models[a];if(!h)return;const E=new Set;if(f.localValueSets.forEach(_=>{_.forEach(R=>E.add(R))}),E.size===0)return;const m=[...new Set(f.virtuals.map(_=>_.options.foreignField))],M=Array.from(E);let N;m.length===1?N={[String(m[0])]:{$in:M}}:N={$or:m.map(_=>({[_]:{$in:M}}))};const x=yield h.find(N,n).lean();for(const _ of f.virtuals){const{name:R,options:v}=_,T=x.filter(S=>{const y=S[v.foreignField];return y!=null&&E.has(String(y))});if(v.count){const S=new Map;T.forEach(y=>{var p;const g=(p=y[v.foreignField])==null?void 0:p.toString();g&&S.set(g,(S.get(g)||0)+1)}),f.localValueSets.get(R).forEach(y=>{const p=f.docsByLocalValue.get(y)||[],g=S.get(y)||0;p.forEach(w=>{const C=u[w];C[R]===void 0&&(C[R]=g)})})}else{const S=new Map;T.forEach(y=>{var p;const g=(p=y[v.foreignField])==null?void 0:p.toString();g&&(S.has(g)||S.set(g,[]),S.get(g).push(y))}),f.localValueSets.get(R).forEach(y=>{const p=f.docsByLocalValue.get(y)||[],g=S.get(y)||[],w=v.justOne?g[0]||null:g;p.forEach(C=>{const B=u[C];B[R]=w})})}}}))),u})}class ce{constructor(t,e){te(this,"collection"),this.collection=t.collection(e)}createOne(t){return l(this,null,function*(){try{const e=O(O({},V.createGenericFields()),t);return(yield this.collection.insertOne(e)).acknowledged?{success:!0,message:"Document created successfully",result:e}:{success:!1,message:"Document creation failed",code:D.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(e){return d.catchError(e)}})}createMany(t){return l(this,null,function*(){try{const e=t.map(n=>O(O({},V.createGenericFields()),n)),r=yield this.collection.insertMany(e);return r.insertedCount===0?{success:!1,message:"No documents were inserted",code:D.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:`${r.insertedCount} documents created successfully`,result:e}}catch(e){return d.catchError(e)}})}findOne(t){return l(this,null,function*(){try{const e=yield this.collection.findOne(t);return e?{success:!0,message:"Document found",result:e}:{success:!1,message:"Document not found",code:D.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(e){return d.catchError(e)}})}findAll(){return l(this,arguments,function*(t={}){try{return{success:!0,message:"Documents retrieved successfully",result:yield this.collection.find(t).toArray()}}catch(e){return d.catchError(e)}})}count(){return l(this,arguments,function*(t={}){try{const e=yield this.collection.countDocuments(t);return{success:!0,message:`${e} documents counted successfully`,result:e}}catch(e){return d.catchError(e)}})}updateOne(t,e){return l(this,null,function*(){try{const r=yield this.collection.updateOne(t,{$set:e});return r.matchedCount===0?{success:!1,message:"No documents matched the filter",code:D.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document updated successfully",result:r}}catch(r){return d.catchError(r)}})}updateMany(t,e){return l(this,null,function*(){try{const r=yield this.collection.updateMany(t,{$set:e});return r.matchedCount===0?{success:!1,message:"No documents matched the filter",code:D.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents updated successfully",result:r}}catch(r){return d.catchError(r)}})}deleteOne(t){return l(this,null,function*(){try{const e=yield this.collection.deleteOne(t);return e.deletedCount===0?{success:!1,message:"No documents matched the filter",code:D.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document deleted successfully",result:e}}catch(e){return d.catchError(e)}})}deleteMany(t){return l(this,null,function*(){try{const e=yield this.collection.deleteMany(t);return e.deletedCount===0?{success:!1,message:"No documents matched the filter",code:D.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents deleted successfully",result:e}}catch(e){return d.catchError(e)}})}}class ue{constructor(t){this.model=t}getModelName(){return this.model.modelName}getDynamicVirtuals(){return this.model.schema.statics._dynamicVirtuals}populateDynamicVirtualsForDocument(t,e){return l(this,null,function*(){const r=this.getDynamicVirtuals();if(r&&r.length>0){const n=yield L(this.model.base,[t],r,e);return n&&n[0]?n[0]:t}return t})}populateDynamicVirtualsForDocuments(t,e){return l(this,null,function*(){const r=this.getDynamicVirtuals();return r&&r.length>0&&t.length>0?yield L(this.model.base,t,r,e):t})}findOne(){return l(this,arguments,function*(t={},e={},r={},n){try{const c=this.model.findOne(t,e,r),u=this.getDynamicVirtuals(),i=F(n,u);i&&c.populate(i);const o=yield c.exec();return o?{success:!0,result:yield this.populateDynamicVirtualsForDocument(o,n)}:{success:!1,message:`No ${this.getModelName()} found.`,code:D.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(c){return d.catchError(c)}})}findAll(){return l(this,arguments,function*(t={},e={},r={},n){try{const c=this.model.find(t,e,r),u=this.getDynamicVirtuals(),i=F(n,u);i&&c.populate(i);const o=yield c.exec();return{success:!0,result:yield this.populateDynamicVirtualsForDocuments(o,n)}}catch(c){return d.catchError(c)}})}findPaging(){return l(this,arguments,function*(t={},e={}){try{const r=this.getDynamicVirtuals(),n=O({},e);e.populate&&(n.populate=F(e.populate,r));const c=yield this.model.paginate(t,n),u=yield this.populateDynamicVirtualsForDocuments(c.docs,e.populate);return{success:!0,result:b(O({},c),{docs:u})}}catch(r){return d.catchError(r)}})}findPagingAggregate(t){return l(this,arguments,function*(e,r={}){try{const n=this.getDynamicVirtuals(),c=O({},r);r.populate&&(c.populate=F(r.populate,n));const u=yield this.model.aggregatePaginate(this.model.aggregate(e),c),i=yield this.populateDynamicVirtualsForDocuments(u.docs,r.populate);return{success:!0,result:b(O({},u),{docs:i})}}catch(n){return d.catchError(n)}})}count(){return l(this,arguments,function*(t={}){try{return{success:!0,result:yield this.model.countDocuments(t)}}catch(e){return d.catchError(e)}})}createOne(t){return l(this,null,function*(){try{return{success:!0,result:yield this.model.create(t)}}catch(e){return d.catchError(e)}})}createMany(t){return l(this,arguments,function*(e,r={}){try{return{success:!0,result:(yield this.model.insertMany(e,r)).map(u=>u instanceof k.Document?u.toObject():null).filter(u=>u!==null)}}catch(n){return d.catchError(n)}})}updateOne(){return l(this,arguments,function*(t={},e={},r={}){try{const n=yield this.model.findOneAndUpdate(t,e,O({new:!0},r)).exec();return n?{success:!0,result:n}:{success:!1,message:`Failed to update ${this.getModelName()}.`,code:D.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(n){return d.catchError(n)}})}updateMany(){return l(this,arguments,function*(t={},e={},r={}){try{return{success:!0,result:yield this.model.updateMany(t,e,r).exec()}}catch(n){return d.catchError(n)}})}deleteOne(){return l(this,arguments,function*(t={},e={}){try{const r=yield this.model.findOneAndDelete(t,e).exec();return r?{success:!0,result:r}:{success:!1,message:`No ${this.getModelName()} found to delete.`,code:D.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(r){return d.catchError(r)}})}deleteMany(){return l(this,arguments,function*(t={},e={}){try{const r=yield this.model.deleteMany(t,e).exec();return r.deletedCount===0?{success:!1,message:"No documents found to delete.",code:D.RESPONSE_STATUS.NOT_FOUND.CODE}:{success:!0,result:r}}catch(r){return d.catchError(r)}})}createShortId(t,e=4){return l(this,null,function*(){try{const n=Array.from({length:10},(i,o)=>j.generateShortId(t,o+e)),u=(yield Promise.all(n.map(i=>this.model.exists({shortId:i})))).findIndex(i=>!i);if(u!==-1){const i=n[u];if(i)return{success:!0,result:i}}return{success:!1,message:"Failed to create a unique shortId",code:D.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(r){return d.catchError(r)}})}createSlugQuery({slug:t,field:e,isObject:r,filter:n}){const c=O({},n!=null?n:{});return r?b(O({},c),{$or:[{[`slug.${e}`]:t},{slugHistory:{$elemMatch:{[`slug.${e}`]:t}}}]}):b(O({},c),{$or:[{slug:t},{slugHistory:t}]})}createUniqueSlug(t){return l(this,arguments,function*({slug:e,field:r,isObject:n,filter:c}){const u=j.generateSlug(e),o=Array.from({length:100},(h,E)=>E===0?u:`${u}-${E}`),f=(yield Promise.all(o.map(h=>this.model.exists(this.createSlugQuery({slug:h,field:r,isObject:n,filter:c}))))).findIndex(h=>!h);if(f!==-1){const h=o[f];if(h)return h}return`${u}-${Date.now()}`})}createSlug(t){return l(this,arguments,function*({field:e,from:r,filter:n}){try{const c=r[e];return I.isObject(c)?{success:!0,result:Object.fromEntries(yield Promise.all(Object.entries(c).map(a=>l(this,[a],function*([f,h]){const E=yield this.createUniqueSlug({slug:h,field:f,isObject:!0,filter:n});return[f,E]}))))}:{success:!0,result:yield this.createUniqueSlug({slug:c,field:e,isObject:!1,filter:n})}}catch(c){return d.catchError(c)}})}checkSlug(t){return l(this,arguments,function*({slug:e,field:r,from:n,filter:c}){try{const u=n[r];if(I.isObject(u)){const h=Object.values(u).map(m=>j.generateSlug(m));return(yield Promise.all(h.map(m=>this.model.exists(this.createSlugQuery({slug:m,field:r,isObject:!0,filter:c}))))).some(m=>m)?{success:!0,result:!0}:{success:!0,result:!1}}const o=j.generateSlug(e);return{success:!0,result:(yield this.model.exists(this.createSlugQuery({slug:o,field:r,isObject:!1,filter:c})))!==null}}catch(u){return d.catchError(u)}})}aggregate(t){return l(this,null,function*(){try{return{success:!0,result:yield this.model.aggregate(t)}}catch(e){return d.catchError(e)}})}}exports.MongoController=ce;exports.MongooseController=ue;exports.mongo=V;
|
|
@@ -160,6 +160,22 @@ export declare const mongo: {
|
|
|
160
160
|
* @returns True if the options contain a dynamic ref function.
|
|
161
161
|
*/
|
|
162
162
|
isDynamicVirtual<T, R extends string = string>(options?: T_VirtualOptions<T, R>): options is I_DynamicVirtualOptions<T, R>;
|
|
163
|
+
/**
|
|
164
|
+
* Generic utility function to get new records from the database
|
|
165
|
+
* @param controller - MongoController instance
|
|
166
|
+
* @param recordsToCheck - Array of records to check
|
|
167
|
+
* @param filterFn - Function to determine if a record already exists
|
|
168
|
+
* @returns Array of records that don't exist in the database
|
|
169
|
+
*/
|
|
170
|
+
getNewRecords<T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean): Promise<T[]>;
|
|
171
|
+
/**
|
|
172
|
+
* Generic utility function to get existing records that match the filter criteria
|
|
173
|
+
* @param controller - MongoController instance
|
|
174
|
+
* @param recordsToCheck - Array of records to check
|
|
175
|
+
* @param filterFn - Function to determine if a record exists
|
|
176
|
+
* @returns Array of existing records that match the filter criteria
|
|
177
|
+
*/
|
|
178
|
+
getExistingRecords<T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean): Promise<T_WithId<T>[]>;
|
|
163
179
|
};
|
|
164
180
|
/**
|
|
165
181
|
* MongoDB native driver controller for direct database operations.
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { cloneDeep as B, isObject as
|
|
1
|
+
import { cloneDeep as B, isObject as $ } from "lodash-es";
|
|
2
2
|
import K from "migrate-mongo";
|
|
3
3
|
import { Document as W } from "mongoose";
|
|
4
4
|
import Q from "mongoose-aggregate-paginate-v2";
|
|
5
5
|
import H from "mongoose-paginate-v2";
|
|
6
|
-
import { v4 as
|
|
6
|
+
import { v4 as T } from "uuid";
|
|
7
7
|
import { getNestedValue as J, setNestedValue as z } from "../../util/object/object.util.js";
|
|
8
8
|
import { regexSearchMapper as X } from "../../util/common/common.util.js";
|
|
9
9
|
import { writeFileSync as G, pathExistsSync as Y, readFileSync as Z, appendFileSync as ee } from "../fs/fs.util.js";
|
|
@@ -12,28 +12,28 @@ import { validate as te } from "../../util/validate/validate.util.js";
|
|
|
12
12
|
import { generateShortId as re, generateSlug as I } from "../../util/string/string.util.js";
|
|
13
13
|
import { RESPONSE_STATUS as v } from "../../constant/response-status.js";
|
|
14
14
|
import { catchError as d } from "../log/log.util.js";
|
|
15
|
-
var se = Object.defineProperty, ne = Object.defineProperties, ue = Object.getOwnPropertyDescriptors, L = Object.getOwnPropertySymbols, ce = Object.prototype.hasOwnProperty,
|
|
15
|
+
var se = Object.defineProperty, ne = Object.defineProperties, ue = Object.getOwnPropertyDescriptors, L = Object.getOwnPropertySymbols, ce = Object.prototype.hasOwnProperty, ie = Object.prototype.propertyIsEnumerable, j = (s, t, e) => t in s ? se(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e, S = (s, t) => {
|
|
16
16
|
for (var e in t || (t = {}))
|
|
17
17
|
ce.call(t, e) && j(s, e, t[e]);
|
|
18
18
|
if (L)
|
|
19
19
|
for (var e of L(t))
|
|
20
|
-
|
|
20
|
+
ie.call(t, e) && j(s, e, t[e]);
|
|
21
21
|
return s;
|
|
22
|
-
}, w = (s, t) => ne(s, ue(t)),
|
|
23
|
-
var u = (
|
|
22
|
+
}, w = (s, t) => ne(s, ue(t)), oe = (s, t, e) => j(s, typeof t != "symbol" ? t + "" : t, e), l = (s, t, e) => new Promise((r, n) => {
|
|
23
|
+
var u = (i) => {
|
|
24
24
|
try {
|
|
25
|
-
|
|
25
|
+
o(e.next(i));
|
|
26
26
|
} catch (a) {
|
|
27
27
|
n(a);
|
|
28
28
|
}
|
|
29
|
-
}, c = (
|
|
29
|
+
}, c = (i) => {
|
|
30
30
|
try {
|
|
31
|
-
|
|
31
|
+
o(e.throw(i));
|
|
32
32
|
} catch (a) {
|
|
33
33
|
n(a);
|
|
34
34
|
}
|
|
35
|
-
},
|
|
36
|
-
|
|
35
|
+
}, o = (i) => i.done ? r(i.value) : Promise.resolve(i.value).then(u, c);
|
|
36
|
+
o((e = e.apply(s, t)).next());
|
|
37
37
|
});
|
|
38
38
|
function le(s) {
|
|
39
39
|
return s === s.toUpperCase() ? s.charAt(0).toUpperCase() + s.slice(1).toLowerCase() : s;
|
|
@@ -48,7 +48,7 @@ const V = {
|
|
|
48
48
|
*/
|
|
49
49
|
createGenericFields() {
|
|
50
50
|
return {
|
|
51
|
-
id:
|
|
51
|
+
id: T(),
|
|
52
52
|
isDel: !1,
|
|
53
53
|
createdAt: /* @__PURE__ */ new Date(),
|
|
54
54
|
updatedAt: /* @__PURE__ */ new Date()
|
|
@@ -88,7 +88,7 @@ const V = {
|
|
|
88
88
|
createGenericSchema(s) {
|
|
89
89
|
return new s.Schema(
|
|
90
90
|
{
|
|
91
|
-
id: { type: String, default:
|
|
91
|
+
id: { type: String, default: T, unique: !0 },
|
|
92
92
|
isDel: { type: Boolean, default: !1 }
|
|
93
93
|
},
|
|
94
94
|
{ timestamps: !0 }
|
|
@@ -118,21 +118,21 @@ const V = {
|
|
|
118
118
|
toObject: { virtuals: !0 }
|
|
119
119
|
// So `console.log()` and other functions that use `toObject()` include virtuals
|
|
120
120
|
});
|
|
121
|
-
return e.forEach(({ name: u, options: c, get:
|
|
121
|
+
return e.forEach(({ name: u, options: c, get: o }) => {
|
|
122
122
|
if (V.isDynamicVirtual(c)) {
|
|
123
|
-
const
|
|
124
|
-
|
|
123
|
+
const i = n.statics;
|
|
124
|
+
i._dynamicVirtuals || (i._dynamicVirtuals = []), i._dynamicVirtuals.push({
|
|
125
125
|
name: u,
|
|
126
126
|
options: c
|
|
127
127
|
});
|
|
128
128
|
const a = n.virtual(u);
|
|
129
|
-
|
|
129
|
+
o ? a.get(o) : a.get(function() {
|
|
130
130
|
var f;
|
|
131
131
|
return ((f = this._populated) == null ? void 0 : f[u]) || (c != null && c.count ? 0 : c != null && c.justOne ? null : []);
|
|
132
132
|
});
|
|
133
133
|
} else {
|
|
134
|
-
const
|
|
135
|
-
|
|
134
|
+
const i = n.virtual(u, c);
|
|
135
|
+
o && i.get(o);
|
|
136
136
|
}
|
|
137
137
|
}), r || n.add(V.createGenericSchema(s)), n;
|
|
138
138
|
},
|
|
@@ -165,11 +165,11 @@ const V = {
|
|
|
165
165
|
throw new Error("Model name is required.");
|
|
166
166
|
if (s.models[t])
|
|
167
167
|
return s.models[t];
|
|
168
|
-
const
|
|
169
|
-
return (n || u) && V.applyPlugins(
|
|
168
|
+
const o = V.createSchema({ mongoose: s, schema: e, virtuals: r });
|
|
169
|
+
return (n || u) && V.applyPlugins(o, [
|
|
170
170
|
n && H,
|
|
171
171
|
u && Q
|
|
172
|
-
]), V.applyMiddlewares(
|
|
172
|
+
]), V.applyMiddlewares(o, c), s.model(t, o);
|
|
173
173
|
},
|
|
174
174
|
/**
|
|
175
175
|
* Validation utilities for Mongoose schemas.
|
|
@@ -286,6 +286,40 @@ ${q}
|
|
|
286
286
|
*/
|
|
287
287
|
isDynamicVirtual(s) {
|
|
288
288
|
return !!(s && typeof s.ref == "function");
|
|
289
|
+
},
|
|
290
|
+
/**
|
|
291
|
+
* Generic utility function to get new records from the database
|
|
292
|
+
* @param controller - MongoController instance
|
|
293
|
+
* @param recordsToCheck - Array of records to check
|
|
294
|
+
* @param filterFn - Function to determine if a record already exists
|
|
295
|
+
* @returns Array of records that don't exist in the database
|
|
296
|
+
*/
|
|
297
|
+
getNewRecords(s, t, e) {
|
|
298
|
+
return l(this, null, function* () {
|
|
299
|
+
const r = yield s.findAll({});
|
|
300
|
+
return r.success ? t.filter(
|
|
301
|
+
(u) => !r.result.some(
|
|
302
|
+
(c) => e(c, u)
|
|
303
|
+
)
|
|
304
|
+
) : t;
|
|
305
|
+
});
|
|
306
|
+
},
|
|
307
|
+
/**
|
|
308
|
+
* Generic utility function to get existing records that match the filter criteria
|
|
309
|
+
* @param controller - MongoController instance
|
|
310
|
+
* @param recordsToCheck - Array of records to check
|
|
311
|
+
* @param filterFn - Function to determine if a record exists
|
|
312
|
+
* @returns Array of existing records that match the filter criteria
|
|
313
|
+
*/
|
|
314
|
+
getExistingRecords(s, t, e) {
|
|
315
|
+
return l(this, null, function* () {
|
|
316
|
+
const r = yield s.findAll({});
|
|
317
|
+
return r.success ? r.result.filter(
|
|
318
|
+
(u) => t.some(
|
|
319
|
+
(c) => e(u, c)
|
|
320
|
+
)
|
|
321
|
+
) : [];
|
|
322
|
+
});
|
|
289
323
|
}
|
|
290
324
|
};
|
|
291
325
|
function ae(s) {
|
|
@@ -326,8 +360,8 @@ function de(s, t, e) {
|
|
|
326
360
|
return;
|
|
327
361
|
const c = typeof u == "string" ? u : String(u);
|
|
328
362
|
if (c && c.trim() !== "") {
|
|
329
|
-
const
|
|
330
|
-
r.has(
|
|
363
|
+
const o = le(c);
|
|
364
|
+
r.has(o) || r.set(o, []), r.get(o).push(n);
|
|
331
365
|
}
|
|
332
366
|
} catch (u) {
|
|
333
367
|
d(new Error(`Dynamic ref function failed for virtual "${t}": ${u instanceof Error ? u.message : String(u)}`));
|
|
@@ -338,47 +372,47 @@ function U(s, t, e, r, n) {
|
|
|
338
372
|
return l(this, null, function* () {
|
|
339
373
|
if (!t.length || !e.length || !r)
|
|
340
374
|
return t;
|
|
341
|
-
const u = e.filter((
|
|
375
|
+
const u = e.filter((i) => {
|
|
342
376
|
if (Array.isArray(r))
|
|
343
|
-
return r.length > 0 && r.some((a) => typeof a == "string" ? a ===
|
|
377
|
+
return r.length > 0 && r.some((a) => typeof a == "string" ? a === i.name : a.path === i.name);
|
|
344
378
|
if (typeof r == "string")
|
|
345
|
-
return r ===
|
|
379
|
+
return r === i.name;
|
|
346
380
|
if (typeof r == "object" && r !== null) {
|
|
347
381
|
const a = r;
|
|
348
|
-
return a.path && a.path ===
|
|
382
|
+
return a.path && a.path === i.name || a.populate && a.populate === i.name;
|
|
349
383
|
}
|
|
350
384
|
return !1;
|
|
351
385
|
});
|
|
352
386
|
if (u.length === 0)
|
|
353
387
|
return t;
|
|
354
388
|
const c = B(t.map(
|
|
355
|
-
(
|
|
389
|
+
(i) => ae(i) ? i.toObject() : i
|
|
356
390
|
));
|
|
357
|
-
c.forEach((
|
|
391
|
+
c.forEach((i) => {
|
|
358
392
|
u.forEach(({ name: a, options: f }) => {
|
|
359
|
-
if (!(a in
|
|
360
|
-
const m =
|
|
393
|
+
if (!(a in i)) {
|
|
394
|
+
const m = i;
|
|
361
395
|
m[a] = f.count ? 0 : f.justOne ? null : [];
|
|
362
396
|
}
|
|
363
397
|
});
|
|
364
398
|
});
|
|
365
|
-
const
|
|
366
|
-
for (const
|
|
367
|
-
const { name: a, options: f } =
|
|
399
|
+
const o = /* @__PURE__ */ new Map();
|
|
400
|
+
for (const i of u) {
|
|
401
|
+
const { name: a, options: f } = i, m = de(c, a, f);
|
|
368
402
|
for (const p of m) {
|
|
369
|
-
|
|
403
|
+
o.has(p.model) || o.set(p.model, {
|
|
370
404
|
virtuals: [],
|
|
371
405
|
localValueSets: /* @__PURE__ */ new Map(),
|
|
372
406
|
docsByLocalValue: /* @__PURE__ */ new Map()
|
|
373
407
|
});
|
|
374
|
-
const h =
|
|
375
|
-
h.virtuals.find((N) => N.name === a) || (h.virtuals.push(
|
|
376
|
-
const
|
|
408
|
+
const h = o.get(p.model);
|
|
409
|
+
h.virtuals.find((N) => N.name === a) || (h.virtuals.push(i), h.localValueSets.set(a, /* @__PURE__ */ new Set()));
|
|
410
|
+
const A = h.localValueSets.get(a);
|
|
377
411
|
p.docs.forEach((N) => {
|
|
378
|
-
const
|
|
379
|
-
if (
|
|
380
|
-
const D = String(
|
|
381
|
-
|
|
412
|
+
const M = N[f.localField];
|
|
413
|
+
if (M != null) {
|
|
414
|
+
const D = String(M);
|
|
415
|
+
A.add(D);
|
|
382
416
|
let _ = -1;
|
|
383
417
|
const R = N;
|
|
384
418
|
R.id !== void 0 ? _ = c.findIndex((b) => b.id === R.id) : R._id !== void 0 && (_ = c.findIndex((b) => {
|
|
@@ -389,7 +423,7 @@ function U(s, t, e, r, n) {
|
|
|
389
423
|
});
|
|
390
424
|
}
|
|
391
425
|
}
|
|
392
|
-
return yield Promise.all(Array.from(
|
|
426
|
+
return yield Promise.all(Array.from(o.entries()).map((i) => l(null, [i], function* ([a, f]) {
|
|
393
427
|
const m = s.models[a];
|
|
394
428
|
if (!m)
|
|
395
429
|
return;
|
|
@@ -398,12 +432,12 @@ function U(s, t, e, r, n) {
|
|
|
398
432
|
D.forEach((_) => p.add(_));
|
|
399
433
|
}), p.size === 0)
|
|
400
434
|
return;
|
|
401
|
-
const h = [...new Set(f.virtuals.map((D) => D.options.foreignField))],
|
|
435
|
+
const h = [...new Set(f.virtuals.map((D) => D.options.foreignField))], A = Array.from(p);
|
|
402
436
|
let N;
|
|
403
|
-
h.length === 1 ? N = { [String(h[0])]: { $in:
|
|
404
|
-
const
|
|
437
|
+
h.length === 1 ? N = { [String(h[0])]: { $in: A } } : N = { $or: h.map((D) => ({ [D]: { $in: A } })) };
|
|
438
|
+
const M = yield m.find(N, n).lean();
|
|
405
439
|
for (const D of f.virtuals) {
|
|
406
|
-
const { name: _, options: R } = D, b =
|
|
440
|
+
const { name: _, options: R } = D, b = M.filter((O) => {
|
|
407
441
|
const y = O[R.foreignField];
|
|
408
442
|
return y != null && p.has(String(y));
|
|
409
443
|
});
|
|
@@ -415,8 +449,8 @@ function U(s, t, e, r, n) {
|
|
|
415
449
|
g && O.set(g, (O.get(g) || 0) + 1);
|
|
416
450
|
}), f.localValueSets.get(_).forEach((y) => {
|
|
417
451
|
const E = f.docsByLocalValue.get(y) || [], g = O.get(y) || 0;
|
|
418
|
-
E.forEach((
|
|
419
|
-
const P = c[
|
|
452
|
+
E.forEach((C) => {
|
|
453
|
+
const P = c[C];
|
|
420
454
|
P[_] === void 0 && (P[_] = g);
|
|
421
455
|
});
|
|
422
456
|
});
|
|
@@ -427,10 +461,10 @@ function U(s, t, e, r, n) {
|
|
|
427
461
|
const g = (E = y[R.foreignField]) == null ? void 0 : E.toString();
|
|
428
462
|
g && (O.has(g) || O.set(g, []), O.get(g).push(y));
|
|
429
463
|
}), f.localValueSets.get(_).forEach((y) => {
|
|
430
|
-
const E = f.docsByLocalValue.get(y) || [], g = O.get(y) || [],
|
|
464
|
+
const E = f.docsByLocalValue.get(y) || [], g = O.get(y) || [], C = R.justOne ? g[0] || null : g;
|
|
431
465
|
E.forEach((P) => {
|
|
432
466
|
const k = c[P];
|
|
433
|
-
k[_] =
|
|
467
|
+
k[_] = C;
|
|
434
468
|
});
|
|
435
469
|
});
|
|
436
470
|
}
|
|
@@ -446,7 +480,7 @@ class Ve {
|
|
|
446
480
|
* @param collectionName - The name of the collection to operate on.
|
|
447
481
|
*/
|
|
448
482
|
constructor(t, e) {
|
|
449
|
-
|
|
483
|
+
oe(this, "collection"), this.collection = t.collection(e);
|
|
450
484
|
}
|
|
451
485
|
/**
|
|
452
486
|
* Creates a single document in the collection.
|
|
@@ -724,10 +758,10 @@ class be {
|
|
|
724
758
|
findOne() {
|
|
725
759
|
return l(this, arguments, function* (t = {}, e = {}, r = {}, n) {
|
|
726
760
|
try {
|
|
727
|
-
const u = this.model.findOne(t, e, r), c = this.getDynamicVirtuals(),
|
|
728
|
-
|
|
729
|
-
const
|
|
730
|
-
return
|
|
761
|
+
const u = this.model.findOne(t, e, r), c = this.getDynamicVirtuals(), o = F(n, c);
|
|
762
|
+
o && u.populate(o);
|
|
763
|
+
const i = yield u.exec();
|
|
764
|
+
return i ? { success: !0, result: yield this.populateDynamicVirtualsForDocument(i, n) } : {
|
|
731
765
|
success: !1,
|
|
732
766
|
message: `No ${this.getModelName()} found.`,
|
|
733
767
|
code: v.NOT_FOUND.CODE
|
|
@@ -750,10 +784,10 @@ class be {
|
|
|
750
784
|
findAll() {
|
|
751
785
|
return l(this, arguments, function* (t = {}, e = {}, r = {}, n) {
|
|
752
786
|
try {
|
|
753
|
-
const u = this.model.find(t, e, r), c = this.getDynamicVirtuals(),
|
|
754
|
-
|
|
755
|
-
const
|
|
756
|
-
return { success: !0, result: yield this.populateDynamicVirtualsForDocuments(
|
|
787
|
+
const u = this.model.find(t, e, r), c = this.getDynamicVirtuals(), o = F(n, c);
|
|
788
|
+
o && u.populate(o);
|
|
789
|
+
const i = yield u.exec();
|
|
790
|
+
return { success: !0, result: yield this.populateDynamicVirtualsForDocuments(i, n) };
|
|
757
791
|
} catch (u) {
|
|
758
792
|
return d(u);
|
|
759
793
|
}
|
|
@@ -794,8 +828,8 @@ class be {
|
|
|
794
828
|
const c = yield this.model.aggregatePaginate(
|
|
795
829
|
this.model.aggregate(e),
|
|
796
830
|
u
|
|
797
|
-
),
|
|
798
|
-
return { success: !0, result: w(S({}, c), { docs:
|
|
831
|
+
), o = yield this.populateDynamicVirtualsForDocuments(c.docs, r.populate);
|
|
832
|
+
return { success: !0, result: w(S({}, c), { docs: o }) };
|
|
799
833
|
} catch (n) {
|
|
800
834
|
return d(n);
|
|
801
835
|
}
|
|
@@ -941,13 +975,13 @@ class be {
|
|
|
941
975
|
createShortId(t, e = 4) {
|
|
942
976
|
return l(this, null, function* () {
|
|
943
977
|
try {
|
|
944
|
-
const n = Array.from({ length: 10 }, (
|
|
945
|
-
n.map((
|
|
946
|
-
)).findIndex((
|
|
978
|
+
const n = Array.from({ length: 10 }, (o, i) => re(t, i + e)), c = (yield Promise.all(
|
|
979
|
+
n.map((o) => this.model.exists({ shortId: o }))
|
|
980
|
+
)).findIndex((o) => !o);
|
|
947
981
|
if (c !== -1) {
|
|
948
|
-
const
|
|
949
|
-
if (
|
|
950
|
-
return { success: !0, result:
|
|
982
|
+
const o = n[c];
|
|
983
|
+
if (o)
|
|
984
|
+
return { success: !0, result: o };
|
|
951
985
|
}
|
|
952
986
|
return {
|
|
953
987
|
success: !1,
|
|
@@ -997,13 +1031,13 @@ class be {
|
|
|
997
1031
|
*/
|
|
998
1032
|
createUniqueSlug(t) {
|
|
999
1033
|
return l(this, arguments, function* ({ slug: e, field: r, isObject: n, filter: u }) {
|
|
1000
|
-
const c = I(e),
|
|
1001
|
-
|
|
1034
|
+
const c = I(e), i = Array.from({ length: 100 }, (m, p) => p === 0 ? c : `${c}-${p}`), f = (yield Promise.all(
|
|
1035
|
+
i.map(
|
|
1002
1036
|
(m) => this.model.exists(this.createSlugQuery({ slug: m, field: r, isObject: n, filter: u }))
|
|
1003
1037
|
)
|
|
1004
1038
|
)).findIndex((m) => !m);
|
|
1005
1039
|
if (f !== -1) {
|
|
1006
|
-
const m =
|
|
1040
|
+
const m = i[f];
|
|
1007
1041
|
if (m)
|
|
1008
1042
|
return m;
|
|
1009
1043
|
}
|
|
@@ -1024,7 +1058,7 @@ class be {
|
|
|
1024
1058
|
return l(this, arguments, function* ({ field: e, from: r, filter: n }) {
|
|
1025
1059
|
try {
|
|
1026
1060
|
const u = r[e];
|
|
1027
|
-
return
|
|
1061
|
+
return $(u) ? { success: !0, result: Object.fromEntries(
|
|
1028
1062
|
yield Promise.all(
|
|
1029
1063
|
Object.entries(u).map((a) => l(this, [a], function* ([f, m]) {
|
|
1030
1064
|
const p = yield this.createUniqueSlug({
|
|
@@ -1062,7 +1096,7 @@ class be {
|
|
|
1062
1096
|
return l(this, arguments, function* ({ slug: e, field: r, from: n, filter: u }) {
|
|
1063
1097
|
try {
|
|
1064
1098
|
const c = n[r];
|
|
1065
|
-
if (
|
|
1099
|
+
if ($(c)) {
|
|
1066
1100
|
const m = Object.values(c).map((h) => I(h));
|
|
1067
1101
|
return (yield Promise.all(
|
|
1068
1102
|
m.map(
|
|
@@ -1075,9 +1109,9 @@ class be {
|
|
|
1075
1109
|
)
|
|
1076
1110
|
)).some((h) => h) ? { success: !0, result: !0 } : { success: !0, result: !1 };
|
|
1077
1111
|
}
|
|
1078
|
-
const
|
|
1112
|
+
const i = I(e);
|
|
1079
1113
|
return { success: !0, result: (yield this.model.exists(this.createSlugQuery({
|
|
1080
|
-
slug:
|
|
1114
|
+
slug: i,
|
|
1081
1115
|
field: r,
|
|
1082
1116
|
isObject: !1,
|
|
1083
1117
|
filter: u
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./upload.constant.cjs"),a=require("./upload.type.cjs"),e=require("./upload.util.cjs");exports.BYTES_PER_MB=t.BYTES_PER_MB;Object.defineProperty(exports,"E_UploadType",{enumerable:!0,get:()=>a.E_UploadType});exports.createUploadConfig=e.createUploadConfig;exports.getAndValidateFile=e.getAndValidateFile;exports.getFileSizeFromStream=e.getFileSizeFromStream;exports.getFileWebStream=e.getFileWebStream;exports.upload=e.upload;exports.validateFileExtension=e.validateFileExtension;exports.validateUpload=e.validateUpload;
|
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { BYTES_PER_MB as o } from "./upload.constant.js";
|
|
2
|
+
import { E_UploadType as l } from "./upload.type.js";
|
|
3
|
+
import { createUploadConfig as r, getAndValidateFile as d, getFileSizeFromStream as p, getFileWebStream as m, upload as F, validateFileExtension as f, validateUpload as g } from "./upload.util.js";
|
|
3
4
|
export {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
o as BYTES_PER_MB,
|
|
6
|
+
l as E_UploadType,
|
|
7
|
+
r as createUploadConfig,
|
|
8
|
+
d as getAndValidateFile,
|
|
9
|
+
p as getFileSizeFromStream,
|
|
10
|
+
m as getFileWebStream,
|
|
11
|
+
F as upload,
|
|
12
|
+
f as validateFileExtension,
|
|
13
|
+
g as validateUpload
|
|
7
14
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=1024*1024;exports.BYTES_PER_MB=e;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const BYTES_PER_MB: number;
|
|
@@ -18,19 +18,16 @@ export interface I_UploadConfig {
|
|
|
18
18
|
[E_UploadType.DOCUMENT]: I_UploadTypeConfig;
|
|
19
19
|
[E_UploadType.OTHER]: I_UploadTypeConfig;
|
|
20
20
|
}
|
|
21
|
+
export interface I_UploadFileData {
|
|
22
|
+
createReadStream: () => NodeJS.ReadableStream;
|
|
23
|
+
filename: string;
|
|
24
|
+
}
|
|
25
|
+
export interface I_UploadFile {
|
|
26
|
+
file: I_UploadFileData;
|
|
27
|
+
}
|
|
21
28
|
export interface I_UploadOptions {
|
|
22
|
-
file: Promise<
|
|
23
|
-
file: {
|
|
24
|
-
createReadStream: () => NodeJS.ReadableStream;
|
|
25
|
-
filename: string;
|
|
26
|
-
};
|
|
27
|
-
}>;
|
|
29
|
+
file: Promise<I_UploadFile>;
|
|
28
30
|
path: string;
|
|
29
31
|
type: E_UploadType;
|
|
30
32
|
config?: I_UploadConfig;
|
|
31
33
|
}
|
|
32
|
-
export interface I_UploadResult {
|
|
33
|
-
success: boolean;
|
|
34
|
-
message: string;
|
|
35
|
-
result: string;
|
|
36
|
-
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const z=require("node:buffer"),C=require("node:stream"),x=require("node:stream/web"),A=require("./upload.constant.cjs"),c=require("./upload.type.cjs"),D=require("../path/path.util.cjs"),S=require("../fs/fs.util.cjs"),f=require("../../constant/response-status.cjs");var P=Object.defineProperty,v=Object.getOwnPropertySymbols,h=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable,T=(r,t,e)=>t in r?P(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,O=(r,t)=>{for(var e in t||(t={}))h.call(t,e)&&T(r,e,t[e]);if(v)for(var e of v(t))F.call(t,e)&&T(r,e,t[e]);return r},m=(r,t,e)=>new Promise((i,a)=>{var s=l=>{try{o(e.next(l))}catch(d){a(d)}},n=l=>{try{o(e.throw(l))}catch(d){a(d)}},o=l=>l.done?i(l.value):Promise.resolve(l.value).then(s,n);o((e=e.apply(r,t)).next())});function w(r){return m(this,null,function*(){return new Promise((t,e)=>{let i=0;r.on("data",a=>{i+=a.length}),r.on("end",()=>t(i)),r.on("error",e)})})}function _(r,t,e){return m(this,null,function*(){const i=yield(yield t).file,a=i.createReadStream(),s=yield w(a),n=e!=null?e:y(),o=U({filename:i.filename,fileSize:s},n,r);return o.isValid?{success:!0,result:i,message:"File validated successfully"}:{success:!1,message:o.error||"File validation failed",code:f.RESPONSE_STATUS.BAD_REQUEST.CODE}})}function L(r,t,e){return m(this,null,function*(){const a=(e!=null?e:y())[r],s=yield _(r,t,e);if(!s.success)return s;const{createReadStream:n}=s.result;let o=a.sizeLimit;const l=new C.Transform({transform(u,p,g){o-=u.length,o<0?g(new Error(`File size exceeds limit of ${a.sizeLimit/A.BYTES_PER_MB}MB`)):g(null,u)}}),E=n().pipe(l);return{success:!0,result:new x.ReadableStream({start(u){E.on("data",p=>{u.enqueue(typeof p=="string"?z.Buffer.from(p):p)}),E.on("end",()=>u.close()),E.on("error",p=>u.error(p))}})}})}function R(r,t){const e=r.lastIndexOf(".");if(e===-1)return!1;const i=r.substring(e+1).toLowerCase();return t.includes(i)}function U(r,t,e){const{filename:i,fileSize:a}=r,s=t[e],{allowedExtensions:n,sizeLimit:o}=s;if(!R(i,n))return{isValid:!1,error:`File extension not allowed for ${e.toLowerCase()} files. Allowed extensions: ${n.join(", ")}`};if(a!==void 0&&a>o){const l=Math.round(o/1048576);return{isValid:!1,error:`File size exceeds limit for ${e.toLowerCase()} files. Maximum size: ${l}MB`}}return{isValid:!0}}function y(r){const t={[c.E_UploadType.IMAGE]:{allowedExtensions:["jpg","jpeg","png","gif","webp","svg"],sizeLimit:5242880},[c.E_UploadType.VIDEO]:{allowedExtensions:["mp4","avi","mov","wmv","flv","webm"],sizeLimit:524288e3},[c.E_UploadType.DOCUMENT]:{allowedExtensions:["pdf","doc","docx","txt","rtf"],sizeLimit:10485760},[c.E_UploadType.OTHER]:{allowedExtensions:["zip","rar","tar","gz"],sizeLimit:5242880}};return O(O({},t),r)}function b(r){return m(this,null,function*(){const{path:t,file:e,config:i,type:a}=r;if(!t||typeof t!="string")return{success:!1,message:"Invalid path provided",code:f.RESPONSE_STATUS.BAD_REQUEST.CODE};if(!e||typeof e!="object")return{success:!1,message:"Invalid file provided",code:f.RESPONSE_STATUS.BAD_REQUEST.CODE};if(i){const s=[c.E_UploadType.IMAGE,c.E_UploadType.VIDEO,c.E_UploadType.DOCUMENT,c.E_UploadType.OTHER];for(const n of s){if(!i[n]||!Array.isArray(i[n].allowedExtensions)||i[n].allowedExtensions.length===0)return{success:!1,message:`Invalid config for ${n.toLowerCase()} files`,code:f.RESPONSE_STATUS.BAD_REQUEST.CODE};if(typeof i[n].sizeLimit!="number"||i[n].sizeLimit<=0)return{success:!1,message:`Invalid size limit for ${n.toLowerCase()} files`,code:f.RESPONSE_STATUS.BAD_REQUEST.CODE}}}try{const s=yield _(a,yield e,i);if(!s.success)return s;const{createReadStream:n}=s.result,o=D.dirname(t);S.pathExistsSync(o)||S.mkdirSync(o,{recursive:!0});const l=n(),d=S.createWriteStream(t);return l.pipe(d),yield new Promise((E,u)=>{d.on("finish",()=>E()),d.on("error",u),l.on("error",u)}),{success:!0,result:t,message:"File uploaded successfully",code:f.RESPONSE_STATUS.OK.CODE}}catch(s){return{success:!1,message:s instanceof Error?s.message:"File upload failed",code:f.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}})}exports.createUploadConfig=y;exports.getAndValidateFile=_;exports.getFileSizeFromStream=w;exports.getFileWebStream=L;exports.upload=b;exports.validateFileExtension=R;exports.validateUpload=U;
|
|
@@ -1,4 +1,71 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ReadableStream } from 'node:stream/web';
|
|
2
|
+
import { I_Return } from '../../typescript/index.js';
|
|
3
|
+
import { I_UploadConfig, I_UploadFile, I_UploadFileData, I_UploadOptions, I_UploadValidationConfig, E_UploadType } from './upload.type.js';
|
|
4
|
+
/**
|
|
5
|
+
* Calculates the size of a file from a readable stream.
|
|
6
|
+
* This function reads through the entire stream to determine the total byte size
|
|
7
|
+
* by accumulating the length of each data chunk.
|
|
8
|
+
*
|
|
9
|
+
* @param stream - The readable stream to calculate the size for.
|
|
10
|
+
* @returns A promise that resolves to the total size of the stream in bytes.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getFileSizeFromStream(stream: NodeJS.ReadableStream): Promise<number>;
|
|
13
|
+
/**
|
|
14
|
+
* Extracts and validates file data from an upload file.
|
|
15
|
+
* This function processes upload files by:
|
|
16
|
+
* - Extracting file metadata and creating a readable stream
|
|
17
|
+
* - Calculating the file size from the stream
|
|
18
|
+
* - Validating file size and extension against upload configuration
|
|
19
|
+
* - Returning a standardized response with success status and error codes
|
|
20
|
+
* - Providing validated file data for further processing
|
|
21
|
+
*
|
|
22
|
+
* @param type - The type of upload being processed (IMAGE, VIDEO, DOCUMENT, OTHER).
|
|
23
|
+
* @param file - The upload file object containing file metadata and stream creation method.
|
|
24
|
+
* @param config - Optional upload configuration. If not provided, uses default configuration.
|
|
25
|
+
* @returns A promise that resolves to a standardized response containing validated file data or error information.
|
|
26
|
+
*/
|
|
27
|
+
export declare function getAndValidateFile(type: E_UploadType, file: I_UploadFile, config?: I_UploadConfig): Promise<I_Return<I_UploadFileData>>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a validated web-readable stream from an upload file with size validation.
|
|
30
|
+
* This function processes file uploads for web environments by:
|
|
31
|
+
* - Validating file data using getAndValidateFile function
|
|
32
|
+
* - Creating a size validation transform stream to monitor upload progress
|
|
33
|
+
* - Returning a web-compatible ReadableStream with real-time validation
|
|
34
|
+
* - Providing standardized error responses for validation failures
|
|
35
|
+
* - Wrapping the stream in a standardized response format
|
|
36
|
+
*
|
|
37
|
+
* @param type - The type of upload being processed (IMAGE, VIDEO, DOCUMENT, OTHER).
|
|
38
|
+
* @param file - The upload file object containing file metadata and stream creation method.
|
|
39
|
+
* @param config - Optional upload configuration. If not provided, uses default configuration.
|
|
40
|
+
* @returns A promise that resolves to a standardized response containing either a web ReadableStream or error information.
|
|
41
|
+
*/
|
|
42
|
+
export declare function getFileWebStream(type: E_UploadType, file: I_UploadFile, config?: I_UploadConfig): Promise<I_Return<ReadableStream<Uint8Array>>>;
|
|
43
|
+
/**
|
|
44
|
+
* Validates if a file has an allowed extension.
|
|
45
|
+
* This function extracts the file extension from the filename and checks if it's
|
|
46
|
+
* included in the list of allowed extensions (case-insensitive comparison).
|
|
47
|
+
*
|
|
48
|
+
* @param filename - The filename to check for valid extension.
|
|
49
|
+
* @param allowedExtensions - An array of allowed file extensions (without dots).
|
|
50
|
+
* @returns True if the file extension is allowed, false otherwise.
|
|
51
|
+
*/
|
|
52
|
+
export declare function validateFileExtension(filename: string, allowedExtensions: string[]): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Validates an upload against the specified configuration.
|
|
55
|
+
* This function performs comprehensive validation including:
|
|
56
|
+
* - File extension validation against allowed extensions
|
|
57
|
+
* - File size validation against size limits
|
|
58
|
+
* - Returns detailed error messages for validation failures
|
|
59
|
+
*
|
|
60
|
+
* @param config - The validation configuration including filename and optional file size.
|
|
61
|
+
* @param uploadConfig - The upload configuration containing allowed extensions and size limits.
|
|
62
|
+
* @param uploadType - The type of upload being validated.
|
|
63
|
+
* @returns An object indicating validation success and optional error message.
|
|
64
|
+
*/
|
|
65
|
+
export declare function validateUpload(config: I_UploadValidationConfig, uploadConfig: I_UploadConfig, uploadType: E_UploadType): {
|
|
66
|
+
isValid: boolean;
|
|
67
|
+
error?: string;
|
|
68
|
+
};
|
|
2
69
|
/**
|
|
3
70
|
* Creates a default upload configuration with predefined settings for different file types.
|
|
4
71
|
* This function provides sensible defaults for image, video, document, and other file types,
|
|
@@ -13,12 +80,12 @@ export declare function createUploadConfig(overrides?: Partial<I_UploadConfig>):
|
|
|
13
80
|
* This function processes file uploads with the following features:
|
|
14
81
|
* - Input validation for path and file parameters
|
|
15
82
|
* - Configuration validation for all upload types
|
|
16
|
-
* - File
|
|
83
|
+
* - File validation using getAndValidateFile function
|
|
17
84
|
* - Automatic directory creation
|
|
18
85
|
* - Stream-based file writing
|
|
19
|
-
* - Comprehensive error handling
|
|
86
|
+
* - Comprehensive error handling with standardized response codes
|
|
20
87
|
*
|
|
21
88
|
* @param options - Upload configuration including file, path, type, and optional validation config.
|
|
22
|
-
* @returns A promise that resolves to
|
|
89
|
+
* @returns A promise that resolves to a standardized response with success status, message, file path, and response codes.
|
|
23
90
|
*/
|
|
24
|
-
export declare function upload(options: I_UploadOptions): Promise<
|
|
91
|
+
export declare function upload(options: I_UploadOptions): Promise<I_Return<string>>;
|
|
@@ -1,154 +1,196 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { Buffer as O } from "node:buffer";
|
|
2
|
+
import { Transform as x } from "node:stream";
|
|
3
|
+
import { ReadableStream as z } from "node:stream/web";
|
|
4
|
+
import { BYTES_PER_MB as C } from "./upload.constant.js";
|
|
5
|
+
import { E_UploadType as u } from "./upload.type.js";
|
|
6
|
+
import { dirname as D } from "../path/path.util.js";
|
|
7
|
+
import { pathExistsSync as R, mkdirSync as h, createWriteStream as L } from "../fs/fs.util.js";
|
|
8
|
+
import { RESPONSE_STATUS as d } from "../../constant/response-status.js";
|
|
9
|
+
var T = Object.defineProperty, v = Object.getOwnPropertySymbols, A = Object.prototype.hasOwnProperty, I = Object.prototype.propertyIsEnumerable, y = (t, r, e) => r in t ? T(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e, S = (t, r) => {
|
|
5
10
|
for (var e in r || (r = {}))
|
|
6
|
-
|
|
11
|
+
A.call(r, e) && y(t, e, r[e]);
|
|
7
12
|
if (v)
|
|
8
13
|
for (var e of v(r))
|
|
9
|
-
I.call(r, e) &&
|
|
10
|
-
return
|
|
11
|
-
},
|
|
12
|
-
var
|
|
14
|
+
I.call(r, e) && y(t, e, r[e]);
|
|
15
|
+
return t;
|
|
16
|
+
}, E = (t, r, e) => new Promise((i, a) => {
|
|
17
|
+
var s = (l) => {
|
|
13
18
|
try {
|
|
14
|
-
|
|
19
|
+
n(e.next(l));
|
|
15
20
|
} catch (c) {
|
|
16
|
-
|
|
21
|
+
a(c);
|
|
17
22
|
}
|
|
18
|
-
},
|
|
23
|
+
}, o = (l) => {
|
|
19
24
|
try {
|
|
20
|
-
|
|
25
|
+
n(e.throw(l));
|
|
21
26
|
} catch (c) {
|
|
22
|
-
|
|
27
|
+
a(c);
|
|
23
28
|
}
|
|
24
|
-
},
|
|
25
|
-
|
|
29
|
+
}, n = (l) => l.done ? i(l.value) : Promise.resolve(l.value).then(s, o);
|
|
30
|
+
n((e = e.apply(t, r)).next());
|
|
26
31
|
});
|
|
27
|
-
function
|
|
28
|
-
return
|
|
32
|
+
function P(t) {
|
|
33
|
+
return E(this, null, function* () {
|
|
29
34
|
return new Promise((r, e) => {
|
|
30
|
-
let
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}),
|
|
35
|
+
let i = 0;
|
|
36
|
+
t.on("data", (a) => {
|
|
37
|
+
i += a.length;
|
|
38
|
+
}), t.on("end", () => r(i)), t.on("error", e);
|
|
34
39
|
});
|
|
35
40
|
});
|
|
36
41
|
}
|
|
37
|
-
function
|
|
38
|
-
|
|
42
|
+
function w(t, r, e) {
|
|
43
|
+
return E(this, null, function* () {
|
|
44
|
+
const i = yield (yield r).file, a = i.createReadStream(), s = yield P(a), o = e != null ? e : _(), n = F(
|
|
45
|
+
{ filename: i.filename, fileSize: s },
|
|
46
|
+
o,
|
|
47
|
+
t
|
|
48
|
+
);
|
|
49
|
+
return n.isValid ? {
|
|
50
|
+
success: !0,
|
|
51
|
+
result: i,
|
|
52
|
+
message: "File validated successfully"
|
|
53
|
+
} : {
|
|
54
|
+
success: !1,
|
|
55
|
+
message: n.error || "File validation failed",
|
|
56
|
+
code: d.BAD_REQUEST.CODE
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function q(t, r, e) {
|
|
61
|
+
return E(this, null, function* () {
|
|
62
|
+
const a = (e != null ? e : _())[t], s = yield w(t, r, e);
|
|
63
|
+
if (!s.success)
|
|
64
|
+
return s;
|
|
65
|
+
const { createReadStream: o } = s.result;
|
|
66
|
+
let n = a.sizeLimit;
|
|
67
|
+
const l = new x({
|
|
68
|
+
transform(f, m, g) {
|
|
69
|
+
n -= f.length, n < 0 ? g(new Error(`File size exceeds limit of ${a.sizeLimit / C}MB`)) : g(null, f);
|
|
70
|
+
}
|
|
71
|
+
}), p = o().pipe(l);
|
|
72
|
+
return {
|
|
73
|
+
success: !0,
|
|
74
|
+
result: new z({
|
|
75
|
+
start(f) {
|
|
76
|
+
p.on("data", (m) => {
|
|
77
|
+
f.enqueue(typeof m == "string" ? O.from(m) : m);
|
|
78
|
+
}), p.on("end", () => f.close()), p.on("error", (m) => f.error(m));
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function B(t, r) {
|
|
85
|
+
const e = t.lastIndexOf(".");
|
|
39
86
|
if (e === -1)
|
|
40
87
|
return !1;
|
|
41
|
-
const
|
|
42
|
-
return r.includes(
|
|
88
|
+
const i = t.substring(e + 1).toLowerCase();
|
|
89
|
+
return r.includes(i);
|
|
43
90
|
}
|
|
44
|
-
function
|
|
45
|
-
const { filename:
|
|
46
|
-
if (!
|
|
91
|
+
function F(t, r, e) {
|
|
92
|
+
const { filename: i, fileSize: a } = t, s = r[e], { allowedExtensions: o, sizeLimit: n } = s;
|
|
93
|
+
if (!B(i, o))
|
|
47
94
|
return {
|
|
48
95
|
isValid: !1,
|
|
49
|
-
error: `File extension not allowed for ${e.toLowerCase()} files. Allowed extensions: ${
|
|
96
|
+
error: `File extension not allowed for ${e.toLowerCase()} files. Allowed extensions: ${o.join(", ")}`
|
|
50
97
|
};
|
|
51
|
-
if (
|
|
52
|
-
const
|
|
98
|
+
if (a !== void 0 && a > n) {
|
|
99
|
+
const l = Math.round(n / 1048576);
|
|
53
100
|
return {
|
|
54
101
|
isValid: !1,
|
|
55
|
-
error: `File size exceeds limit for ${e.toLowerCase()} files. Maximum size: ${
|
|
102
|
+
error: `File size exceeds limit for ${e.toLowerCase()} files. Maximum size: ${l}MB`
|
|
56
103
|
};
|
|
57
104
|
}
|
|
58
105
|
return { isValid: !0 };
|
|
59
106
|
}
|
|
60
|
-
function
|
|
107
|
+
function _(t) {
|
|
61
108
|
const r = {
|
|
62
|
-
[
|
|
109
|
+
[u.IMAGE]: {
|
|
63
110
|
allowedExtensions: ["jpg", "jpeg", "png", "gif", "webp", "svg"],
|
|
64
111
|
sizeLimit: 5242880
|
|
65
112
|
// 5MB
|
|
66
113
|
},
|
|
67
|
-
[
|
|
114
|
+
[u.VIDEO]: {
|
|
68
115
|
allowedExtensions: ["mp4", "avi", "mov", "wmv", "flv", "webm"],
|
|
69
116
|
sizeLimit: 524288e3
|
|
70
117
|
// 500MB
|
|
71
118
|
},
|
|
72
|
-
[
|
|
119
|
+
[u.DOCUMENT]: {
|
|
73
120
|
allowedExtensions: ["pdf", "doc", "docx", "txt", "rtf"],
|
|
74
121
|
sizeLimit: 10485760
|
|
75
122
|
// 10MB
|
|
76
123
|
},
|
|
77
|
-
[
|
|
124
|
+
[u.OTHER]: {
|
|
78
125
|
allowedExtensions: ["zip", "rar", "tar", "gz"],
|
|
79
126
|
sizeLimit: 5242880
|
|
80
127
|
// 5MB
|
|
81
128
|
}
|
|
82
129
|
};
|
|
83
|
-
return
|
|
130
|
+
return S(S({}, r), t);
|
|
84
131
|
}
|
|
85
|
-
function
|
|
86
|
-
return
|
|
87
|
-
const { path: r, file: e, config:
|
|
132
|
+
function G(t) {
|
|
133
|
+
return E(this, null, function* () {
|
|
134
|
+
const { path: r, file: e, config: i, type: a } = t;
|
|
88
135
|
if (!r || typeof r != "string")
|
|
89
136
|
return {
|
|
90
137
|
success: !1,
|
|
91
138
|
message: "Invalid path provided",
|
|
92
|
-
|
|
139
|
+
code: d.BAD_REQUEST.CODE
|
|
93
140
|
};
|
|
94
141
|
if (!e || typeof e != "object")
|
|
95
142
|
return {
|
|
96
143
|
success: !1,
|
|
97
144
|
message: "Invalid file provided",
|
|
98
|
-
|
|
145
|
+
code: d.BAD_REQUEST.CODE
|
|
99
146
|
};
|
|
100
|
-
if (
|
|
101
|
-
const
|
|
102
|
-
for (const
|
|
103
|
-
if (!
|
|
147
|
+
if (i) {
|
|
148
|
+
const s = [u.IMAGE, u.VIDEO, u.DOCUMENT, u.OTHER];
|
|
149
|
+
for (const o of s) {
|
|
150
|
+
if (!i[o] || !Array.isArray(i[o].allowedExtensions) || i[o].allowedExtensions.length === 0)
|
|
104
151
|
return {
|
|
105
152
|
success: !1,
|
|
106
|
-
message: `Invalid config for ${
|
|
107
|
-
|
|
153
|
+
message: `Invalid config for ${o.toLowerCase()} files`,
|
|
154
|
+
code: d.BAD_REQUEST.CODE
|
|
108
155
|
};
|
|
109
|
-
if (typeof
|
|
156
|
+
if (typeof i[o].sizeLimit != "number" || i[o].sizeLimit <= 0)
|
|
110
157
|
return {
|
|
111
158
|
success: !1,
|
|
112
|
-
message: `Invalid size limit for ${
|
|
113
|
-
|
|
159
|
+
message: `Invalid size limit for ${o.toLowerCase()} files`,
|
|
160
|
+
code: d.BAD_REQUEST.CODE
|
|
114
161
|
};
|
|
115
162
|
}
|
|
116
163
|
}
|
|
117
164
|
try {
|
|
118
|
-
const
|
|
119
|
-
if (
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return {
|
|
127
|
-
success: !1,
|
|
128
|
-
message: p.error || "File validation failed",
|
|
129
|
-
result: ""
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
const o = E(r);
|
|
133
|
-
x(o) || z(o, { recursive: !0 });
|
|
134
|
-
const c = a(), d = h(r);
|
|
135
|
-
return c.pipe(d), yield new Promise((m, u) => {
|
|
136
|
-
d.on("finish", () => m()), d.on("error", u), c.on("error", u);
|
|
165
|
+
const s = yield w(a, yield e, i);
|
|
166
|
+
if (!s.success)
|
|
167
|
+
return s;
|
|
168
|
+
const { createReadStream: o } = s.result, n = D(r);
|
|
169
|
+
R(n) || h(n, { recursive: !0 });
|
|
170
|
+
const l = o(), c = L(r);
|
|
171
|
+
return l.pipe(c), yield new Promise((p, f) => {
|
|
172
|
+
c.on("finish", () => p()), c.on("error", f), l.on("error", f);
|
|
137
173
|
}), {
|
|
138
174
|
success: !0,
|
|
175
|
+
result: r,
|
|
139
176
|
message: "File uploaded successfully",
|
|
140
|
-
|
|
177
|
+
code: d.OK.CODE
|
|
141
178
|
};
|
|
142
|
-
} catch (
|
|
179
|
+
} catch (s) {
|
|
143
180
|
return {
|
|
144
181
|
success: !1,
|
|
145
|
-
message:
|
|
146
|
-
|
|
182
|
+
message: s instanceof Error ? s.message : "File upload failed",
|
|
183
|
+
code: d.INTERNAL_SERVER_ERROR.CODE
|
|
147
184
|
};
|
|
148
185
|
}
|
|
149
186
|
});
|
|
150
187
|
}
|
|
151
188
|
export {
|
|
152
|
-
|
|
153
|
-
|
|
189
|
+
_ as createUploadConfig,
|
|
190
|
+
w as getAndValidateFile,
|
|
191
|
+
P as getFileSizeFromStream,
|
|
192
|
+
q as getFileWebStream,
|
|
193
|
+
G as upload,
|
|
194
|
+
B as validateFileExtension,
|
|
195
|
+
F as validateUpload
|
|
154
196
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("lodash-es");var c=Object.defineProperty,y=Object.defineProperties,d=Object.getOwnPropertyDescriptors,u=Object.getOwnPropertySymbols,m=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable,a=(t,e,n)=>e in t?c(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,l=(t,e)=>{for(var n in e||(e={}))m.call(e,n)&&a(t,n,e[n]);if(u)for(var n of u(e))v.call(e,n)&&a(t,n,e[n]);return t},f=(t,e)=>y(t,d(e));function g(t){try{return JSON.parse(t),!0}catch(e){return!1}}function _(t,e){return e.reduce((n,o)=>{if(n&&typeof n=="object"&&o in n)return n[o]},t)}function p(t,e,n){if(e.length===0)return t;const[o,...r]=e;if(r.length===0)return f(l({},t),{[o]:n});const i=t[o];return f(l({},t),{[o]:p(typeof i=="object"&&i!==null?i:{},r,n)})}function h(...t){if(t.length===0)return{};const e=t.filter(r=>r!=null).map(r=>typeof r!="object"?{}:r);if(e.length===0)return{};if(e.length===1)return e[0];if(e.every(s.isArray))return[].concat(...e);if(e.every(r=>typeof r=="object"&&r!==null&&!s.isArray(r)))return s.mergeWith({},...e,(r,i)=>{if(s.isArray(r)&&s.isArray(i))return r.concat(i)});if(e.every(r=>typeof r!="object"||r===null))throw new Error("deepMerge: Cannot merge primitive values. All arguments must be objects or arrays.");const n=e.some(s.isArray),o=e.some(r=>typeof r=="object"&&r!==null&&!s.isArray(r));throw n&&o?new Error("deepMerge: Cannot mix arrays and objects. All arguments must be either arrays or objects."):new Error("deepMerge: Invalid arguments provided. All arguments must be objects or arrays of the same type.")}exports.deepMerge=h;exports.getNestedValue=_;exports.isJSON=g;exports.setNestedValue=p;
|
|
@@ -30,13 +30,44 @@ export declare function getNestedValue<T>(obj: T, path: (string | number)[]): un
|
|
|
30
30
|
export declare function setNestedValue<T>(obj: T, path: (string | number)[], value: unknown): T;
|
|
31
31
|
/**
|
|
32
32
|
* Deep merges multiple objects or arrays into a single object or array.
|
|
33
|
-
* This function handles different types of merging:
|
|
33
|
+
* This function handles different types of merging with improved type safety and error handling:
|
|
34
34
|
* - If all arguments are arrays, it concatenates them
|
|
35
35
|
* - If all arguments are objects, it deeply merges them (concatenating arrays within objects)
|
|
36
|
-
* -
|
|
36
|
+
* - Handles null/undefined values gracefully by treating them as empty objects
|
|
37
|
+
* - Provides better type inference and safety
|
|
38
|
+
* - Throws descriptive errors for invalid input
|
|
37
39
|
*
|
|
38
|
-
* @param args - The objects or arrays to merge.
|
|
40
|
+
* @param args - The objects or arrays to merge. Can be empty, in which case returns an empty object.
|
|
39
41
|
* @returns The merged result - either a concatenated array or a deeply merged object.
|
|
40
|
-
* @throws {Error} When arguments are mixed types (some arrays, some objects).
|
|
42
|
+
* @throws {Error} When arguments are mixed types (some arrays, some objects) or when all arguments are primitive values.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // Merge objects
|
|
47
|
+
* deepMerge({ a: 1 }, { b: 2 }, { a: 3 }) // { a: 3, b: 2 }
|
|
48
|
+
*
|
|
49
|
+
* // Merge arrays
|
|
50
|
+
* deepMerge([1, 2], [3, 4]) // [1, 2, 3, 4]
|
|
51
|
+
*
|
|
52
|
+
* // Handle null/undefined
|
|
53
|
+
* deepMerge({ a: 1 }, null, undefined, { b: 2 }) // { a: 1, b: 2 }
|
|
54
|
+
*
|
|
55
|
+
* // Nested objects with arrays
|
|
56
|
+
* deepMerge(
|
|
57
|
+
* { items: [1, 2], config: { theme: 'dark' } },
|
|
58
|
+
* { items: [3, 4], config: { size: 'large' } }
|
|
59
|
+
* ) // { items: [1, 2, 3, 4], config: { theme: 'dark', size: 'large' } }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
/**
|
|
63
|
+
* Deep merges multiple objects into a single object.
|
|
64
|
+
* @param args - The objects to merge. Can be empty, in which case returns an empty object.
|
|
65
|
+
* @returns The merged object.
|
|
66
|
+
*/
|
|
67
|
+
export declare function deepMerge<T extends Record<string, unknown>>(...args: (T | null | undefined)[]): T;
|
|
68
|
+
/**
|
|
69
|
+
* Deep merges multiple arrays into a single array.
|
|
70
|
+
* @param args - The arrays to merge. Can be empty, in which case returns an empty array.
|
|
71
|
+
* @returns The merged array.
|
|
41
72
|
*/
|
|
42
|
-
export declare function deepMerge<T
|
|
73
|
+
export declare function deepMerge<T extends unknown[]>(...args: (T | null | undefined)[]): T;
|
|
@@ -1,55 +1,75 @@
|
|
|
1
|
-
import { isArray as i,
|
|
2
|
-
var
|
|
3
|
-
for (var
|
|
4
|
-
y.call(
|
|
5
|
-
if (
|
|
6
|
-
for (var
|
|
7
|
-
|
|
8
|
-
return
|
|
9
|
-
},
|
|
10
|
-
function
|
|
1
|
+
import { isArray as i, mergeWith as l } from "lodash-es";
|
|
2
|
+
var c = Object.defineProperty, d = Object.defineProperties, m = Object.getOwnPropertyDescriptors, u = Object.getOwnPropertySymbols, y = Object.prototype.hasOwnProperty, v = Object.prototype.propertyIsEnumerable, f = (t, e, n) => e in t ? c(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n, a = (t, e) => {
|
|
3
|
+
for (var n in e || (e = {}))
|
|
4
|
+
y.call(e, n) && f(t, n, e[n]);
|
|
5
|
+
if (u)
|
|
6
|
+
for (var n of u(e))
|
|
7
|
+
v.call(e, n) && f(t, n, e[n]);
|
|
8
|
+
return t;
|
|
9
|
+
}, p = (t, e) => d(t, m(e));
|
|
10
|
+
function O(t) {
|
|
11
11
|
try {
|
|
12
|
-
return JSON.parse(
|
|
13
|
-
} catch (
|
|
12
|
+
return JSON.parse(t), !0;
|
|
13
|
+
} catch (e) {
|
|
14
14
|
return !1;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
function
|
|
18
|
-
return
|
|
19
|
-
if (
|
|
20
|
-
return
|
|
21
|
-
},
|
|
17
|
+
function b(t, e) {
|
|
18
|
+
return e.reduce((n, o) => {
|
|
19
|
+
if (n && typeof n == "object" && o in n)
|
|
20
|
+
return n[o];
|
|
21
|
+
}, t);
|
|
22
22
|
}
|
|
23
|
-
function
|
|
23
|
+
function _(t, e, n) {
|
|
24
|
+
if (e.length === 0)
|
|
25
|
+
return t;
|
|
26
|
+
const [o, ...r] = e;
|
|
24
27
|
if (r.length === 0)
|
|
25
|
-
return
|
|
26
|
-
|
|
27
|
-
if (p.length === 0)
|
|
28
|
-
return a(f({}, e), {
|
|
29
|
-
[n]: t
|
|
28
|
+
return p(a({}, t), {
|
|
29
|
+
[o]: n
|
|
30
30
|
});
|
|
31
|
-
const
|
|
32
|
-
return a(
|
|
33
|
-
[
|
|
34
|
-
typeof
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
const s = t[o];
|
|
32
|
+
return p(a({}, t), {
|
|
33
|
+
[o]: _(
|
|
34
|
+
typeof s == "object" && s !== null ? s : {},
|
|
35
|
+
r,
|
|
36
|
+
n
|
|
37
37
|
)
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
|
-
function
|
|
40
|
+
function g(...t) {
|
|
41
|
+
if (t.length === 0)
|
|
42
|
+
return {};
|
|
43
|
+
const e = t.filter((r) => r != null).map((r) => typeof r != "object" ? {} : r);
|
|
44
|
+
if (e.length === 0)
|
|
45
|
+
return {};
|
|
46
|
+
if (e.length === 1)
|
|
47
|
+
return e[0];
|
|
41
48
|
if (e.every(i))
|
|
42
49
|
return [].concat(...e);
|
|
43
|
-
if (e.every(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
50
|
+
if (e.every(
|
|
51
|
+
(r) => typeof r == "object" && r !== null && !i(r)
|
|
52
|
+
))
|
|
53
|
+
return l({}, ...e, (r, s) => {
|
|
54
|
+
if (i(r) && i(s))
|
|
55
|
+
return r.concat(s);
|
|
47
56
|
});
|
|
48
|
-
|
|
57
|
+
if (e.every((r) => typeof r != "object" || r === null))
|
|
58
|
+
throw new Error(
|
|
59
|
+
"deepMerge: Cannot merge primitive values. All arguments must be objects or arrays."
|
|
60
|
+
);
|
|
61
|
+
const n = e.some(i), o = e.some(
|
|
62
|
+
(r) => typeof r == "object" && r !== null && !i(r)
|
|
63
|
+
);
|
|
64
|
+
throw n && o ? new Error(
|
|
65
|
+
"deepMerge: Cannot mix arrays and objects. All arguments must be either arrays or objects."
|
|
66
|
+
) : new Error(
|
|
67
|
+
"deepMerge: Invalid arguments provided. All arguments must be objects or arrays of the same type."
|
|
68
|
+
);
|
|
49
69
|
}
|
|
50
70
|
export {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
71
|
+
g as deepMerge,
|
|
72
|
+
b as getNestedValue,
|
|
73
|
+
O as isJSON,
|
|
74
|
+
_ as setNestedValue
|
|
55
75
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cyberskill/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.5.0",
|
|
5
5
|
"description": "CyberSkill Shared",
|
|
6
6
|
"author": "Stephen Cheng",
|
|
7
7
|
"license": "MIT",
|
|
@@ -208,11 +208,11 @@
|
|
|
208
208
|
},
|
|
209
209
|
"dependencies": {
|
|
210
210
|
"@antfu/eslint-config": "5.0.0",
|
|
211
|
-
"@apollo/client": "3.13.
|
|
212
|
-
"@apollo/client-integration-nextjs": "0.12.
|
|
211
|
+
"@apollo/client": "3.13.9",
|
|
212
|
+
"@apollo/client-integration-nextjs": "0.12.3",
|
|
213
213
|
"@apollo/server": "5.0.0",
|
|
214
214
|
"@as-integrations/express5": "1.1.2",
|
|
215
|
-
"@dotenvx/dotenvx": "1.48.
|
|
215
|
+
"@dotenvx/dotenvx": "1.48.4",
|
|
216
216
|
"@eddeee888/gcg-typescript-resolver-files": "0.12.1",
|
|
217
217
|
"@eslint-react/eslint-plugin": "1.52.3",
|
|
218
218
|
"@graphql-codegen/cli": "5.0.7",
|
|
@@ -256,8 +256,8 @@
|
|
|
256
256
|
"node-fetch": "3.3.2",
|
|
257
257
|
"node-persist": "4.0.4",
|
|
258
258
|
"qs": "6.14.0",
|
|
259
|
-
"react": "19.1.
|
|
260
|
-
"react-dom": "19.1.
|
|
259
|
+
"react": "19.1.1",
|
|
260
|
+
"react-dom": "19.1.1",
|
|
261
261
|
"react-hot-toast": "2.5.2",
|
|
262
262
|
"react-i18next": "15.6.1",
|
|
263
263
|
"slugify": "1.6.6",
|
|
@@ -271,8 +271,8 @@
|
|
|
271
271
|
"@commitlint/cli": "19.8.1",
|
|
272
272
|
"@commitlint/config-conventional": "19.8.1",
|
|
273
273
|
"@eslint/config-inspector": "1.1.0",
|
|
274
|
-
"@next/eslint-plugin-next": "15.4.
|
|
275
|
-
"@testing-library/jest-dom": "6.6.
|
|
274
|
+
"@next/eslint-plugin-next": "15.4.5",
|
|
275
|
+
"@testing-library/jest-dom": "6.6.4",
|
|
276
276
|
"@testing-library/react": "16.3.0",
|
|
277
277
|
"@types/apollo-upload-client": "18.0.0",
|
|
278
278
|
"@types/body-parser": "1.19.6",
|
|
@@ -289,15 +289,15 @@
|
|
|
289
289
|
"@types/migrate-mongo": "10.0.5",
|
|
290
290
|
"@types/node": "24.1.0",
|
|
291
291
|
"@types/node-persist": "3.1.8",
|
|
292
|
-
"@types/react": "19.1.
|
|
293
|
-
"@types/react-dom": "19.1.
|
|
292
|
+
"@types/react": "19.1.9",
|
|
293
|
+
"@types/react-dom": "19.1.7",
|
|
294
294
|
"@types/unorm": "1.3.31",
|
|
295
295
|
"@types/uuid": "10.0.0",
|
|
296
296
|
"@types/ws": "8.18.1",
|
|
297
297
|
"@types/yargs": "17.0.33",
|
|
298
298
|
"@vitest/browser": "3.2.4",
|
|
299
299
|
"eslint": "9.32.0",
|
|
300
|
-
"eslint-config-next": "15.4.
|
|
300
|
+
"eslint-config-next": "15.4.5",
|
|
301
301
|
"glob": "11.0.3",
|
|
302
302
|
"lint-staged": "16.1.2",
|
|
303
303
|
"node-modules-inspector": "1.0.0",
|