@zennify/sdk-js 1.0.0-beta.1

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.
@@ -0,0 +1,35 @@
1
+ name: Publish to npm
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v4
13
+ - uses: actions/setup-node@v3
14
+ with:
15
+ node-version: 20
16
+ - run: npm ci
17
+ - run: npm run build
18
+
19
+ publish-gpr:
20
+ needs: build
21
+ runs-on: ubuntu-latest
22
+ permissions:
23
+ contents: read
24
+ packages: write
25
+ steps:
26
+ - uses: actions/checkout@v4
27
+ - uses: actions/setup-node@v3
28
+ with:
29
+ node-version: 20
30
+ registry-url: https://registry.npmjs.org/
31
+ - run: npm ci
32
+ - run: npm run build
33
+ - run: npm publish --access public --tag beta
34
+ env:
35
+ NPM_TOKEN: ${{secrets.NPM_TOKEN}}
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@zennify/sdk-js",
3
+ "version": "1.0.0-beta.1",
4
+ "description": "A simple package to work with https://api.zennify.app",
5
+ "main": "dist/main.js",
6
+ "keywords": [],
7
+ "author": "josejooj",
8
+ "license": "ISC",
9
+ "repository": {
10
+ "url": "git+https://github.com/zennify-ofc/sdk-js.git"
11
+ },
12
+ "devDependencies": {
13
+ "@types/node": "^20.12.12",
14
+ "typescript": "^5.5.3"
15
+ },
16
+ "scripts": {
17
+ "build": "tsc"
18
+ },
19
+ "dependencies": {
20
+ "undici-types": "^5.26.5"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/zennify-ofc/sdk-js/issues"
24
+ },
25
+ "homepage": "https://github.com/zennify-ofc/sdk-js#readme"
26
+ }
@@ -0,0 +1,55 @@
1
+ import pt_BR from '../translations/pt_BR.json';
2
+ import en_US from '../translations/en_US.json';
3
+ import { ZENNIFY_API_RESPONSE_LANGUAGE } from './var';
4
+
5
+ const errors_translations = { pt_BR, en_US };
6
+ const match_regex = /{[\w.]+}/g;
7
+ const replacer = (body: any, value: string) => {
8
+ return body[value.slice(1, -1)]
9
+ }
10
+
11
+ type Body = Record<string, any> & {
12
+ code: number
13
+ }
14
+
15
+ export class ZennifyAPIRequestError extends Error {
16
+
17
+ status: number = 0;
18
+ code: number = 0;
19
+ body: Body
20
+
21
+ constructor(response: Response, body: Body) {
22
+
23
+ super();
24
+
25
+ this.body = body;
26
+ this.status = response.status;
27
+
28
+ if (this.status === 403) {
29
+ this.name = "Usuário inválido";
30
+ this.message = "Por favor, se re-autentique"
31
+ }
32
+
33
+ if (this.status === 503) {
34
+ this.name = "API em atualização.";
35
+ this.message = "Por favor, tente novamente em alguns minutos.";
36
+ }
37
+
38
+ if (this.status === 429) {
39
+ this.name = "Calma ai!";
40
+ this.message = "Suas ações estão sendo limitadas!";
41
+ }
42
+
43
+ const error: { name: string, message: string } =
44
+ errors_translations[ZENNIFY_API_RESPONSE_LANGUAGE][body.code as 0] ||
45
+ errors_translations[ZENNIFY_API_RESPONSE_LANGUAGE]["1"];
46
+
47
+ this.name = error.name
48
+ .replace(match_regex, (value) => replacer(body, value));
49
+
50
+ this.message = error.message
51
+ .replace(match_regex, (value) => replacer(body, value));
52
+
53
+ }
54
+
55
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,108 @@
1
+ export enum APIErrors {
2
+ UNKNOWN_ERROR = 0,
3
+ UNKNOWN_TRANSLATION = 1,
4
+ METHOD_NOT_ALLOWED = 2,
5
+ EXPECTED_AN_OBJECT_ARRAY = 3,
6
+ EXPECTED_A_STRING_ARRAY = 4,
7
+ EXPECTED_A_NUMBER_ARRAY = 5,
8
+ EXPECTED_AN_OBJECT = 6,
9
+ EXPECTED_A_NON_EMPTY_OBJECT = 7,
10
+ EXPECTED_A_STRING = 8,
11
+ EXPECTED_A_NUMBER = 9,
12
+ DUPLICATED_VALUES_ISNT_ALLOWED = 10,
13
+ // 1000-1100 = oAuth errors
14
+ GRANT_CODE_MISSING_IN_QUERY = 1000,
15
+ INVALID_SCOPE = 1001,
16
+ VERIFY_YOUR_EMAIL = 1002,
17
+ PLEASE_REVERIFY = 1003,
18
+ // 1500-2000 = user errors
19
+ USER_NOT_FOUND = 1500,
20
+ // 1501 - 1510
21
+ // password errors
22
+ INSERT_A_VALID_PASSWORD = 1501,
23
+ YOU_MUST_INSERT_THE_ACTUAL_PASSWORD = 1502,
24
+ THE_PASSWORD_DOES_NOT_MATCH = 1503,
25
+ THE_PASSWORD_MUST_HAVE_AT_LEAST_8_CHARACTERS = 1504,
26
+ THE_PASSWORD_MUST_HAVE_AT_LEAST_ONE_NUMBER = 1505,
27
+ THE_PASSWORD_MUST_HAVE_AT_LEAST_ONE_LETTER = 1506,
28
+ THE_PASSWORD_MUST_HAVE_AT_LEAST_ONE_SPECIAL_CHARACTER = 1507,
29
+ // 1511 - 1520
30
+ // pix key errors
31
+ THE_PIX_KEY_SENDED_ISNT_A_VALID_STRING = 1511,
32
+ THE_EMAIL_SENDED_ISNT_VALID = 1512,
33
+ CELLPHONE_NUMBERS_MUST_HAVE_THE_9_DIGIT = 1513,
34
+ THE_CELLPHONE_NUMBER_MUST_HAVE_ONLY_DIGITS = 1514,
35
+ THE_PIX_KEY_INSERTED_ISNT_VALID = 1515,
36
+ // 1521 - 1530
37
+ // webhook url error
38
+ INSERT_A_VALID_URL = 1521,
39
+ THE_ONLY_ACCEPTED_PROTOCOL_IS_HTTPS = 1522,
40
+ THIS_DOMAIN_IS_BLACKLISTED = 1523,
41
+ THE_URL_EXTENSION_ISNT_ACCEPTED_YET = 1524,
42
+ THE_URL_IS_INACCESSIBLE_BY_THE_WEBHOOK_SERVER = 1525,
43
+ THE_URL_WOULD_NOT_HAVE_REDIRECTS = 1526,
44
+ THE_URL_HAS_TIMEOUT_5S = 1527,
45
+ // 2000-3000 = payment errors
46
+ INVALID_PAYMENT_METHOD = 2000,
47
+ // 2200-2500
48
+ // Order Errors
49
+ YOU_NEED_TO_INSERT_AT_LEAST_1_ITEM = 2200,
50
+ ONE_PROVIDED_PRODUCT_IS_OUT_OF_STOCK = 2201,
51
+ WE_DONT_HAVE_THIS_AMOUNT_IN_STOCK = 2202,
52
+ THIS_SELLER_WAS_BANNED_FROM_ZENNIFY = 2203,
53
+ WE_DO_NOT_YET_SUPPORT_MULTIPLE_USER_PRODUCTS = 2204,
54
+ WE_DO_NOT_YET_SUPPORT_MULTIPLE_STORE_PRODUCTS = 2205,
55
+ YOU_ALREADY_HAVE_ANOTHER_ORDER_WAITING_PAYMENT = 2206,
56
+ YOU_INSERTED_AN_INVALID_ADDITIONAL = 2207,
57
+ // 2211 - 2250
58
+ // subtype
59
+ // bad preference configuration
60
+ THIS_SELLER_HAS_NOT_YET_CONFIGURED_RECEIPT_VIA_PIX = 2211,
61
+ THIS_SELLER_HAS_NOT_YET_CONFIGURED_RECEIPT_VIA_BOLETO = 2212,
62
+ THIS_SELLER_HAS_NOT_YET_CONFIGURED_RECEIPT_VIA_CRYPTO = 2213,
63
+ // 2251 - 2275
64
+ // subtype
65
+ // coupons
66
+ THIS_COUPON_DOES_NOT_EXISTS = 2251,
67
+ THIS_COUPON_HAS_EXPIRED = 2252,
68
+ THIS_COUPON_REACHED_THE_USE_LIMIT = 2253,
69
+ YOU_NEED_TO_SELECT_MORE_ITENS_DO_REACH_THE_MINIMUM_VALUE_OF_THE_COUPON = 2254,
70
+ THIS_COUPON_IS_VALID_ONLY_FOR_THE_FIRST_BUY = 2255,
71
+ // 2500 - 2600 = store errors
72
+ UNKNOWN_STORE = 2500,
73
+ UNKNOWN_BANK = 2501,
74
+ INVALID_STORE_NAME = 2502,
75
+ // 2600 - 3000 = product errors
76
+ UNKNOWN_PRODUCT = 2600,
77
+ INVALID_PRODUCT_NAME = 2601,
78
+ INVALID_PRODUCT_VALUE = 2602,
79
+ YOU_REACHED_THE_PRODUCT_LIMIT = 2603,
80
+ YOU_ALREADY_HAVE_ANOTHER_PRODUCT_WITH_THIS_NAME = 2604,
81
+ // 2605 - 2610
82
+ // Description errors
83
+ INVALID_DISCORD_DESCRIPTION = 2605,
84
+ // 2611 - 2650
85
+ // Stock errors
86
+ INVALID_STOCK_ID = 2611,
87
+ UNKNOWN_STOCK = 2612,
88
+ INVALID_STOCK_CONTENT = 2613,
89
+ INVALID_STOCK_COST = 2614,
90
+ // 3001 - 3100 = Media Errors
91
+ UNKNOWN_FILE = 3001,
92
+ NO_FILES_FOUND = 3002,
93
+ INVALID_FILE_TYPE = 3003,
94
+ INVALID_FILE_NAME = 3004,
95
+ INVALID_FILE_SIZE = 3005,
96
+ YOU_REACHED_THE_MAXIMUM_STORAGE = 3006,
97
+ YOU_REACHED_THE_MAXIMUM_MEDIA_COUNT = 3007,
98
+ YOU_ALREADY_HAVE_THIS_IMAGE_ON_ZENNIFY = 3008,
99
+ YOU_ALREADY_HAVE_ANOTHER_IMAGE_WITH_THIS_NAME = 3009,
100
+ // 3101 - 3200 = Discord Panels Errors
101
+ UNKNOWN_PANEL = 3101,
102
+ INVALID_PANEL_NAME = 3102,
103
+ INVALID_PANEL_DESCRIPTION = 3103,
104
+ INVALID_PANEL_PLACEHOLDER = 3104,
105
+ YOU_REACHED_THE_PANEL_LIMIT = 3105,
106
+ YOU_ALREADY_HAVE_ANOTHER_PANEL_WITH_THIS_NAME = 3106,
107
+ YOU_REACHED_THE_PRODUCTS_LIMIT_ON_PANEL = 3107,
108
+ }
package/src/main.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './var';
2
+ export * from './errors';
3
+ export * from './utils';
package/src/utils.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { ZennifyAPIRequestError } from './RequestError';
2
+ import { APIErrors } from './errors';
3
+
4
+ export function GetResponseError(
5
+ error: keyof typeof APIErrors,
6
+ additional: any = {}
7
+ ): { error: keyof APIErrors | string, code: number } & Record<string, any> {
8
+
9
+ const code = APIErrors[error];
10
+
11
+ return { error, code, ...additional }
12
+
13
+ }
14
+
15
+ export async function TreatZennifyResponse<T extends any>(response: Response): Promise<T> {
16
+
17
+ const body = await response.json();
18
+
19
+ if (response.ok) {
20
+ return body as T;
21
+ }
22
+
23
+ throw new ZennifyAPIRequestError(response, body);
24
+
25
+ }
package/src/var.ts ADDED
@@ -0,0 +1,3 @@
1
+ type Languages = 'pt_BR' | 'en_US';
2
+
3
+ export let ZENNIFY_API_RESPONSE_LANGUAGE: Languages = 'en_US';
@@ -0,0 +1,10 @@
1
+ {
2
+ "0": {
3
+ "name": "An unknown error happened.",
4
+ "message": "Contact the support with id {id}"
5
+ },
6
+ "1": {
7
+ "name": "{code} not translated yet",
8
+ "message": "Contact the support."
9
+ }
10
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "0": {
3
+ "name": "Um erro deconhecido ocorreu.",
4
+ "message": "Contate o suporte com o ID {id}"
5
+ },
6
+ "1": {
7
+ "name": "{code} não traduzido ainda",
8
+ "message": "Entre em contato com o suporte."
9
+ }
10
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig to read more about this file */
4
+
5
+ "target": "ES2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
6
+ "module": "CommonJS", /* Specify what module code is generated. */
7
+ "rootDir": "./src", /* Specify the root folder within your source files. */
8
+ "resolveJsonModule": true, /* Enable importing .json files. */
9
+ "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
10
+ "outDir": "dist", /* Specify an output folder for all emitted files. */
11
+ "declarationDir": "./types", /* Specify the output directory for generated declaration files. */
12
+ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
13
+ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
14
+ "strict": true, /* Enable all strict type-checking options. */
15
+ "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
16
+ "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
17
+ "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
18
+ "skipLibCheck": true, /* Skip type checking all .d.ts files. */
19
+ }
20
+ }