vesal 0.1.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/vesal.cjs +2 -0
- package/dist/vesal.cjs.map +1 -0
- package/dist/vesal.d.ts +128 -0
- package/dist/vesal.mjs +2 -0
- package/dist/vesal.mjs.map +1 -0
- package/dist/vesal.modern.js +2 -0
- package/dist/vesal.modern.js.map +1 -0
- package/dist/vesal.umd.js +2 -0
- package/dist/vesal.umd.js.map +1 -0
- package/package.json +89 -0
- package/src/vesal.ts +310 -0
package/dist/vesal.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function e(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(e=function(){return!!t})()}function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},t.apply(this,arguments)}function r(e){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},r(e)}function n(e,t){return n=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},n(e,t)}function o(t){var s="function"==typeof Map?new Map:void 0;return o=function(t){if(null===t||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==s){if(s.has(t))return s.get(t);s.set(t,o)}function o(){return function(t,r,o){if(e())return Reflect.construct.apply(null,arguments);var s=[null];s.push.apply(s,r);var i=new(t.bind.apply(t,s));return o&&n(i,o.prototype),i}(t,arguments,r(this).constructor)}return o.prototype=Object.create(t.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),n(o,t)},o(t)}var s;function i(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}var c=/*#__PURE__*/function(){function e(e,t,r){return this.apiUrl="http://vesal.armaghan.net:8080/rest",this.username=void 0,this.password=void 0,this.from=void 0,this.username=e||"",this.password=t,this.from=r,this}var r=e.prototype;return r.Api=function(e,r,n){void 0===r&&(r="POST");try{var o,s,c=function(e){function t(e){if(!s.references)throw new a(void 0,"The server didn't respond correctly");return s}var r=i(function(){return Promise.resolve(o.json()).then(function(e){s=e})},function(){throw new a(void 0,"The server didn't respond correctly")});return r&&r.then?r.then(t):t()},u=this;n=t({},n,{username:u.username,password:u.password});var f=i(function(){return Promise.resolve(fetch(u.apiUrl+"/"+e,{headers:{Accept:"application/json","Content-Type":"application/json"},method:r,body:n&&JSON.stringify(n)})).then(function(e){o=e})},function(){throw new a(void 0,"Server connection failed")});return Promise.resolve(f&&f.then?f.then(c):c())}catch(e){return Promise.reject(e)}},r.Send=function(e){var r=e.recipients,n=e.messages,o=e.from;try{if(!r||!n)throw new a(void 0,"recipients and messages should not be empty");"string"==typeof r&&(r=[r]),o||(o=this.from);var s=Array.isArray(n)||Array.isArray(o);if(s){if(!r.length)throw new a(void 0,"recipients and messages should have the same length");if(Array.isArray(n)&&!n.length)throw new a(void 0,"recipients and messages should have the same length");if("string"==typeof n&&(n=Array(r.length).fill(n)),"string"==typeof o&&(o=Array(r.length).fill(o)),r.length!==n.length||r.length!==o.length)throw new a(void 0,"recipients and messages should have the same length")}var i=t({destinations:r},s?{originators:o}:{originator:o},s?{contents:n}:{content:n});return Promise.resolve(this.Api(s?"ManyToMany":"OneToMany","POST",i)).then(function(e){var r,n=0,o=0;return null==(r=e.references)||r.map(function(e){e?n++:o++}),t({},e,{references:e.references||[],count:{success:n,fail:o}})})}catch(e){return Promise.reject(e)}},r.GetMessageStatus=function(e){try{return Promise.resolve(this.Api("messageState","POST",{referenceids:e}))}catch(e){return Promise.reject(e)}},r.GetReceivedMessages=function(){try{return Promise.resolve(this.Api("pullReceivedMessages"))}catch(e){return Promise.reject(e)}},r.GetReceivedMessagesCount=function(){try{return Promise.resolve(this.Api("receivedMessageCount"))}catch(e){return Promise.reject(e)}},r.GetUserInfo=function(){try{return Promise.resolve(this.Api("userInfo"))}catch(e){return Promise.reject(e)}},e}(),a=/*#__PURE__*/function(e){var t,r;function o(t,r){var n,o,s;return t&&Object.keys(f).includes(""+t)?(o=u(t),s=t):(o=r||"Unknown Vesal error",s=void 0),(n=e.call(this,o)||this).status=void 0,n.message=o,n.status=s,n}return r=e,(t=o).prototype=Object.create(r.prototype),t.prototype.constructor=t,n(t,r),o}(/*#__PURE__*/o(Error));function u(e){return 0===e?"success":Object.keys(f).includes(""+e)?f[e]:""}var f=((s={0:"عملیات با موفقیت انجام شد"})[-100]="refrenceId مورد نظر یافت نشد",s[-101]="احراز هویت کاربر موفقیت آمیز نبود",s[-102]="نام کاربری یافت نشد",s[-103]="شماره originator اشتباه یا در بازه شماره های کاربر نیست",s[-104]="اعتبار کم است",s[-105]="فرمت درخواست اشتباه است",s[-106]="تعداد refrenceId ها بیش از 1000 عدد است",s[-107]="شماره گیرنده پیامک اشتباه است",s[-109]="تاریخ انقضای حساب کاربری فرارسیده است",s[-110]="درخواست از ip مجاز کاربر ارسال نشده است",s[-111]="شماره گیرنده در بلک لیست قرار دارد",s[-112]="حساب مشتری فعال نیست",s[-115]="فرمت UDH اشتباه است",s[-117]="مقدار mclass وارد شده اشتباه است",s[-118]="شماره پورت وارد شده صحیح نیست",s[-119]="کاربر به سرویس مورد نظر دسترسی ندارد",s[-120]="پیام ارسال شده دارای هیچ شماره معتبری نیست",s[-200]="خطای داخلی در پایگاه داده رخ داده است",s[-201]="خطای نامشخص داخل پایگاه داده",s[-137]="پیام نباید حاوی کلمات غیرمجاز می باشد",s);exports.GetStatusText=u,exports.Vesal=c,exports.VesalError=a,exports.default=c,exports.messageStates={0:"پیامک در صف ارسال قرار دارد",1:"ارسال شده",2:"پیامک به موبایل گیرنده تحویل شده است",3:"پیامک به موبایل گیرنده تحویل نشده است",4:"وضعیت نامشخص",5:"پیامک توسط وب سرویس به شرکت ارمغان راه طلایی رسیده است",6:"پیام از سمت اپراتور لغو شده است",7:"پیام از سمت اپراتور منقضی شده است",8:"پیام از سمت اپراتور reject شده است"},exports.vesalErrors=f;
|
|
2
|
+
//# sourceMappingURL=vesal.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vesal.cjs","sources":["../src/vesal.ts"],"sourcesContent":["/**\r\n * The Vesal API Class\r\n * @author Shahab Movahhedi\r\n * @see {@link https://shmovahhedi.com Shahab Movahhedi's Website}\r\n * @see {@link https://github.com/movahhedi/vesal vesal's Repository}\r\n * @license MIT\r\n */\r\nexport class Vesal {\r\n\tpublic readonly apiUrl = \"http://vesal.armaghan.net:8080/rest\";\r\n\tprivate username: string;\r\n\tprivate password: string;\r\n\tprivate from: string;\r\n\r\n\tconstructor(username: string, password: string, from: string) {\r\n\t\tthis.username = username || \"\";\r\n\t\tthis.password = password;\r\n\t\tthis.from = from;\r\n\r\n\t\treturn this;\r\n\t}\r\n\r\n\t/**\r\n\t * Make an API request to the Vesal API\r\n\t * @private\r\n\t * @param {string} urlSuffix - The URL suffix for the API endpoint\r\n\t * @param {(\"GET\"|\"POST\")} [method=\"GET\"] - The HTTP method to use for the request\r\n\t * @param {object|null} [data=null] - The data to send with the request\r\n\t * @returns {Promise} The response from the API\r\n\t */\r\n\tprivate async Api(\r\n\t\turlSuffix: string,\r\n\t\tmethod: \"GET\" | \"POST\" = \"POST\",\r\n\t\tdata?: object,\r\n\t): Promise<any> {\r\n\t\tlet response: Response, responseBody: any;\r\n\r\n\t\tdata = {\r\n\t\t\t...data,\r\n\t\t\tusername: this.username,\r\n\t\t\tpassword: this.password,\r\n\t\t};\r\n\r\n\t\ttry {\r\n\t\t\tresponse = await fetch(`${this.apiUrl}/${urlSuffix}`, {\r\n\t\t\t\theaders: {\r\n\t\t\t\t\tAccept: \"application/json\",\r\n\t\t\t\t\t\"Content-Type\": \"application/json\",\r\n\t\t\t\t},\r\n\t\t\t\tmethod,\r\n\t\t\t\tbody: data && JSON.stringify(data),\r\n\t\t\t});\r\n\t\t} catch (error) {\r\n\t\t\tthrow new VesalError(undefined, \"Server connection failed\");\r\n\t\t}\r\n\r\n\t\ttry {\r\n\t\t\tresponseBody = await response.json();\r\n\t\t} catch (error) {\r\n\t\t\tthrow new VesalError(undefined, \"The server didn't respond correctly\");\r\n\t\t}\r\n\r\n\t\tif (!responseBody.references) {\r\n\t\t\tthrow new VesalError(undefined, \"The server didn't respond correctly\");\r\n\t\t}\r\n\r\n\t\treturn responseBody;\r\n\t}\r\n\r\n\tasync Send({\r\n\t\trecipients,\r\n\t\tmessages,\r\n\t\tfrom,\r\n\t}: {\r\n\t\trecipients: string | string[];\r\n\t\tmessages: string | string[];\r\n\t\tfrom?: string | string[];\r\n\t}): Promise<IVesalResponse_Send_WithCount> {\r\n\t\tif (!recipients || !messages) {\r\n\t\t\tthrow new VesalError(\r\n\t\t\t\tundefined,\r\n\t\t\t\t\"recipients and messages should not be empty\",\r\n\t\t\t);\r\n\t\t}\r\n\r\n\t\tif (typeof recipients === \"string\") {\r\n\t\t\trecipients = [recipients];\r\n\t\t}\r\n\r\n\t\tif (!from) {\r\n\t\t\tfrom = this.from;\r\n\t\t}\r\n\r\n\t\tconst isManyToMany = Array.isArray(messages) || Array.isArray(from);\r\n\r\n\t\tif (isManyToMany) {\r\n\t\t\tif (!recipients.length) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\t\t\tif (Array.isArray(messages) && !messages.length) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\r\n\t\t\tif (typeof messages === \"string\") {\r\n\t\t\t\t// messages = recipients.map(() => messages);\r\n\t\t\t\tmessages = Array(recipients.length).fill(messages);\r\n\t\t\t}\r\n\t\t\tif (typeof from === \"string\") {\r\n\t\t\t\t// from = recipients.map(() => from);\r\n\t\t\t\tfrom = Array(recipients.length).fill(from);\r\n\t\t\t}\r\n\r\n\t\t\tif (\r\n\t\t\t\trecipients.length !== messages.length ||\r\n\t\t\t\trecipients.length !== from.length\r\n\t\t\t) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst sendData = {\r\n\t\t\tdestinations: recipients,\r\n\t\t\t...(isManyToMany ? { originators: from } : { originator: from }),\r\n\t\t\t...(isManyToMany ? { contents: messages } : { content: messages }),\r\n\t\t};\r\n\r\n\t\tconst result: IVesalResponse_Send = await this.Api(\r\n\t\t\tisManyToMany ? \"ManyToMany\" : \"OneToMany\",\r\n\t\t\t\"POST\",\r\n\t\t\tsendData,\r\n\t\t);\r\n\r\n\t\tlet successCount: number = 0,\r\n\t\t\tfailCount: number = 0;\r\n\r\n\t\tresult.references?.map((messageResult) => {\r\n\t\t\tif (messageResult) {\r\n\t\t\t\tsuccessCount++;\r\n\t\t\t} else {\r\n\t\t\t\tfailCount++;\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\treturn {\r\n\t\t\t...result,\r\n\t\t\treferences: result.references || [],\r\n\t\t\tcount: {\r\n\t\t\t\tsuccess: successCount,\r\n\t\t\t\tfail: failCount,\r\n\t\t\t},\r\n\t\t};\r\n\t}\r\n\r\n\tasync GetMessageStatus(\r\n\t\treferencesIds: number[],\r\n\t): Promise<IVesalResponse_MessageState> {\r\n\t\treturn await this.Api(\"messageState\", \"POST\", {\r\n\t\t\treferenceids: referencesIds,\r\n\t\t});\r\n\t}\r\n\r\n\tasync GetReceivedMessages(): Promise<IVesalResponse_ReceivedMessages> {\r\n\t\treturn await this.Api(\"pullReceivedMessages\");\r\n\t}\r\n\r\n\tasync GetReceivedMessagesCount(): Promise<IVesalResponse_ReceivedMessagesCount> {\r\n\t\treturn await this.Api(\"receivedMessageCount\");\r\n\t}\r\n\r\n\tasync GetUserInfo(): Promise<IVesalResponse_UserInfo> {\r\n\t\treturn await this.Api(\"userInfo\");\r\n\t}\r\n}\r\n\r\nexport class VesalError extends Error {\r\n\tstatus: keyof typeof vesalErrors | undefined;\r\n\r\n\tconstructor(statusParam: number | undefined, messageParam?: string) {\r\n\t\tlet message: string, status: keyof typeof vesalErrors | undefined;\r\n\r\n\t\tif (statusParam && Object.keys(vesalErrors).includes(\"\" + statusParam)) {\r\n\t\t\tmessage = GetStatusText(statusParam);\r\n\t\t\tstatus = statusParam as any;\r\n\t\t} else {\r\n\t\t\tmessage = messageParam || \"Unknown Vesal error\";\r\n\t\t\tstatus = undefined;\r\n\t\t}\r\n\r\n\t\tsuper(message);\r\n\r\n\t\tthis.message = message;\r\n\t\tthis.status = status;\r\n\t}\r\n}\r\n\r\ninterface IVesalResponse_Base {\r\n\terrorModel: {\r\n\t\terrorCode: number;\r\n\t\ttimestamp: string | number | null;\r\n\t};\r\n}\r\n\r\ninterface IVesalResponse_Send extends IVesalResponse_Base {\r\n\treferences: (number | Omit<keyof typeof vesalErrors, 0>)[];\r\n}\r\n\r\ninterface IVesalResponse_Send_WithCount extends IVesalResponse_Send {\r\n\tcount: {\r\n\t\tsuccess: number;\r\n\t\tfail: number;\r\n\t};\r\n}\r\n\r\ninterface IVesalResponse_MessageState extends IVesalResponse_Base {\r\n\tstates: {\r\n\t\tid: number;\r\n\t\tstate: number;\r\n\t}[];\r\n}\r\ninterface IVesalResponse_ReceivedMessages extends IVesalResponse_Base {\r\n\tmessageModels: {\r\n\t\toriginator: string;\r\n\t\tdestination: string;\r\n\t\tcontent: string;\r\n\t}[];\r\n}\r\ninterface IVesalResponse_ReceivedMessagesCount extends IVesalResponse_Base {\r\n\tcount: number;\r\n}\r\ninterface IVesalResponse_UserInfo extends IVesalResponse_Base {\r\n\tuser: {\r\n\t\tcredit: number;\r\n\t\tdeliveryUrl: unknown;\r\n\t\treceiveUrl: unknown;\r\n\t\tiPs: string[];\r\n\t\tnumbers: string[];\r\n\t\tid: number;\r\n\t\tusername: string;\r\n\t\tpassword: unknown;\r\n\t\tfirstName: unknown;\r\n\t\tlastName: unknown;\r\n\t\tmobile: unknown;\r\n\t\temail: unknown;\r\n\t\tnationalId: number;\r\n\t\texpirationDate: string;\r\n\t\tactive: boolean;\r\n\t\tdeleted: boolean;\r\n\t\tdbProxyStandalone: boolean;\r\n\t\tinsertDate: unknown;\r\n\t\tupdateDate: unknown;\r\n\t\tcustomer: unknown;\r\n\t\troles: unknown;\r\n\t};\r\n}\r\n\r\nexport function GetStatusText(status: number) {\r\n\tif (status === 0) {\r\n\t\treturn \"success\";\r\n\t}\r\n\tif (Object.keys(vesalErrors).includes(\"\" + status)) {\r\n\t\treturn vesalErrors[status];\r\n\t}\r\n\treturn \"\";\r\n}\r\n\r\nexport const messageStates = {\r\n\t0: \"پیامک در صف ارسال قرار دارد\",\r\n\t1: \"ارسال شده\",\r\n\t2: \"پیامک به موبایل گیرنده تحویل شده است\",\r\n\t3: \"پیامک به موبایل گیرنده تحویل نشده است\",\r\n\t4: \"وضعیت نامشخص\",\r\n\t5: \"پیامک توسط وب سرویس به شرکت ارمغان راه طلایی رسیده است\",\r\n\t6: \"پیام از سمت اپراتور لغو شده است\",\r\n\t7: \"پیام از سمت اپراتور منقضی شده است\",\r\n\t8: \"پیام از سمت اپراتور reject شده است\",\r\n} as const;\r\n\r\nexport const vesalErrors = {\r\n\t0: \"عملیات با موفقیت انجام شد\",\r\n\t[-100]: \"refrenceId مورد نظر یافت نشد\",\r\n\t[-101]: \"احراز هویت کاربر موفقیت آمیز نبود\",\r\n\t[-102]: \"نام کاربری یافت نشد\",\r\n\t[-103]: \"شماره originator اشتباه یا در بازه شماره های کاربر نیست\",\r\n\t[-104]: \"اعتبار کم است\",\r\n\t[-105]: \"فرمت درخواست اشتباه است\",\r\n\t[-106]: \"تعداد refrenceId ها بیش از 1000 عدد است\",\r\n\t[-107]: \"شماره گیرنده پیامک اشتباه است\",\r\n\t[-109]: \"تاریخ انقضای حساب کاربری فرارسیده است\",\r\n\t[-110]: \"درخواست از ip مجاز کاربر ارسال نشده است\",\r\n\t[-111]: \"شماره گیرنده در بلک لیست قرار دارد\",\r\n\t[-112]: \"حساب مشتری فعال نیست\",\r\n\t[-115]: \"فرمت UDH اشتباه است\",\r\n\t[-117]: \"مقدار mclass وارد شده اشتباه است\",\r\n\t[-118]: \"شماره پورت وارد شده صحیح نیست\",\r\n\t[-119]: \"کاربر به سرویس مورد نظر دسترسی ندارد\",\r\n\t[-120]: \"پیام ارسال شده دارای هیچ شماره معتبری نیست\",\r\n\t[-200]: \"خطای داخلی در پایگاه داده رخ داده است\",\r\n\t[-201]: \"خطای نامشخص داخل پایگاه داده\",\r\n\t[-137]: \"پیام نباید حاوی کلمات غیرمجاز می باشد\",\r\n} as const;\r\n\r\nexport default Vesal;\r\n"],"names":["Vesal","username","password","from","this","apiUrl","_proto","prototype","Api","urlSuffix","method","data","response","responseBody","_temp4","_result","_temp2","_result2","references","VesalError","undefined","_temp","_catch","Promise","resolve","json","then","_response$json","_exit","_this","_extends","_temp3","fetch","headers","Accept","body","JSON","stringify","_fetch","e","reject","Send","_ref","recipients","messages","isManyToMany","Array","isArray","length","fill","sendData","destinations","originators","originator","contents","content","result","_result$references","successCount","failCount","map","messageResult","count","success","fail","GetMessageStatus","referencesIds","referenceids","GetReceivedMessages","GetReceivedMessagesCount","GetUserInfo","_Error","statusParam","messageParam","_this7","message","status","Object","keys","vesalErrors","includes","GetStatusText","call","_wrapNativeSuper","Error","_vesalErrors"],"mappings":"y5CAOa,IAAAA,eAAK,WAMjB,SAAAA,EAAYC,EAAkBC,EAAkBC,GAK/C,OAL2DC,KAL5CC,OAAS,sCACjBJ,KAAAA,qBACAC,cAAQ,EAAAE,KACRD,UAAI,EAGXC,KAAKH,SAAWA,GAAY,GAC5BG,KAAKF,SAAWA,EACh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CAA+BA,GACrC,KAAM,uCAAsCA,GAC5C,KAAM,6CAA4CA,GAClD,KAAM,wCAAuCA,GAC7C,KAAM,+BAA8BA,GACpC,KAAM,wCAAuCA,wGAjCnB,CAC5B,EAAG,8BACH,EAAG,YACH,EAAG,uCACH,EAAG,wCACH,EAAG,eACH,EAAG,yDACH,EAAG,kCACH,EAAG,oCACH,EAAG"}
|
package/dist/vesal.d.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Vesal API Class
|
|
3
|
+
* @author Shahab Movahhedi
|
|
4
|
+
* @see {@link https://shmovahhedi.com Shahab Movahhedi's Website}
|
|
5
|
+
* @see {@link https://github.com/movahhedi/vesal vesal's Repository}
|
|
6
|
+
* @license MIT
|
|
7
|
+
*/
|
|
8
|
+
export declare class Vesal {
|
|
9
|
+
readonly apiUrl = "http://vesal.armaghan.net:8080/rest";
|
|
10
|
+
private username;
|
|
11
|
+
private password;
|
|
12
|
+
private from;
|
|
13
|
+
constructor(username: string, password: string, from: string);
|
|
14
|
+
/**
|
|
15
|
+
* Make an API request to the Vesal API
|
|
16
|
+
* @private
|
|
17
|
+
* @param {string} urlSuffix - The URL suffix for the API endpoint
|
|
18
|
+
* @param {("GET"|"POST")} [method="GET"] - The HTTP method to use for the request
|
|
19
|
+
* @param {object|null} [data=null] - The data to send with the request
|
|
20
|
+
* @returns {Promise} The response from the API
|
|
21
|
+
*/
|
|
22
|
+
private Api;
|
|
23
|
+
Send({ recipients, messages, from, }: {
|
|
24
|
+
recipients: string | string[];
|
|
25
|
+
messages: string | string[];
|
|
26
|
+
from?: string | string[];
|
|
27
|
+
}): Promise<IVesalResponse_Send_WithCount>;
|
|
28
|
+
GetMessageStatus(referencesIds: number[]): Promise<IVesalResponse_MessageState>;
|
|
29
|
+
GetReceivedMessages(): Promise<IVesalResponse_ReceivedMessages>;
|
|
30
|
+
GetReceivedMessagesCount(): Promise<IVesalResponse_ReceivedMessagesCount>;
|
|
31
|
+
GetUserInfo(): Promise<IVesalResponse_UserInfo>;
|
|
32
|
+
}
|
|
33
|
+
export declare class VesalError extends Error {
|
|
34
|
+
status: keyof typeof vesalErrors | undefined;
|
|
35
|
+
constructor(statusParam: number | undefined, messageParam?: string);
|
|
36
|
+
}
|
|
37
|
+
interface IVesalResponse_Base {
|
|
38
|
+
errorModel: {
|
|
39
|
+
errorCode: number;
|
|
40
|
+
timestamp: string | number | null;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
interface IVesalResponse_Send extends IVesalResponse_Base {
|
|
44
|
+
references: (number | Omit<keyof typeof vesalErrors, 0>)[];
|
|
45
|
+
}
|
|
46
|
+
interface IVesalResponse_Send_WithCount extends IVesalResponse_Send {
|
|
47
|
+
count: {
|
|
48
|
+
success: number;
|
|
49
|
+
fail: number;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
interface IVesalResponse_MessageState extends IVesalResponse_Base {
|
|
53
|
+
states: {
|
|
54
|
+
id: number;
|
|
55
|
+
state: number;
|
|
56
|
+
}[];
|
|
57
|
+
}
|
|
58
|
+
interface IVesalResponse_ReceivedMessages extends IVesalResponse_Base {
|
|
59
|
+
messageModels: {
|
|
60
|
+
originator: string;
|
|
61
|
+
destination: string;
|
|
62
|
+
content: string;
|
|
63
|
+
}[];
|
|
64
|
+
}
|
|
65
|
+
interface IVesalResponse_ReceivedMessagesCount extends IVesalResponse_Base {
|
|
66
|
+
count: number;
|
|
67
|
+
}
|
|
68
|
+
interface IVesalResponse_UserInfo extends IVesalResponse_Base {
|
|
69
|
+
user: {
|
|
70
|
+
credit: number;
|
|
71
|
+
deliveryUrl: unknown;
|
|
72
|
+
receiveUrl: unknown;
|
|
73
|
+
iPs: string[];
|
|
74
|
+
numbers: string[];
|
|
75
|
+
id: number;
|
|
76
|
+
username: string;
|
|
77
|
+
password: unknown;
|
|
78
|
+
firstName: unknown;
|
|
79
|
+
lastName: unknown;
|
|
80
|
+
mobile: unknown;
|
|
81
|
+
email: unknown;
|
|
82
|
+
nationalId: number;
|
|
83
|
+
expirationDate: string;
|
|
84
|
+
active: boolean;
|
|
85
|
+
deleted: boolean;
|
|
86
|
+
dbProxyStandalone: boolean;
|
|
87
|
+
insertDate: unknown;
|
|
88
|
+
updateDate: unknown;
|
|
89
|
+
customer: unknown;
|
|
90
|
+
roles: unknown;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export declare function GetStatusText(status: number): any;
|
|
94
|
+
export declare const messageStates: {
|
|
95
|
+
readonly 0: "پیامک در صف ارسال قرار دارد";
|
|
96
|
+
readonly 1: "ارسال شده";
|
|
97
|
+
readonly 2: "پیامک به موبایل گیرنده تحویل شده است";
|
|
98
|
+
readonly 3: "پیامک به موبایل گیرنده تحویل نشده است";
|
|
99
|
+
readonly 4: "وضعیت نامشخص";
|
|
100
|
+
readonly 5: "پیامک توسط وب سرویس به شرکت ارمغان راه طلایی رسیده است";
|
|
101
|
+
readonly 6: "پیام از سمت اپراتور لغو شده است";
|
|
102
|
+
readonly 7: "پیام از سمت اپراتور منقضی شده است";
|
|
103
|
+
readonly 8: "پیام از سمت اپراتور reject شده است";
|
|
104
|
+
};
|
|
105
|
+
export declare const vesalErrors: {
|
|
106
|
+
readonly 0: "عملیات با موفقیت انجام شد";
|
|
107
|
+
readonly [-100]: "refrenceId مورد نظر یافت نشد";
|
|
108
|
+
readonly [-101]: "احراز هویت کاربر موفقیت آمیز نبود";
|
|
109
|
+
readonly [-102]: "نام کاربری یافت نشد";
|
|
110
|
+
readonly [-103]: "شماره originator اشتباه یا در بازه شماره های کاربر نیست";
|
|
111
|
+
readonly [-104]: "اعتبار کم است";
|
|
112
|
+
readonly [-105]: "فرمت درخواست اشتباه است";
|
|
113
|
+
readonly [-106]: "تعداد refrenceId ها بیش از 1000 عدد است";
|
|
114
|
+
readonly [-107]: "شماره گیرنده پیامک اشتباه است";
|
|
115
|
+
readonly [-109]: "تاریخ انقضای حساب کاربری فرارسیده است";
|
|
116
|
+
readonly [-110]: "درخواست از ip مجاز کاربر ارسال نشده است";
|
|
117
|
+
readonly [-111]: "شماره گیرنده در بلک لیست قرار دارد";
|
|
118
|
+
readonly [-112]: "حساب مشتری فعال نیست";
|
|
119
|
+
readonly [-115]: "فرمت UDH اشتباه است";
|
|
120
|
+
readonly [-117]: "مقدار mclass وارد شده اشتباه است";
|
|
121
|
+
readonly [-118]: "شماره پورت وارد شده صحیح نیست";
|
|
122
|
+
readonly [-119]: "کاربر به سرویس مورد نظر دسترسی ندارد";
|
|
123
|
+
readonly [-120]: "پیام ارسال شده دارای هیچ شماره معتبری نیست";
|
|
124
|
+
readonly [-200]: "خطای داخلی در پایگاه داده رخ داده است";
|
|
125
|
+
readonly [-201]: "خطای نامشخص داخل پایگاه داده";
|
|
126
|
+
readonly [-137]: "پیام نباید حاوی کلمات غیرمجاز می باشد";
|
|
127
|
+
};
|
|
128
|
+
export default Vesal;
|
package/dist/vesal.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function e(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(e=function(){return!!t})()}function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},t.apply(this,arguments)}function r(e){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},r(e)}function n(e,t){return n=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},n(e,t)}function o(t){var i="function"==typeof Map?new Map:void 0;return o=function(t){if(null===t||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==i){if(i.has(t))return i.get(t);i.set(t,o)}function o(){return function(t,r,o){if(e())return Reflect.construct.apply(null,arguments);var i=[null];i.push.apply(i,r);var s=new(t.bind.apply(t,i));return o&&n(s,o.prototype),s}(t,arguments,r(this).constructor)}return o.prototype=Object.create(t.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),n(o,t)},o(t)}var i;function s(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}var c=/*#__PURE__*/function(){function e(e,t,r){return this.apiUrl="http://vesal.armaghan.net:8080/rest",this.username=void 0,this.password=void 0,this.from=void 0,this.username=e||"",this.password=t,this.from=r,this}var r=e.prototype;return r.Api=function(e,r,n){void 0===r&&(r="POST");try{var o,i,c=function(e){function t(e){if(!i.references)throw new a(void 0,"The server didn't respond correctly");return i}var r=s(function(){return Promise.resolve(o.json()).then(function(e){i=e})},function(){throw new a(void 0,"The server didn't respond correctly")});return r&&r.then?r.then(t):t()},u=this;n=t({},n,{username:u.username,password:u.password});var f=s(function(){return Promise.resolve(fetch(u.apiUrl+"/"+e,{headers:{Accept:"application/json","Content-Type":"application/json"},method:r,body:n&&JSON.stringify(n)})).then(function(e){o=e})},function(){throw new a(void 0,"Server connection failed")});return Promise.resolve(f&&f.then?f.then(c):c())}catch(e){return Promise.reject(e)}},r.Send=function(e){var r=e.recipients,n=e.messages,o=e.from;try{if(!r||!n)throw new a(void 0,"recipients and messages should not be empty");"string"==typeof r&&(r=[r]),o||(o=this.from);var i=Array.isArray(n)||Array.isArray(o);if(i){if(!r.length)throw new a(void 0,"recipients and messages should have the same length");if(Array.isArray(n)&&!n.length)throw new a(void 0,"recipients and messages should have the same length");if("string"==typeof n&&(n=Array(r.length).fill(n)),"string"==typeof o&&(o=Array(r.length).fill(o)),r.length!==n.length||r.length!==o.length)throw new a(void 0,"recipients and messages should have the same length")}var s=t({destinations:r},i?{originators:o}:{originator:o},i?{contents:n}:{content:n});return Promise.resolve(this.Api(i?"ManyToMany":"OneToMany","POST",s)).then(function(e){var r,n=0,o=0;return null==(r=e.references)||r.map(function(e){e?n++:o++}),t({},e,{references:e.references||[],count:{success:n,fail:o}})})}catch(e){return Promise.reject(e)}},r.GetMessageStatus=function(e){try{return Promise.resolve(this.Api("messageState","POST",{referenceids:e}))}catch(e){return Promise.reject(e)}},r.GetReceivedMessages=function(){try{return Promise.resolve(this.Api("pullReceivedMessages"))}catch(e){return Promise.reject(e)}},r.GetReceivedMessagesCount=function(){try{return Promise.resolve(this.Api("receivedMessageCount"))}catch(e){return Promise.reject(e)}},r.GetUserInfo=function(){try{return Promise.resolve(this.Api("userInfo"))}catch(e){return Promise.reject(e)}},e}(),a=/*#__PURE__*/function(e){var t,r;function o(t,r){var n,o,i;return t&&Object.keys(h).includes(""+t)?(o=u(t),i=t):(o=r||"Unknown Vesal error",i=void 0),(n=e.call(this,o)||this).status=void 0,n.message=o,n.status=i,n}return r=e,(t=o).prototype=Object.create(r.prototype),t.prototype.constructor=t,n(t,r),o}(/*#__PURE__*/o(Error));function u(e){return 0===e?"success":Object.keys(h).includes(""+e)?h[e]:""}var f={0:"پیامک در صف ارسال قرار دارد",1:"ارسال شده",2:"پیامک به موبایل گیرنده تحویل شده است",3:"پیامک به موبایل گیرنده تحویل نشده است",4:"وضعیت نامشخص",5:"پیامک توسط وب سرویس به شرکت ارمغان راه طلایی رسیده است",6:"پیام از سمت اپراتور لغو شده است",7:"پیام از سمت اپراتور منقضی شده است",8:"پیام از سمت اپراتور reject شده است"},h=((i={0:"عملیات با موفقیت انجام شد"})[-100]="refrenceId مورد نظر یافت نشد",i[-101]="احراز هویت کاربر موفقیت آمیز نبود",i[-102]="نام کاربری یافت نشد",i[-103]="شماره originator اشتباه یا در بازه شماره های کاربر نیست",i[-104]="اعتبار کم است",i[-105]="فرمت درخواست اشتباه است",i[-106]="تعداد refrenceId ها بیش از 1000 عدد است",i[-107]="شماره گیرنده پیامک اشتباه است",i[-109]="تاریخ انقضای حساب کاربری فرارسیده است",i[-110]="درخواست از ip مجاز کاربر ارسال نشده است",i[-111]="شماره گیرنده در بلک لیست قرار دارد",i[-112]="حساب مشتری فعال نیست",i[-115]="فرمت UDH اشتباه است",i[-117]="مقدار mclass وارد شده اشتباه است",i[-118]="شماره پورت وارد شده صحیح نیست",i[-119]="کاربر به سرویس مورد نظر دسترسی ندارد",i[-120]="پیام ارسال شده دارای هیچ شماره معتبری نیست",i[-200]="خطای داخلی در پایگاه داده رخ داده است",i[-201]="خطای نامشخص داخل پایگاه داده",i[-137]="پیام نباید حاوی کلمات غیرمجاز می باشد",i);export{u as GetStatusText,c as Vesal,a as VesalError,c as default,f as messageStates,h as vesalErrors};
|
|
2
|
+
//# sourceMappingURL=vesal.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vesal.mjs","sources":["../src/vesal.ts"],"sourcesContent":["/**\r\n * The Vesal API Class\r\n * @author Shahab Movahhedi\r\n * @see {@link https://shmovahhedi.com Shahab Movahhedi's Website}\r\n * @see {@link https://github.com/movahhedi/vesal vesal's Repository}\r\n * @license MIT\r\n */\r\nexport class Vesal {\r\n\tpublic readonly apiUrl = \"http://vesal.armaghan.net:8080/rest\";\r\n\tprivate username: string;\r\n\tprivate password: string;\r\n\tprivate from: string;\r\n\r\n\tconstructor(username: string, password: string, from: string) {\r\n\t\tthis.username = username || \"\";\r\n\t\tthis.password = password;\r\n\t\tthis.from = from;\r\n\r\n\t\treturn this;\r\n\t}\r\n\r\n\t/**\r\n\t * Make an API request to the Vesal API\r\n\t * @private\r\n\t * @param {string} urlSuffix - The URL suffix for the API endpoint\r\n\t * @param {(\"GET\"|\"POST\")} [method=\"GET\"] - The HTTP method to use for the request\r\n\t * @param {object|null} [data=null] - The data to send with the request\r\n\t * @returns {Promise} The response from the API\r\n\t */\r\n\tprivate async Api(\r\n\t\turlSuffix: string,\r\n\t\tmethod: \"GET\" | \"POST\" = \"POST\",\r\n\t\tdata?: object,\r\n\t): Promise<any> {\r\n\t\tlet response: Response, responseBody: any;\r\n\r\n\t\tdata = {\r\n\t\t\t...data,\r\n\t\t\tusername: this.username,\r\n\t\t\tpassword: this.password,\r\n\t\t};\r\n\r\n\t\ttry {\r\n\t\t\tresponse = await fetch(`${this.apiUrl}/${urlSuffix}`, {\r\n\t\t\t\theaders: {\r\n\t\t\t\t\tAccept: \"application/json\",\r\n\t\t\t\t\t\"Content-Type\": \"application/json\",\r\n\t\t\t\t},\r\n\t\t\t\tmethod,\r\n\t\t\t\tbody: data && JSON.stringify(data),\r\n\t\t\t});\r\n\t\t} catch (error) {\r\n\t\t\tthrow new VesalError(undefined, \"Server connection failed\");\r\n\t\t}\r\n\r\n\t\ttry {\r\n\t\t\tresponseBody = await response.json();\r\n\t\t} catch (error) {\r\n\t\t\tthrow new VesalError(undefined, \"The server didn't respond correctly\");\r\n\t\t}\r\n\r\n\t\tif (!responseBody.references) {\r\n\t\t\tthrow new VesalError(undefined, \"The server didn't respond correctly\");\r\n\t\t}\r\n\r\n\t\treturn responseBody;\r\n\t}\r\n\r\n\tasync Send({\r\n\t\trecipients,\r\n\t\tmessages,\r\n\t\tfrom,\r\n\t}: {\r\n\t\trecipients: string | string[];\r\n\t\tmessages: string | string[];\r\n\t\tfrom?: string | string[];\r\n\t}): Promise<IVesalResponse_Send_WithCount> {\r\n\t\tif (!recipients || !messages) {\r\n\t\t\tthrow new VesalError(\r\n\t\t\t\tundefined,\r\n\t\t\t\t\"recipients and messages should not be empty\",\r\n\t\t\t);\r\n\t\t}\r\n\r\n\t\tif (typeof recipients === \"string\") {\r\n\t\t\trecipients = [recipients];\r\n\t\t}\r\n\r\n\t\tif (!from) {\r\n\t\t\tfrom = this.from;\r\n\t\t}\r\n\r\n\t\tconst isManyToMany = Array.isArray(messages) || Array.isArray(from);\r\n\r\n\t\tif (isManyToMany) {\r\n\t\t\tif (!recipients.length) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\t\t\tif (Array.isArray(messages) && !messages.length) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\r\n\t\t\tif (typeof messages === \"string\") {\r\n\t\t\t\t// messages = recipients.map(() => messages);\r\n\t\t\t\tmessages = Array(recipients.length).fill(messages);\r\n\t\t\t}\r\n\t\t\tif (typeof from === \"string\") {\r\n\t\t\t\t// from = recipients.map(() => from);\r\n\t\t\t\tfrom = Array(recipients.length).fill(from);\r\n\t\t\t}\r\n\r\n\t\t\tif (\r\n\t\t\t\trecipients.length !== messages.length ||\r\n\t\t\t\trecipients.length !== from.length\r\n\t\t\t) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst sendData = {\r\n\t\t\tdestinations: recipients,\r\n\t\t\t...(isManyToMany ? { originators: from } : { originator: from }),\r\n\t\t\t...(isManyToMany ? { contents: messages } : { content: messages }),\r\n\t\t};\r\n\r\n\t\tconst result: IVesalResponse_Send = await this.Api(\r\n\t\t\tisManyToMany ? \"ManyToMany\" : \"OneToMany\",\r\n\t\t\t\"POST\",\r\n\t\t\tsendData,\r\n\t\t);\r\n\r\n\t\tlet successCount: number = 0,\r\n\t\t\tfailCount: number = 0;\r\n\r\n\t\tresult.references?.map((messageResult) => {\r\n\t\t\tif (messageResult) {\r\n\t\t\t\tsuccessCount++;\r\n\t\t\t} else {\r\n\t\t\t\tfailCount++;\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\treturn {\r\n\t\t\t...result,\r\n\t\t\treferences: result.references || [],\r\n\t\t\tcount: {\r\n\t\t\t\tsuccess: successCount,\r\n\t\t\t\tfail: failCount,\r\n\t\t\t},\r\n\t\t};\r\n\t}\r\n\r\n\tasync GetMessageStatus(\r\n\t\treferencesIds: number[],\r\n\t): Promise<IVesalResponse_MessageState> {\r\n\t\treturn await this.Api(\"messageState\", \"POST\", {\r\n\t\t\treferenceids: referencesIds,\r\n\t\t});\r\n\t}\r\n\r\n\tasync GetReceivedMessages(): Promise<IVesalResponse_ReceivedMessages> {\r\n\t\treturn await this.Api(\"pullReceivedMessages\");\r\n\t}\r\n\r\n\tasync GetReceivedMessagesCount(): Promise<IVesalResponse_ReceivedMessagesCount> {\r\n\t\treturn await this.Api(\"receivedMessageCount\");\r\n\t}\r\n\r\n\tasync GetUserInfo(): Promise<IVesalResponse_UserInfo> {\r\n\t\treturn await this.Api(\"userInfo\");\r\n\t}\r\n}\r\n\r\nexport class VesalError extends Error {\r\n\tstatus: keyof typeof vesalErrors | undefined;\r\n\r\n\tconstructor(statusParam: number | undefined, messageParam?: string) {\r\n\t\tlet message: string, status: keyof typeof vesalErrors | undefined;\r\n\r\n\t\tif (statusParam && Object.keys(vesalErrors).includes(\"\" + statusParam)) {\r\n\t\t\tmessage = GetStatusText(statusParam);\r\n\t\t\tstatus = statusParam as any;\r\n\t\t} else {\r\n\t\t\tmessage = messageParam || \"Unknown Vesal error\";\r\n\t\t\tstatus = undefined;\r\n\t\t}\r\n\r\n\t\tsuper(message);\r\n\r\n\t\tthis.message = message;\r\n\t\tthis.status = status;\r\n\t}\r\n}\r\n\r\ninterface IVesalResponse_Base {\r\n\terrorModel: {\r\n\t\terrorCode: number;\r\n\t\ttimestamp: string | number | null;\r\n\t};\r\n}\r\n\r\ninterface IVesalResponse_Send extends IVesalResponse_Base {\r\n\treferences: (number | Omit<keyof typeof vesalErrors, 0>)[];\r\n}\r\n\r\ninterface IVesalResponse_Send_WithCount extends IVesalResponse_Send {\r\n\tcount: {\r\n\t\tsuccess: number;\r\n\t\tfail: number;\r\n\t};\r\n}\r\n\r\ninterface IVesalResponse_MessageState extends IVesalResponse_Base {\r\n\tstates: {\r\n\t\tid: number;\r\n\t\tstate: number;\r\n\t}[];\r\n}\r\ninterface IVesalResponse_ReceivedMessages extends IVesalResponse_Base {\r\n\tmessageModels: {\r\n\t\toriginator: string;\r\n\t\tdestination: string;\r\n\t\tcontent: string;\r\n\t}[];\r\n}\r\ninterface IVesalResponse_ReceivedMessagesCount extends IVesalResponse_Base {\r\n\tcount: number;\r\n}\r\ninterface IVesalResponse_UserInfo extends IVesalResponse_Base {\r\n\tuser: {\r\n\t\tcredit: number;\r\n\t\tdeliveryUrl: unknown;\r\n\t\treceiveUrl: unknown;\r\n\t\tiPs: string[];\r\n\t\tnumbers: string[];\r\n\t\tid: number;\r\n\t\tusername: string;\r\n\t\tpassword: unknown;\r\n\t\tfirstName: unknown;\r\n\t\tlastName: unknown;\r\n\t\tmobile: unknown;\r\n\t\temail: unknown;\r\n\t\tnationalId: number;\r\n\t\texpirationDate: string;\r\n\t\tactive: boolean;\r\n\t\tdeleted: boolean;\r\n\t\tdbProxyStandalone: boolean;\r\n\t\tinsertDate: unknown;\r\n\t\tupdateDate: unknown;\r\n\t\tcustomer: unknown;\r\n\t\troles: unknown;\r\n\t};\r\n}\r\n\r\nexport function GetStatusText(status: number) {\r\n\tif (status === 0) {\r\n\t\treturn \"success\";\r\n\t}\r\n\tif (Object.keys(vesalErrors).includes(\"\" + status)) {\r\n\t\treturn vesalErrors[status];\r\n\t}\r\n\treturn \"\";\r\n}\r\n\r\nexport const messageStates = {\r\n\t0: \"پیامک در صف ارسال قرار دارد\",\r\n\t1: \"ارسال شده\",\r\n\t2: \"پیامک به موبایل گیرنده تحویل شده است\",\r\n\t3: \"پیامک به موبایل گیرنده تحویل نشده است\",\r\n\t4: \"وضعیت نامشخص\",\r\n\t5: \"پیامک توسط وب سرویس به شرکت ارمغان راه طلایی رسیده است\",\r\n\t6: \"پیام از سمت اپراتور لغو شده است\",\r\n\t7: \"پیام از سمت اپراتور منقضی شده است\",\r\n\t8: \"پیام از سمت اپراتور reject شده است\",\r\n} as const;\r\n\r\nexport const vesalErrors = {\r\n\t0: \"عملیات با موفقیت انجام شد\",\r\n\t[-100]: \"refrenceId مورد نظر یافت نشد\",\r\n\t[-101]: \"احراز هویت کاربر موفقیت آمیز نبود\",\r\n\t[-102]: \"نام کاربری یافت نشد\",\r\n\t[-103]: \"شماره originator اشتباه یا در بازه شماره های کاربر نیست\",\r\n\t[-104]: \"اعتبار کم است\",\r\n\t[-105]: \"فرمت درخواست اشتباه است\",\r\n\t[-106]: \"تعداد refrenceId ها بیش از 1000 عدد است\",\r\n\t[-107]: \"شماره گیرنده پیامک اشتباه است\",\r\n\t[-109]: \"تاریخ انقضای حساب کاربری فرارسیده است\",\r\n\t[-110]: \"درخواست از ip مجاز کاربر ارسال نشده است\",\r\n\t[-111]: \"شماره گیرنده در بلک لیست قرار دارد\",\r\n\t[-112]: \"حساب مشتری فعال نیست\",\r\n\t[-115]: \"فرمت UDH اشتباه است\",\r\n\t[-117]: \"مقدار mclass وارد شده اشتباه است\",\r\n\t[-118]: \"شماره پورت وارد شده صحیح نیست\",\r\n\t[-119]: \"کاربر به سرویس مورد نظر دسترسی ندارد\",\r\n\t[-120]: \"پیام ارسال شده دارای هیچ شماره معتبری نیست\",\r\n\t[-200]: \"خطای داخلی در پایگاه داده رخ داده است\",\r\n\t[-201]: \"خطای نامشخص داخل پایگاه داده\",\r\n\t[-137]: \"پیام نباید حاوی کلمات غیرمجاز می باشد\",\r\n} as const;\r\n\r\nexport default Vesal;\r\n"],"names":["Vesal","username","password","from","this","apiUrl","_proto","prototype","Api","urlSuffix","method","data","response","responseBody","_temp4","_result","_temp2","_result2","references","VesalError","undefined","_temp","_catch","Promise","resolve","json","then","_response$json","_exit","_this","_extends","_temp3","fetch","headers","Accept","body","JSON","stringify","_fetch","e","reject","Send","_ref","recipients","messages","isManyToMany","Array","isArray","length","fill","sendData","destinations","originators","originator","contents","content","result","_result$references","successCount","failCount","map","messageResult","count","success","fail","GetMessageStatus","referencesIds","referenceids","GetReceivedMessages","GetReceivedMessagesCount","GetUserInfo","_Error","statusParam","messageParam","_this7","message","status","Object","keys","vesalErrors","includes","GetStatusText","call","_wrapNativeSuper","Error","messageStates","_vesalErrors"],"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ACD,KAAM,+BAA8BA,GACpC,KAAM,oCAAmCA,GACzC,KAAM,sBAAqBA,GAC3B,KAAM,0DAAyDA,GAC/D,KAAM,gBAAeA,GACrB,KAAM,0BAAyBA,GAC/B,KAAM,0CAAyCA,GAC/C,KAAM,gCAA+BA,GACrC,KAAM,wCAAuCA,GAC7C,KAAM,0CAAyCA,GAC/C,KAAM,qCAAoCA,GAC1C,KAAM,uBAAsBA,GAC5B,KAAM,sBAAqBA,GAC3B,KAAM,mCAAkCA,GACxC,KAAM,gCAA+BA,GACrC,KAAM,uCAAsCA,GAC5C,KAAM,6CAA4CA,GAClD,KAAM,wCAAuCA,GAC7C,KAAM,+BAA8BA,GACpC,KAAM,wCAAuCA"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function e(){return e=Object.assign?Object.assign.bind():function(e){for(var s=1;s<arguments.length;s++){var t=arguments[s];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},e.apply(this,arguments)}class s{constructor(e,s,t){return this.apiUrl="http://vesal.armaghan.net:8080/rest",this.username=void 0,this.password=void 0,this.from=void 0,this.username=e||"",this.password=s,this.from=t,this}async Api(s,r="POST",n){let i,a;n=e({},n,{username:this.username,password:this.password});try{i=await fetch(`${this.apiUrl}/${s}`,{headers:{Accept:"application/json","Content-Type":"application/json"},method:r,body:n&&JSON.stringify(n)})}catch(e){throw new t(void 0,"Server connection failed")}try{a=await i.json()}catch(e){throw new t(void 0,"The server didn't respond correctly")}if(!a.references)throw new t(void 0,"The server didn't respond correctly");return a}async Send({recipients:s,messages:r,from:n}){var i;if(!s||!r)throw new t(void 0,"recipients and messages should not be empty");"string"==typeof s&&(s=[s]),n||(n=this.from);const a=Array.isArray(r)||Array.isArray(n);if(a){if(!s.length)throw new t(void 0,"recipients and messages should have the same length");if(Array.isArray(r)&&!r.length)throw new t(void 0,"recipients and messages should have the same length");if("string"==typeof r&&(r=Array(s.length).fill(r)),"string"==typeof n&&(n=Array(s.length).fill(n)),s.length!==r.length||s.length!==n.length)throw new t(void 0,"recipients and messages should have the same length")}const o=e({destinations:s},a?{originators:n}:{originator:n},a?{contents:r}:{content:r}),c=await this.Api(a?"ManyToMany":"OneToMany","POST",o);let h=0,d=0;return null==(i=c.references)||i.map(e=>{e?h++:d++}),e({},c,{references:c.references||[],count:{success:h,fail:d}})}async GetMessageStatus(e){return await this.Api("messageState","POST",{referenceids:e})}async GetReceivedMessages(){return await this.Api("pullReceivedMessages")}async GetReceivedMessagesCount(){return await this.Api("receivedMessageCount")}async GetUserInfo(){return await this.Api("userInfo")}}class t extends Error{constructor(e,s){let t,n;e&&Object.keys(i).includes(""+e)?(t=r(e),n=e):(t=s||"Unknown Vesal error",n=void 0),super(t),this.status=void 0,this.message=t,this.status=n}}function r(e){return 0===e?"success":Object.keys(i).includes(""+e)?i[e]:""}const n={0:"پیامک در صف ارسال قرار دارد",1:"ارسال شده",2:"پیامک به موبایل گیرنده تحویل شده است",3:"پیامک به موبایل گیرنده تحویل نشده است",4:"وضعیت نامشخص",5:"پیامک توسط وب سرویس به شرکت ارمغان راه طلایی رسیده است",6:"پیام از سمت اپراتور لغو شده است",7:"پیام از سمت اپراتور منقضی شده است",8:"پیام از سمت اپراتور reject شده است"},i={0:"عملیات با موفقیت انجام شد",[-100]:"refrenceId مورد نظر یافت نشد",[-101]:"احراز هویت کاربر موفقیت آمیز نبود",[-102]:"نام کاربری یافت نشد",[-103]:"شماره originator اشتباه یا در بازه شماره های کاربر نیست",[-104]:"اعتبار کم است",[-105]:"فرمت درخواست اشتباه است",[-106]:"تعداد refrenceId ها بیش از 1000 عدد است",[-107]:"شماره گیرنده پیامک اشتباه است",[-109]:"تاریخ انقضای حساب کاربری فرارسیده است",[-110]:"درخواست از ip مجاز کاربر ارسال نشده است",[-111]:"شماره گیرنده در بلک لیست قرار دارد",[-112]:"حساب مشتری فعال نیست",[-115]:"فرمت UDH اشتباه است",[-117]:"مقدار mclass وارد شده اشتباه است",[-118]:"شماره پورت وارد شده صحیح نیست",[-119]:"کاربر به سرویس مورد نظر دسترسی ندارد",[-120]:"پیام ارسال شده دارای هیچ شماره معتبری نیست",[-200]:"خطای داخلی در پایگاه داده رخ داده است",[-201]:"خطای نامشخص داخل پایگاه داده",[-137]:"پیام نباید حاوی کلمات غیرمجاز می باشد"};export{r as GetStatusText,s as Vesal,t as VesalError,s as default,n as messageStates,i as vesalErrors};
|
|
2
|
+
//# sourceMappingURL=vesal.modern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vesal.modern.js","sources":["../src/vesal.ts"],"sourcesContent":["/**\r\n * The Vesal API Class\r\n * @author Shahab Movahhedi\r\n * @see {@link https://shmovahhedi.com Shahab Movahhedi's Website}\r\n * @see {@link https://github.com/movahhedi/vesal vesal's Repository}\r\n * @license MIT\r\n */\r\nexport class Vesal {\r\n\tpublic readonly apiUrl = \"http://vesal.armaghan.net:8080/rest\";\r\n\tprivate username: string;\r\n\tprivate password: string;\r\n\tprivate from: string;\r\n\r\n\tconstructor(username: string, password: string, from: string) {\r\n\t\tthis.username = username || \"\";\r\n\t\tthis.password = password;\r\n\t\tthis.from = from;\r\n\r\n\t\treturn this;\r\n\t}\r\n\r\n\t/**\r\n\t * Make an API request to the Vesal API\r\n\t * @private\r\n\t * @param {string} urlSuffix - The URL suffix for the API endpoint\r\n\t * @param {(\"GET\"|\"POST\")} [method=\"GET\"] - The HTTP method to use for the request\r\n\t * @param {object|null} [data=null] - The data to send with the request\r\n\t * @returns {Promise} The response from the API\r\n\t */\r\n\tprivate async Api(\r\n\t\turlSuffix: string,\r\n\t\tmethod: \"GET\" | \"POST\" = \"POST\",\r\n\t\tdata?: object,\r\n\t): Promise<any> {\r\n\t\tlet response: Response, responseBody: any;\r\n\r\n\t\tdata = {\r\n\t\t\t...data,\r\n\t\t\tusername: this.username,\r\n\t\t\tpassword: this.password,\r\n\t\t};\r\n\r\n\t\ttry {\r\n\t\t\tresponse = await fetch(`${this.apiUrl}/${urlSuffix}`, {\r\n\t\t\t\theaders: {\r\n\t\t\t\t\tAccept: \"application/json\",\r\n\t\t\t\t\t\"Content-Type\": \"application/json\",\r\n\t\t\t\t},\r\n\t\t\t\tmethod,\r\n\t\t\t\tbody: data && JSON.stringify(data),\r\n\t\t\t});\r\n\t\t} catch (error) {\r\n\t\t\tthrow new VesalError(undefined, \"Server connection failed\");\r\n\t\t}\r\n\r\n\t\ttry {\r\n\t\t\tresponseBody = await response.json();\r\n\t\t} catch (error) {\r\n\t\t\tthrow new VesalError(undefined, \"The server didn't respond correctly\");\r\n\t\t}\r\n\r\n\t\tif (!responseBody.references) {\r\n\t\t\tthrow new VesalError(undefined, \"The server didn't respond correctly\");\r\n\t\t}\r\n\r\n\t\treturn responseBody;\r\n\t}\r\n\r\n\tasync Send({\r\n\t\trecipients,\r\n\t\tmessages,\r\n\t\tfrom,\r\n\t}: {\r\n\t\trecipients: string | string[];\r\n\t\tmessages: string | string[];\r\n\t\tfrom?: string | string[];\r\n\t}): Promise<IVesalResponse_Send_WithCount> {\r\n\t\tif (!recipients || !messages) {\r\n\t\t\tthrow new VesalError(\r\n\t\t\t\tundefined,\r\n\t\t\t\t\"recipients and messages should not be empty\",\r\n\t\t\t);\r\n\t\t}\r\n\r\n\t\tif (typeof recipients === \"string\") {\r\n\t\t\trecipients = [recipients];\r\n\t\t}\r\n\r\n\t\tif (!from) {\r\n\t\t\tfrom = this.from;\r\n\t\t}\r\n\r\n\t\tconst isManyToMany = Array.isArray(messages) || Array.isArray(from);\r\n\r\n\t\tif (isManyToMany) {\r\n\t\t\tif (!recipients.length) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\t\t\tif (Array.isArray(messages) && !messages.length) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\r\n\t\t\tif (typeof messages === \"string\") {\r\n\t\t\t\t// messages = recipients.map(() => messages);\r\n\t\t\t\tmessages = Array(recipients.length).fill(messages);\r\n\t\t\t}\r\n\t\t\tif (typeof from === \"string\") {\r\n\t\t\t\t// from = recipients.map(() => from);\r\n\t\t\t\tfrom = Array(recipients.length).fill(from);\r\n\t\t\t}\r\n\r\n\t\t\tif (\r\n\t\t\t\trecipients.length !== messages.length ||\r\n\t\t\t\trecipients.length !== from.length\r\n\t\t\t) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst sendData = {\r\n\t\t\tdestinations: recipients,\r\n\t\t\t...(isManyToMany ? { originators: from } : { originator: from }),\r\n\t\t\t...(isManyToMany ? { contents: messages } : { content: messages }),\r\n\t\t};\r\n\r\n\t\tconst result: IVesalResponse_Send = await this.Api(\r\n\t\t\tisManyToMany ? \"ManyToMany\" : \"OneToMany\",\r\n\t\t\t\"POST\",\r\n\t\t\tsendData,\r\n\t\t);\r\n\r\n\t\tlet successCount: number = 0,\r\n\t\t\tfailCount: number = 0;\r\n\r\n\t\tresult.references?.map((messageResult) => {\r\n\t\t\tif (messageResult) {\r\n\t\t\t\tsuccessCount++;\r\n\t\t\t} else {\r\n\t\t\t\tfailCount++;\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\treturn {\r\n\t\t\t...result,\r\n\t\t\treferences: result.references || [],\r\n\t\t\tcount: {\r\n\t\t\t\tsuccess: successCount,\r\n\t\t\t\tfail: failCount,\r\n\t\t\t},\r\n\t\t};\r\n\t}\r\n\r\n\tasync GetMessageStatus(\r\n\t\treferencesIds: number[],\r\n\t): Promise<IVesalResponse_MessageState> {\r\n\t\treturn await this.Api(\"messageState\", \"POST\", {\r\n\t\t\treferenceids: referencesIds,\r\n\t\t});\r\n\t}\r\n\r\n\tasync GetReceivedMessages(): Promise<IVesalResponse_ReceivedMessages> {\r\n\t\treturn await this.Api(\"pullReceivedMessages\");\r\n\t}\r\n\r\n\tasync GetReceivedMessagesCount(): Promise<IVesalResponse_ReceivedMessagesCount> {\r\n\t\treturn await this.Api(\"receivedMessageCount\");\r\n\t}\r\n\r\n\tasync GetUserInfo(): Promise<IVesalResponse_UserInfo> {\r\n\t\treturn await this.Api(\"userInfo\");\r\n\t}\r\n}\r\n\r\nexport class VesalError extends Error {\r\n\tstatus: keyof typeof vesalErrors | undefined;\r\n\r\n\tconstructor(statusParam: number | undefined, messageParam?: string) {\r\n\t\tlet message: string, status: keyof typeof vesalErrors | undefined;\r\n\r\n\t\tif (statusParam && Object.keys(vesalErrors).includes(\"\" + statusParam)) {\r\n\t\t\tmessage = GetStatusText(statusParam);\r\n\t\t\tstatus = statusParam as any;\r\n\t\t} else {\r\n\t\t\tmessage = messageParam || \"Unknown Vesal error\";\r\n\t\t\tstatus = undefined;\r\n\t\t}\r\n\r\n\t\tsuper(message);\r\n\r\n\t\tthis.message = message;\r\n\t\tthis.status = status;\r\n\t}\r\n}\r\n\r\ninterface IVesalResponse_Base {\r\n\terrorModel: {\r\n\t\terrorCode: number;\r\n\t\ttimestamp: string | number | null;\r\n\t};\r\n}\r\n\r\ninterface IVesalResponse_Send extends IVesalResponse_Base {\r\n\treferences: (number | Omit<keyof typeof vesalErrors, 0>)[];\r\n}\r\n\r\ninterface IVesalResponse_Send_WithCount extends IVesalResponse_Send {\r\n\tcount: {\r\n\t\tsuccess: number;\r\n\t\tfail: number;\r\n\t};\r\n}\r\n\r\ninterface IVesalResponse_MessageState extends IVesalResponse_Base {\r\n\tstates: {\r\n\t\tid: number;\r\n\t\tstate: number;\r\n\t}[];\r\n}\r\ninterface IVesalResponse_ReceivedMessages extends IVesalResponse_Base {\r\n\tmessageModels: {\r\n\t\toriginator: string;\r\n\t\tdestination: string;\r\n\t\tcontent: string;\r\n\t}[];\r\n}\r\ninterface IVesalResponse_ReceivedMessagesCount extends IVesalResponse_Base {\r\n\tcount: number;\r\n}\r\ninterface IVesalResponse_UserInfo extends IVesalResponse_Base {\r\n\tuser: {\r\n\t\tcredit: number;\r\n\t\tdeliveryUrl: unknown;\r\n\t\treceiveUrl: unknown;\r\n\t\tiPs: string[];\r\n\t\tnumbers: string[];\r\n\t\tid: number;\r\n\t\tusername: string;\r\n\t\tpassword: unknown;\r\n\t\tfirstName: unknown;\r\n\t\tlastName: unknown;\r\n\t\tmobile: unknown;\r\n\t\temail: unknown;\r\n\t\tnationalId: number;\r\n\t\texpirationDate: string;\r\n\t\tactive: boolean;\r\n\t\tdeleted: boolean;\r\n\t\tdbProxyStandalone: boolean;\r\n\t\tinsertDate: unknown;\r\n\t\tupdateDate: unknown;\r\n\t\tcustomer: unknown;\r\n\t\troles: unknown;\r\n\t};\r\n}\r\n\r\nexport function GetStatusText(status: number) {\r\n\tif (status === 0) {\r\n\t\treturn \"success\";\r\n\t}\r\n\tif (Object.keys(vesalErrors).includes(\"\" + status)) {\r\n\t\treturn vesalErrors[status];\r\n\t}\r\n\treturn \"\";\r\n}\r\n\r\nexport const messageStates = {\r\n\t0: \"پیامک در صف ارسال قرار دارد\",\r\n\t1: \"ارسال شده\",\r\n\t2: \"پیامک به موبایل گیرنده تحویل شده است\",\r\n\t3: \"پیامک به موبایل گیرنده تحویل نشده است\",\r\n\t4: \"وضعیت نامشخص\",\r\n\t5: \"پیامک توسط وب سرویس به شرکت ارمغان راه طلایی رسیده است\",\r\n\t6: \"پیام از سمت اپراتور لغو شده است\",\r\n\t7: \"پیام از سمت اپراتور منقضی شده است\",\r\n\t8: \"پیام از سمت اپراتور reject شده است\",\r\n} as const;\r\n\r\nexport const vesalErrors = {\r\n\t0: \"عملیات با موفقیت انجام شد\",\r\n\t[-100]: \"refrenceId مورد نظر یافت نشد\",\r\n\t[-101]: \"احراز هویت کاربر موفقیت آمیز نبود\",\r\n\t[-102]: \"نام کاربری یافت نشد\",\r\n\t[-103]: \"شماره originator اشتباه یا در بازه شماره های کاربر نیست\",\r\n\t[-104]: \"اعتبار کم است\",\r\n\t[-105]: \"فرمت درخواست اشتباه است\",\r\n\t[-106]: \"تعداد refrenceId ها بیش از 1000 عدد است\",\r\n\t[-107]: \"شماره گیرنده پیامک اشتباه است\",\r\n\t[-109]: \"تاریخ انقضای حساب کاربری فرارسیده است\",\r\n\t[-110]: \"درخواست از ip مجاز کاربر ارسال نشده است\",\r\n\t[-111]: \"شماره گیرنده در بلک لیست قرار دارد\",\r\n\t[-112]: \"حساب مشتری فعال نیست\",\r\n\t[-115]: \"فرمت UDH اشتباه است\",\r\n\t[-117]: \"مقدار mclass وارد شده اشتباه است\",\r\n\t[-118]: \"شماره پورت وارد شده صحیح نیست\",\r\n\t[-119]: \"کاربر به سرویس مورد نظر دسترسی ندارد\",\r\n\t[-120]: \"پیام ارسال شده دارای هیچ شماره معتبری نیست\",\r\n\t[-200]: \"خطای داخلی در پایگاه داده رخ داده است\",\r\n\t[-201]: \"خطای نامشخص داخل پایگاه داده\",\r\n\t[-137]: \"پیام نباید حاوی کلمات غیرمجاز می باشد\",\r\n} as const;\r\n\r\nexport default Vesal;\r\n"],"names":["Vesal","constructor","username","password","from","apiUrl","this","Api","urlSuffix","method","data","response","responseBody","_extends","fetch","headers","Accept","body","JSON","stringify","error","VesalError","undefined","json","references","Send","recipients","messages","_result$references","isManyToMany","Array","isArray","length","fill","sendData","destinations","originators","originator","contents","content","result","successCount","failCount","map","messageResult","count","success","fail","GetMessageStatus","referencesIds","referenceids","GetReceivedMessages","GetReceivedMessagesCount","GetUserInfo","Error","statusParam","messageParam","message","status","Object","keys","vesalErrors","includes","GetStatusText","super","messageStates"],"mappings":"0OAOaA,EAMZC,WAAAA,CAAYC,EAAkBC,EAAkBC,GAK/C,OAVeC,KAAAA,OAAS,sCACjBH,KAAAA,qBACAC,cAAQ,EAAAG,KACRF,UAAI,EAGXE,KAAKJ,SAAWA,GAAY,GAC5BI,KAAKH,SAAWA,EA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E,KAAM,0BACR,EAAE,KAAM,0CACR,EAAE,KAAM,gCACR,EAAE,KAAM,wCACR,EAAE,KAAM,0CACR,EAAE,KAAM,qCACR,EAAE,KAAM,uBACR,EAAE,KAAM,sBACR,EAAE,KAAM,mCACR,EAAE,KAAM,gCACR,EAAE,KAAM,uCACR,EAAE,KAAM,6CACR,EAAE,KAAM,wCACR,EAAE,KAAM,+BACR,EAAE,KAAM"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).vesal={})}(this,function(e){function t(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(t=function(){return!!e})()}function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},r.apply(this,arguments)}function n(e){return n=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},n(e)}function o(e,t){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},o(e,t)}function i(e){var r="function"==typeof Map?new Map:void 0;return i=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(e))return r.get(e);r.set(e,i)}function i(){return function(e,r,n){if(t())return Reflect.construct.apply(null,arguments);var i=[null];i.push.apply(i,r);var s=new(e.bind.apply(e,i));return n&&o(s,n.prototype),s}(e,arguments,n(this).constructor)}return i.prototype=Object.create(e.prototype,{constructor:{value:i,enumerable:!1,writable:!0,configurable:!0}}),o(i,e)},i(e)}var s;function c(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}var a=/*#__PURE__*/function(){function e(e,t,r){return this.apiUrl="http://vesal.armaghan.net:8080/rest",this.username=void 0,this.password=void 0,this.from=void 0,this.username=e||"",this.password=t,this.from=r,this}var t=e.prototype;return t.Api=function(e,t,n){void 0===t&&(t="POST");try{var o,i,s=function(e){function t(e){if(!i.references)throw new u(void 0,"The server didn't respond correctly");return i}var r=c(function(){return Promise.resolve(o.json()).then(function(e){i=e})},function(){throw new u(void 0,"The server didn't respond correctly")});return r&&r.then?r.then(t):t()},a=this;n=r({},n,{username:a.username,password:a.password});var f=c(function(){return Promise.resolve(fetch(a.apiUrl+"/"+e,{headers:{Accept:"application/json","Content-Type":"application/json"},method:t,body:n&&JSON.stringify(n)})).then(function(e){o=e})},function(){throw new u(void 0,"Server connection failed")});return Promise.resolve(f&&f.then?f.then(s):s())}catch(e){return Promise.reject(e)}},t.Send=function(e){var t=e.recipients,n=e.messages,o=e.from;try{if(!t||!n)throw new u(void 0,"recipients and messages should not be empty");"string"==typeof t&&(t=[t]),o||(o=this.from);var i=Array.isArray(n)||Array.isArray(o);if(i){if(!t.length)throw new u(void 0,"recipients and messages should have the same length");if(Array.isArray(n)&&!n.length)throw new u(void 0,"recipients and messages should have the same length");if("string"==typeof n&&(n=Array(t.length).fill(n)),"string"==typeof o&&(o=Array(t.length).fill(o)),t.length!==n.length||t.length!==o.length)throw new u(void 0,"recipients and messages should have the same length")}var s=r({destinations:t},i?{originators:o}:{originator:o},i?{contents:n}:{content:n});return Promise.resolve(this.Api(i?"ManyToMany":"OneToMany","POST",s)).then(function(e){var t,n=0,o=0;return null==(t=e.references)||t.map(function(e){e?n++:o++}),r({},e,{references:e.references||[],count:{success:n,fail:o}})})}catch(e){return Promise.reject(e)}},t.GetMessageStatus=function(e){try{return Promise.resolve(this.Api("messageState","POST",{referenceids:e}))}catch(e){return Promise.reject(e)}},t.GetReceivedMessages=function(){try{return Promise.resolve(this.Api("pullReceivedMessages"))}catch(e){return Promise.reject(e)}},t.GetReceivedMessagesCount=function(){try{return Promise.resolve(this.Api("receivedMessageCount"))}catch(e){return Promise.reject(e)}},t.GetUserInfo=function(){try{return Promise.resolve(this.Api("userInfo"))}catch(e){return Promise.reject(e)}},e}(),u=/*#__PURE__*/function(e){var t,r;function n(t,r){var n,o,i;return t&&Object.keys(l).includes(""+t)?(o=f(t),i=t):(o=r||"Unknown Vesal error",i=void 0),(n=e.call(this,o)||this).status=void 0,n.message=o,n.status=i,n}return r=e,(t=n).prototype=Object.create(r.prototype),t.prototype.constructor=t,o(t,r),n}(/*#__PURE__*/i(Error));function f(e){return 0===e?"success":Object.keys(l).includes(""+e)?l[e]:""}var l=((s={0:"عملیات با موفقیت انجام شد"})[-100]="refrenceId مورد نظر یافت نشد",s[-101]="احراز هویت کاربر موفقیت آمیز نبود",s[-102]="نام کاربری یافت نشد",s[-103]="شماره originator اشتباه یا در بازه شماره های کاربر نیست",s[-104]="اعتبار کم است",s[-105]="فرمت درخواست اشتباه است",s[-106]="تعداد refrenceId ها بیش از 1000 عدد است",s[-107]="شماره گیرنده پیامک اشتباه است",s[-109]="تاریخ انقضای حساب کاربری فرارسیده است",s[-110]="درخواست از ip مجاز کاربر ارسال نشده است",s[-111]="شماره گیرنده در بلک لیست قرار دارد",s[-112]="حساب مشتری فعال نیست",s[-115]="فرمت UDH اشتباه است",s[-117]="مقدار mclass وارد شده اشتباه است",s[-118]="شماره پورت وارد شده صحیح نیست",s[-119]="کاربر به سرویس مورد نظر دسترسی ندارد",s[-120]="پیام ارسال شده دارای هیچ شماره معتبری نیست",s[-200]="خطای داخلی در پایگاه داده رخ داده است",s[-201]="خطای نامشخص داخل پایگاه داده",s[-137]="پیام نباید حاوی کلمات غیرمجاز می باشد",s);e.GetStatusText=f,e.Vesal=a,e.VesalError=u,e.default=a,e.messageStates={0:"پیامک در صف ارسال قرار دارد",1:"ارسال شده",2:"پیامک به موبایل گیرنده تحویل شده است",3:"پیامک به موبایل گیرنده تحویل نشده است",4:"وضعیت نامشخص",5:"پیامک توسط وب سرویس به شرکت ارمغان راه طلایی رسیده است",6:"پیام از سمت اپراتور لغو شده است",7:"پیام از سمت اپراتور منقضی شده است",8:"پیام از سمت اپراتور reject شده است"},e.vesalErrors=l});
|
|
2
|
+
//# sourceMappingURL=vesal.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vesal.umd.js","sources":["../src/vesal.ts"],"sourcesContent":["/**\r\n * The Vesal API Class\r\n * @author Shahab Movahhedi\r\n * @see {@link https://shmovahhedi.com Shahab Movahhedi's Website}\r\n * @see {@link https://github.com/movahhedi/vesal vesal's Repository}\r\n * @license MIT\r\n */\r\nexport class Vesal {\r\n\tpublic readonly apiUrl = \"http://vesal.armaghan.net:8080/rest\";\r\n\tprivate username: string;\r\n\tprivate password: string;\r\n\tprivate from: string;\r\n\r\n\tconstructor(username: string, password: string, from: string) {\r\n\t\tthis.username = username || \"\";\r\n\t\tthis.password = password;\r\n\t\tthis.from = from;\r\n\r\n\t\treturn this;\r\n\t}\r\n\r\n\t/**\r\n\t * Make an API request to the Vesal API\r\n\t * @private\r\n\t * @param {string} urlSuffix - The URL suffix for the API endpoint\r\n\t * @param {(\"GET\"|\"POST\")} [method=\"GET\"] - The HTTP method to use for the request\r\n\t * @param {object|null} [data=null] - The data to send with the request\r\n\t * @returns {Promise} The response from the API\r\n\t */\r\n\tprivate async Api(\r\n\t\turlSuffix: string,\r\n\t\tmethod: \"GET\" | \"POST\" = \"POST\",\r\n\t\tdata?: object,\r\n\t): Promise<any> {\r\n\t\tlet response: Response, responseBody: any;\r\n\r\n\t\tdata = {\r\n\t\t\t...data,\r\n\t\t\tusername: this.username,\r\n\t\t\tpassword: this.password,\r\n\t\t};\r\n\r\n\t\ttry {\r\n\t\t\tresponse = await fetch(`${this.apiUrl}/${urlSuffix}`, {\r\n\t\t\t\theaders: {\r\n\t\t\t\t\tAccept: \"application/json\",\r\n\t\t\t\t\t\"Content-Type\": \"application/json\",\r\n\t\t\t\t},\r\n\t\t\t\tmethod,\r\n\t\t\t\tbody: data && JSON.stringify(data),\r\n\t\t\t});\r\n\t\t} catch (error) {\r\n\t\t\tthrow new VesalError(undefined, \"Server connection failed\");\r\n\t\t}\r\n\r\n\t\ttry {\r\n\t\t\tresponseBody = await response.json();\r\n\t\t} catch (error) {\r\n\t\t\tthrow new VesalError(undefined, \"The server didn't respond correctly\");\r\n\t\t}\r\n\r\n\t\tif (!responseBody.references) {\r\n\t\t\tthrow new VesalError(undefined, \"The server didn't respond correctly\");\r\n\t\t}\r\n\r\n\t\treturn responseBody;\r\n\t}\r\n\r\n\tasync Send({\r\n\t\trecipients,\r\n\t\tmessages,\r\n\t\tfrom,\r\n\t}: {\r\n\t\trecipients: string | string[];\r\n\t\tmessages: string | string[];\r\n\t\tfrom?: string | string[];\r\n\t}): Promise<IVesalResponse_Send_WithCount> {\r\n\t\tif (!recipients || !messages) {\r\n\t\t\tthrow new VesalError(\r\n\t\t\t\tundefined,\r\n\t\t\t\t\"recipients and messages should not be empty\",\r\n\t\t\t);\r\n\t\t}\r\n\r\n\t\tif (typeof recipients === \"string\") {\r\n\t\t\trecipients = [recipients];\r\n\t\t}\r\n\r\n\t\tif (!from) {\r\n\t\t\tfrom = this.from;\r\n\t\t}\r\n\r\n\t\tconst isManyToMany = Array.isArray(messages) || Array.isArray(from);\r\n\r\n\t\tif (isManyToMany) {\r\n\t\t\tif (!recipients.length) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\t\t\tif (Array.isArray(messages) && !messages.length) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\r\n\t\t\tif (typeof messages === \"string\") {\r\n\t\t\t\t// messages = recipients.map(() => messages);\r\n\t\t\t\tmessages = Array(recipients.length).fill(messages);\r\n\t\t\t}\r\n\t\t\tif (typeof from === \"string\") {\r\n\t\t\t\t// from = recipients.map(() => from);\r\n\t\t\t\tfrom = Array(recipients.length).fill(from);\r\n\t\t\t}\r\n\r\n\t\t\tif (\r\n\t\t\t\trecipients.length !== messages.length ||\r\n\t\t\t\trecipients.length !== from.length\r\n\t\t\t) {\r\n\t\t\t\tthrow new VesalError(\r\n\t\t\t\t\tundefined,\r\n\t\t\t\t\t\"recipients and messages should have the same length\",\r\n\t\t\t\t);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst sendData = {\r\n\t\t\tdestinations: recipients,\r\n\t\t\t...(isManyToMany ? { originators: from } : { originator: from }),\r\n\t\t\t...(isManyToMany ? { contents: messages } : { content: messages }),\r\n\t\t};\r\n\r\n\t\tconst result: IVesalResponse_Send = await this.Api(\r\n\t\t\tisManyToMany ? \"ManyToMany\" : \"OneToMany\",\r\n\t\t\t\"POST\",\r\n\t\t\tsendData,\r\n\t\t);\r\n\r\n\t\tlet successCount: number = 0,\r\n\t\t\tfailCount: number = 0;\r\n\r\n\t\tresult.references?.map((messageResult) => {\r\n\t\t\tif (messageResult) {\r\n\t\t\t\tsuccessCount++;\r\n\t\t\t} else {\r\n\t\t\t\tfailCount++;\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\treturn {\r\n\t\t\t...result,\r\n\t\t\treferences: result.references || [],\r\n\t\t\tcount: {\r\n\t\t\t\tsuccess: successCount,\r\n\t\t\t\tfail: failCount,\r\n\t\t\t},\r\n\t\t};\r\n\t}\r\n\r\n\tasync GetMessageStatus(\r\n\t\treferencesIds: number[],\r\n\t): Promise<IVesalResponse_MessageState> {\r\n\t\treturn await this.Api(\"messageState\", \"POST\", {\r\n\t\t\treferenceids: referencesIds,\r\n\t\t});\r\n\t}\r\n\r\n\tasync GetReceivedMessages(): Promise<IVesalResponse_ReceivedMessages> {\r\n\t\treturn await this.Api(\"pullReceivedMessages\");\r\n\t}\r\n\r\n\tasync GetReceivedMessagesCount(): Promise<IVesalResponse_ReceivedMessagesCount> {\r\n\t\treturn await this.Api(\"receivedMessageCount\");\r\n\t}\r\n\r\n\tasync GetUserInfo(): Promise<IVesalResponse_UserInfo> {\r\n\t\treturn await this.Api(\"userInfo\");\r\n\t}\r\n}\r\n\r\nexport class VesalError extends Error {\r\n\tstatus: keyof typeof vesalErrors | undefined;\r\n\r\n\tconstructor(statusParam: number | undefined, messageParam?: string) {\r\n\t\tlet message: string, status: keyof typeof vesalErrors | undefined;\r\n\r\n\t\tif (statusParam && Object.keys(vesalErrors).includes(\"\" + statusParam)) {\r\n\t\t\tmessage = GetStatusText(statusParam);\r\n\t\t\tstatus = statusParam as any;\r\n\t\t} else {\r\n\t\t\tmessage = messageParam || \"Unknown Vesal error\";\r\n\t\t\tstatus = undefined;\r\n\t\t}\r\n\r\n\t\tsuper(message);\r\n\r\n\t\tthis.message = message;\r\n\t\tthis.status = status;\r\n\t}\r\n}\r\n\r\ninterface IVesalResponse_Base {\r\n\terrorModel: {\r\n\t\terrorCode: number;\r\n\t\ttimestamp: string | number | null;\r\n\t};\r\n}\r\n\r\ninterface IVesalResponse_Send extends IVesalResponse_Base {\r\n\treferences: (number | Omit<keyof typeof vesalErrors, 0>)[];\r\n}\r\n\r\ninterface IVesalResponse_Send_WithCount extends IVesalResponse_Send {\r\n\tcount: {\r\n\t\tsuccess: number;\r\n\t\tfail: number;\r\n\t};\r\n}\r\n\r\ninterface IVesalResponse_MessageState extends IVesalResponse_Base {\r\n\tstates: {\r\n\t\tid: number;\r\n\t\tstate: number;\r\n\t}[];\r\n}\r\ninterface IVesalResponse_ReceivedMessages extends IVesalResponse_Base {\r\n\tmessageModels: {\r\n\t\toriginator: string;\r\n\t\tdestination: string;\r\n\t\tcontent: string;\r\n\t}[];\r\n}\r\ninterface IVesalResponse_ReceivedMessagesCount extends IVesalResponse_Base {\r\n\tcount: number;\r\n}\r\ninterface IVesalResponse_UserInfo extends IVesalResponse_Base {\r\n\tuser: {\r\n\t\tcredit: number;\r\n\t\tdeliveryUrl: unknown;\r\n\t\treceiveUrl: unknown;\r\n\t\tiPs: string[];\r\n\t\tnumbers: string[];\r\n\t\tid: number;\r\n\t\tusername: string;\r\n\t\tpassword: unknown;\r\n\t\tfirstName: unknown;\r\n\t\tlastName: unknown;\r\n\t\tmobile: unknown;\r\n\t\temail: unknown;\r\n\t\tnationalId: number;\r\n\t\texpirationDate: string;\r\n\t\tactive: boolean;\r\n\t\tdeleted: boolean;\r\n\t\tdbProxyStandalone: boolean;\r\n\t\tinsertDate: unknown;\r\n\t\tupdateDate: unknown;\r\n\t\tcustomer: unknown;\r\n\t\troles: unknown;\r\n\t};\r\n}\r\n\r\nexport function GetStatusText(status: number) {\r\n\tif (status === 0) {\r\n\t\treturn \"success\";\r\n\t}\r\n\tif (Object.keys(vesalErrors).includes(\"\" + status)) {\r\n\t\treturn vesalErrors[status];\r\n\t}\r\n\treturn \"\";\r\n}\r\n\r\nexport const messageStates = {\r\n\t0: \"پیامک در صف ارسال قرار دارد\",\r\n\t1: \"ارسال شده\",\r\n\t2: \"پیامک به موبایل گیرنده تحویل شده است\",\r\n\t3: \"پیامک به موبایل گیرنده تحویل نشده است\",\r\n\t4: \"وضعیت نامشخص\",\r\n\t5: \"پیامک توسط وب سرویس به شرکت ارمغان راه طلایی رسیده است\",\r\n\t6: \"پیام از سمت اپراتور لغو شده است\",\r\n\t7: \"پیام از سمت اپراتور منقضی شده است\",\r\n\t8: \"پیام از سمت اپراتور reject شده است\",\r\n} as const;\r\n\r\nexport const vesalErrors = {\r\n\t0: \"عملیات با موفقیت انجام شد\",\r\n\t[-100]: \"refrenceId مورد نظر یافت نشد\",\r\n\t[-101]: \"احراز هویت کاربر موفقیت آمیز نبود\",\r\n\t[-102]: \"نام کاربری یافت نشد\",\r\n\t[-103]: \"شماره originator اشتباه یا در بازه شماره های کاربر نیست\",\r\n\t[-104]: \"اعتبار کم است\",\r\n\t[-105]: \"فرمت درخواست اشتباه است\",\r\n\t[-106]: \"تعداد refrenceId ها بیش از 1000 عدد است\",\r\n\t[-107]: \"شماره گیرنده پیامک اشتباه است\",\r\n\t[-109]: \"تاریخ انقضای حساب کاربری فرارسیده است\",\r\n\t[-110]: \"درخواست از ip مجاز کاربر ارسال نشده است\",\r\n\t[-111]: \"شماره گیرنده در بلک لیست قرار دارد\",\r\n\t[-112]: \"حساب مشتری فعال نیست\",\r\n\t[-115]: \"فرمت UDH اشتباه است\",\r\n\t[-117]: \"مقدار mclass وارد شده اشتباه است\",\r\n\t[-118]: \"شماره پورت وارد شده صحیح نیست\",\r\n\t[-119]: \"کاربر به سرویس مورد نظر دسترسی ندارد\",\r\n\t[-120]: \"پیام ارسال شده دارای هیچ شماره معتبری نیست\",\r\n\t[-200]: \"خطای داخلی در پایگاه داده رخ داده است\",\r\n\t[-201]: \"خطای نامشخص داخل پایگاه داده\",\r\n\t[-137]: \"پیام نباید حاوی کلمات غیرمجاز می باشد\",\r\n} as const;\r\n\r\nexport default Vesal;\r\n"],"names":["Vesal","username","password","from","this","apiUrl","_proto","prototype","Api","urlSuffix","method","data","response","responseBody","_temp4","_result","_temp2","_result2","references","VesalError","undefined","_temp","_catch","Promise","resolve","json","then","_response$json","_exit","_this","_extends","_temp3","fetch","headers","Accept","body","JSON","stringify","_fetch","e","reject","Send","_ref","recipients","messages","isManyToMany","Array","isArray","length","fill","sendData","destinations","originators","originator","contents","content","result","_result$references","successCount","failCount","map","messageResult","count","success","fail","GetMessageStatus","referencesIds","referenceids","GetReceivedMessages","GetReceivedMessagesCount","GetUserInfo","_Error","statusParam","messageParam","_this7","message","status","Object","keys","vesalErrors","includes","GetStatusText","call","_wrapNativeSuper","Error","_vesalErrors"],"mappings":"wnDAOa,IAAAA,eAAK,WAMjB,SAAAA,EAAYC,EAAkBC,EAAkBC,GAK/C,OAL2DC,KAL5CC,OAAS,sCACjBJ,KAAAA,qBACAC,cAAQ,EAAAE,KACRD,UAAI,EAGXC,KAAKH,SAAWA,GAAY,GAC5BG,KAAKF,SAAWA,EAChBE,KAAKD,KAAOA,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M,uCAAsCA,GAC5C,KAAM,6CAA4CA,GAClD,KAAM,wCAAuCA,GAC7C,KAAM,+BAA8BA,GACpC,KAAM,wCAAuCA,0EAjCnB,CAC5B,EAAG,8BACH,EAAG,YACH,EAAG,uCACH,EAAG,wCACH,EAAG,eACH,EAAG,yDACH,EAAG,kCACH,EAAG,oCACH,EAAG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vesal",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Armaghan Vesal SMS API client for JS/TS/ESM/Node.js",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Shahab Movahhedi",
|
|
7
|
+
"email": "dev@shmovahhedi.com",
|
|
8
|
+
"url": "https://shmovahhedi.com/"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/movahhedi/vesal",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/movahhedi/vesal.git",
|
|
14
|
+
"directory": "/"
|
|
15
|
+
},
|
|
16
|
+
"bugs": "https://github.com/movahhedi/vesal",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"vesal",
|
|
20
|
+
"vesal-sdk",
|
|
21
|
+
"vesal-js",
|
|
22
|
+
"sms-ir",
|
|
23
|
+
"armaghan-vesal",
|
|
24
|
+
"vesal.armaghan.net",
|
|
25
|
+
"armaghan",
|
|
26
|
+
"sms",
|
|
27
|
+
"messaging",
|
|
28
|
+
"api"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"source": "./src/vesal.ts",
|
|
32
|
+
"main": "./dist/vesal.cjs",
|
|
33
|
+
"module": "./dist/vesal.mjs",
|
|
34
|
+
"types": "./dist/vesal.d.ts",
|
|
35
|
+
"typings": "./dist/vesal.d.ts",
|
|
36
|
+
"umd:main": "./dist/vesal.umd.js",
|
|
37
|
+
"unpkg": "./dist/vesal.umd.js",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"require": "./dist/vesal.cjs",
|
|
41
|
+
"default": "./dist/vesal.modern.js"
|
|
42
|
+
},
|
|
43
|
+
"./ts": "./src/vesal.ts",
|
|
44
|
+
"./package.json": "./package.json"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"src",
|
|
48
|
+
"dist"
|
|
49
|
+
],
|
|
50
|
+
"cspell": {
|
|
51
|
+
"version": "0.2",
|
|
52
|
+
"language": [
|
|
53
|
+
"en",
|
|
54
|
+
"fa"
|
|
55
|
+
],
|
|
56
|
+
"words": [
|
|
57
|
+
"Armaghan",
|
|
58
|
+
"likeToLike",
|
|
59
|
+
"mclass",
|
|
60
|
+
"microbundle",
|
|
61
|
+
"referenceids",
|
|
62
|
+
"refrence",
|
|
63
|
+
"Vesal"
|
|
64
|
+
],
|
|
65
|
+
"ignorePaths": [
|
|
66
|
+
"README.fa.md"
|
|
67
|
+
]
|
|
68
|
+
},
|
|
69
|
+
"packageManager": "yarn@4.1.0",
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "microbundle build --raw true --generateType -f modern,esm,cjs,umd",
|
|
72
|
+
"dev": "microbundle watch -f modern,esm,cjs,umd"
|
|
73
|
+
},
|
|
74
|
+
"devDependencies": {
|
|
75
|
+
"@typescript-eslint/eslint-plugin": "^6.19.0",
|
|
76
|
+
"@typescript-eslint/parser": "^6.19.0",
|
|
77
|
+
"concurrently": "^8.2.2",
|
|
78
|
+
"eslint": "^8.56.0",
|
|
79
|
+
"eslint-config-prettier": "^9.1.0",
|
|
80
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
81
|
+
"eslint-plugin-react": "^7.33.2",
|
|
82
|
+
"eslint-plugin-redos": "^4.4.5",
|
|
83
|
+
"microbundle": "^0.15.1",
|
|
84
|
+
"npm-check-updates": "^16.14.12",
|
|
85
|
+
"prettier": "^3.2.4",
|
|
86
|
+
"rimraf": "^5.0.5",
|
|
87
|
+
"typescript": "^5.3.3"
|
|
88
|
+
}
|
|
89
|
+
}
|
package/src/vesal.ts
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Vesal API Class
|
|
3
|
+
* @author Shahab Movahhedi
|
|
4
|
+
* @see {@link https://shmovahhedi.com Shahab Movahhedi's Website}
|
|
5
|
+
* @see {@link https://github.com/movahhedi/vesal vesal's Repository}
|
|
6
|
+
* @license MIT
|
|
7
|
+
*/
|
|
8
|
+
export class Vesal {
|
|
9
|
+
public readonly apiUrl = "http://vesal.armaghan.net:8080/rest";
|
|
10
|
+
private username: string;
|
|
11
|
+
private password: string;
|
|
12
|
+
private from: string;
|
|
13
|
+
|
|
14
|
+
constructor(username: string, password: string, from: string) {
|
|
15
|
+
this.username = username || "";
|
|
16
|
+
this.password = password;
|
|
17
|
+
this.from = from;
|
|
18
|
+
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Make an API request to the Vesal API
|
|
24
|
+
* @private
|
|
25
|
+
* @param {string} urlSuffix - The URL suffix for the API endpoint
|
|
26
|
+
* @param {("GET"|"POST")} [method="GET"] - The HTTP method to use for the request
|
|
27
|
+
* @param {object|null} [data=null] - The data to send with the request
|
|
28
|
+
* @returns {Promise} The response from the API
|
|
29
|
+
*/
|
|
30
|
+
private async Api(
|
|
31
|
+
urlSuffix: string,
|
|
32
|
+
method: "GET" | "POST" = "POST",
|
|
33
|
+
data?: object,
|
|
34
|
+
): Promise<any> {
|
|
35
|
+
let response: Response, responseBody: any;
|
|
36
|
+
|
|
37
|
+
data = {
|
|
38
|
+
...data,
|
|
39
|
+
username: this.username,
|
|
40
|
+
password: this.password,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
response = await fetch(`${this.apiUrl}/${urlSuffix}`, {
|
|
45
|
+
headers: {
|
|
46
|
+
Accept: "application/json",
|
|
47
|
+
"Content-Type": "application/json",
|
|
48
|
+
},
|
|
49
|
+
method,
|
|
50
|
+
body: data && JSON.stringify(data),
|
|
51
|
+
});
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new VesalError(undefined, "Server connection failed");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
responseBody = await response.json();
|
|
58
|
+
} catch (error) {
|
|
59
|
+
throw new VesalError(undefined, "The server didn't respond correctly");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!responseBody.references) {
|
|
63
|
+
throw new VesalError(undefined, "The server didn't respond correctly");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return responseBody;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async Send({
|
|
70
|
+
recipients,
|
|
71
|
+
messages,
|
|
72
|
+
from,
|
|
73
|
+
}: {
|
|
74
|
+
recipients: string | string[];
|
|
75
|
+
messages: string | string[];
|
|
76
|
+
from?: string | string[];
|
|
77
|
+
}): Promise<IVesalResponse_Send_WithCount> {
|
|
78
|
+
if (!recipients || !messages) {
|
|
79
|
+
throw new VesalError(
|
|
80
|
+
undefined,
|
|
81
|
+
"recipients and messages should not be empty",
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (typeof recipients === "string") {
|
|
86
|
+
recipients = [recipients];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!from) {
|
|
90
|
+
from = this.from;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const isManyToMany = Array.isArray(messages) || Array.isArray(from);
|
|
94
|
+
|
|
95
|
+
if (isManyToMany) {
|
|
96
|
+
if (!recipients.length) {
|
|
97
|
+
throw new VesalError(
|
|
98
|
+
undefined,
|
|
99
|
+
"recipients and messages should have the same length",
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
if (Array.isArray(messages) && !messages.length) {
|
|
103
|
+
throw new VesalError(
|
|
104
|
+
undefined,
|
|
105
|
+
"recipients and messages should have the same length",
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (typeof messages === "string") {
|
|
110
|
+
// messages = recipients.map(() => messages);
|
|
111
|
+
messages = Array(recipients.length).fill(messages);
|
|
112
|
+
}
|
|
113
|
+
if (typeof from === "string") {
|
|
114
|
+
// from = recipients.map(() => from);
|
|
115
|
+
from = Array(recipients.length).fill(from);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (
|
|
119
|
+
recipients.length !== messages.length ||
|
|
120
|
+
recipients.length !== from.length
|
|
121
|
+
) {
|
|
122
|
+
throw new VesalError(
|
|
123
|
+
undefined,
|
|
124
|
+
"recipients and messages should have the same length",
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const sendData = {
|
|
130
|
+
destinations: recipients,
|
|
131
|
+
...(isManyToMany ? { originators: from } : { originator: from }),
|
|
132
|
+
...(isManyToMany ? { contents: messages } : { content: messages }),
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const result: IVesalResponse_Send = await this.Api(
|
|
136
|
+
isManyToMany ? "ManyToMany" : "OneToMany",
|
|
137
|
+
"POST",
|
|
138
|
+
sendData,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
let successCount: number = 0,
|
|
142
|
+
failCount: number = 0;
|
|
143
|
+
|
|
144
|
+
result.references?.map((messageResult) => {
|
|
145
|
+
if (messageResult) {
|
|
146
|
+
successCount++;
|
|
147
|
+
} else {
|
|
148
|
+
failCount++;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
...result,
|
|
154
|
+
references: result.references || [],
|
|
155
|
+
count: {
|
|
156
|
+
success: successCount,
|
|
157
|
+
fail: failCount,
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async GetMessageStatus(
|
|
163
|
+
referencesIds: number[],
|
|
164
|
+
): Promise<IVesalResponse_MessageState> {
|
|
165
|
+
return await this.Api("messageState", "POST", {
|
|
166
|
+
referenceids: referencesIds,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async GetReceivedMessages(): Promise<IVesalResponse_ReceivedMessages> {
|
|
171
|
+
return await this.Api("pullReceivedMessages");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async GetReceivedMessagesCount(): Promise<IVesalResponse_ReceivedMessagesCount> {
|
|
175
|
+
return await this.Api("receivedMessageCount");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async GetUserInfo(): Promise<IVesalResponse_UserInfo> {
|
|
179
|
+
return await this.Api("userInfo");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export class VesalError extends Error {
|
|
184
|
+
status: keyof typeof vesalErrors | undefined;
|
|
185
|
+
|
|
186
|
+
constructor(statusParam: number | undefined, messageParam?: string) {
|
|
187
|
+
let message: string, status: keyof typeof vesalErrors | undefined;
|
|
188
|
+
|
|
189
|
+
if (statusParam && Object.keys(vesalErrors).includes("" + statusParam)) {
|
|
190
|
+
message = GetStatusText(statusParam);
|
|
191
|
+
status = statusParam as any;
|
|
192
|
+
} else {
|
|
193
|
+
message = messageParam || "Unknown Vesal error";
|
|
194
|
+
status = undefined;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
super(message);
|
|
198
|
+
|
|
199
|
+
this.message = message;
|
|
200
|
+
this.status = status;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
interface IVesalResponse_Base {
|
|
205
|
+
errorModel: {
|
|
206
|
+
errorCode: number;
|
|
207
|
+
timestamp: string | number | null;
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
interface IVesalResponse_Send extends IVesalResponse_Base {
|
|
212
|
+
references: (number | Omit<keyof typeof vesalErrors, 0>)[];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
interface IVesalResponse_Send_WithCount extends IVesalResponse_Send {
|
|
216
|
+
count: {
|
|
217
|
+
success: number;
|
|
218
|
+
fail: number;
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
interface IVesalResponse_MessageState extends IVesalResponse_Base {
|
|
223
|
+
states: {
|
|
224
|
+
id: number;
|
|
225
|
+
state: number;
|
|
226
|
+
}[];
|
|
227
|
+
}
|
|
228
|
+
interface IVesalResponse_ReceivedMessages extends IVesalResponse_Base {
|
|
229
|
+
messageModels: {
|
|
230
|
+
originator: string;
|
|
231
|
+
destination: string;
|
|
232
|
+
content: string;
|
|
233
|
+
}[];
|
|
234
|
+
}
|
|
235
|
+
interface IVesalResponse_ReceivedMessagesCount extends IVesalResponse_Base {
|
|
236
|
+
count: number;
|
|
237
|
+
}
|
|
238
|
+
interface IVesalResponse_UserInfo extends IVesalResponse_Base {
|
|
239
|
+
user: {
|
|
240
|
+
credit: number;
|
|
241
|
+
deliveryUrl: unknown;
|
|
242
|
+
receiveUrl: unknown;
|
|
243
|
+
iPs: string[];
|
|
244
|
+
numbers: string[];
|
|
245
|
+
id: number;
|
|
246
|
+
username: string;
|
|
247
|
+
password: unknown;
|
|
248
|
+
firstName: unknown;
|
|
249
|
+
lastName: unknown;
|
|
250
|
+
mobile: unknown;
|
|
251
|
+
email: unknown;
|
|
252
|
+
nationalId: number;
|
|
253
|
+
expirationDate: string;
|
|
254
|
+
active: boolean;
|
|
255
|
+
deleted: boolean;
|
|
256
|
+
dbProxyStandalone: boolean;
|
|
257
|
+
insertDate: unknown;
|
|
258
|
+
updateDate: unknown;
|
|
259
|
+
customer: unknown;
|
|
260
|
+
roles: unknown;
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function GetStatusText(status: number) {
|
|
265
|
+
if (status === 0) {
|
|
266
|
+
return "success";
|
|
267
|
+
}
|
|
268
|
+
if (Object.keys(vesalErrors).includes("" + status)) {
|
|
269
|
+
return vesalErrors[status];
|
|
270
|
+
}
|
|
271
|
+
return "";
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export const messageStates = {
|
|
275
|
+
0: "پیامک در صف ارسال قرار دارد",
|
|
276
|
+
1: "ارسال شده",
|
|
277
|
+
2: "پیامک به موبایل گیرنده تحویل شده است",
|
|
278
|
+
3: "پیامک به موبایل گیرنده تحویل نشده است",
|
|
279
|
+
4: "وضعیت نامشخص",
|
|
280
|
+
5: "پیامک توسط وب سرویس به شرکت ارمغان راه طلایی رسیده است",
|
|
281
|
+
6: "پیام از سمت اپراتور لغو شده است",
|
|
282
|
+
7: "پیام از سمت اپراتور منقضی شده است",
|
|
283
|
+
8: "پیام از سمت اپراتور reject شده است",
|
|
284
|
+
} as const;
|
|
285
|
+
|
|
286
|
+
export const vesalErrors = {
|
|
287
|
+
0: "عملیات با موفقیت انجام شد",
|
|
288
|
+
[-100]: "refrenceId مورد نظر یافت نشد",
|
|
289
|
+
[-101]: "احراز هویت کاربر موفقیت آمیز نبود",
|
|
290
|
+
[-102]: "نام کاربری یافت نشد",
|
|
291
|
+
[-103]: "شماره originator اشتباه یا در بازه شماره های کاربر نیست",
|
|
292
|
+
[-104]: "اعتبار کم است",
|
|
293
|
+
[-105]: "فرمت درخواست اشتباه است",
|
|
294
|
+
[-106]: "تعداد refrenceId ها بیش از 1000 عدد است",
|
|
295
|
+
[-107]: "شماره گیرنده پیامک اشتباه است",
|
|
296
|
+
[-109]: "تاریخ انقضای حساب کاربری فرارسیده است",
|
|
297
|
+
[-110]: "درخواست از ip مجاز کاربر ارسال نشده است",
|
|
298
|
+
[-111]: "شماره گیرنده در بلک لیست قرار دارد",
|
|
299
|
+
[-112]: "حساب مشتری فعال نیست",
|
|
300
|
+
[-115]: "فرمت UDH اشتباه است",
|
|
301
|
+
[-117]: "مقدار mclass وارد شده اشتباه است",
|
|
302
|
+
[-118]: "شماره پورت وارد شده صحیح نیست",
|
|
303
|
+
[-119]: "کاربر به سرویس مورد نظر دسترسی ندارد",
|
|
304
|
+
[-120]: "پیام ارسال شده دارای هیچ شماره معتبری نیست",
|
|
305
|
+
[-200]: "خطای داخلی در پایگاه داده رخ داده است",
|
|
306
|
+
[-201]: "خطای نامشخص داخل پایگاه داده",
|
|
307
|
+
[-137]: "پیام نباید حاوی کلمات غیرمجاز می باشد",
|
|
308
|
+
} as const;
|
|
309
|
+
|
|
310
|
+
export default Vesal;
|