@dma-sdk/utils 1.0.2
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/fetch.d.ts +1 -0
- package/dist/fetch.js +26 -0
- package/dist/logger.d.ts +10 -0
- package/dist/logger.js +31 -0
- package/dist/validation.d.ts +5 -0
- package/dist/validation.js +13 -0
- package/package.json +17 -0
- package/src/fetch.ts +22 -0
- package/src/logger.ts +32 -0
- package/src/validation.ts +13 -0
- package/tsconfig.json +12 -0
package/dist/fetch.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function fetchApi(baseUrl: string, endpoint: string, options?: RequestInit, queryParams?: {}): Promise<Response>;
|
package/dist/fetch.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchApi = fetchApi;
|
|
4
|
+
async function fetchApi(baseUrl, endpoint, options = {}, queryParams) {
|
|
5
|
+
// Logger.info(`fetchApi > baseUrl: ${baseUrl}, endpoint: ${endpoint}, options: ${JSON.stringify(options)}, queryParams: ${JSON.stringify(queryParams)}`)
|
|
6
|
+
let res;
|
|
7
|
+
baseUrl = (baseUrl.endsWith("/")) ? baseUrl : baseUrl + "/";
|
|
8
|
+
if (endpoint.startsWith("/"))
|
|
9
|
+
endpoint = endpoint.slice(1);
|
|
10
|
+
let url = baseUrl + endpoint;
|
|
11
|
+
if (queryParams)
|
|
12
|
+
url = url + "?" + (new URLSearchParams(queryParams)).toString();
|
|
13
|
+
res = await fetch(url, options);
|
|
14
|
+
if (!res.ok && res.status >= 400) {
|
|
15
|
+
const errorMessage = await res.text();
|
|
16
|
+
if (res.status === 400)
|
|
17
|
+
throw new Error(`400 Bad request: ${errorMessage}`);
|
|
18
|
+
if (res.status === 401)
|
|
19
|
+
throw new Error(`401 Unauthorized: ${errorMessage}`);
|
|
20
|
+
if (res.status === 403)
|
|
21
|
+
throw new Error(`403 Forbidden: ${res.statusText})`);
|
|
22
|
+
if (res.status === 404)
|
|
23
|
+
throw new Error(`404 Resource not found: ${errorMessage}`);
|
|
24
|
+
}
|
|
25
|
+
return res;
|
|
26
|
+
}
|
package/dist/logger.d.ts
ADDED
package/dist/logger.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Logger = void 0;
|
|
5
|
+
class Logger {
|
|
6
|
+
}
|
|
7
|
+
exports.Logger = Logger;
|
|
8
|
+
_a = Logger;
|
|
9
|
+
Logger.log = (params) => {
|
|
10
|
+
console.log(`[${Date.now()}] [${params.level}] ${JSON.stringify(params.data)}`);
|
|
11
|
+
};
|
|
12
|
+
Logger.debug = (data) => {
|
|
13
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
14
|
+
_a.log({
|
|
15
|
+
level: 'DEBUG',
|
|
16
|
+
data,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
Logger.info = (data) => _a.log({
|
|
21
|
+
level: 'INFO',
|
|
22
|
+
data,
|
|
23
|
+
});
|
|
24
|
+
Logger.warn = (data) => _a.log({
|
|
25
|
+
level: 'WARNING',
|
|
26
|
+
data,
|
|
27
|
+
});
|
|
28
|
+
Logger.notice = (data) => _a.log({
|
|
29
|
+
level: 'NOTICE',
|
|
30
|
+
data,
|
|
31
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValidationUtils = void 0;
|
|
4
|
+
class ValidationUtils {
|
|
5
|
+
static parse(data, schema) {
|
|
6
|
+
return schema.parse(data);
|
|
7
|
+
}
|
|
8
|
+
static safeParse(data, schema) {
|
|
9
|
+
const result = schema.safeParse(data);
|
|
10
|
+
return [result.success ? result.data : null, result.error];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.ValidationUtils = ValidationUtils;
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dma-sdk/utils",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "DMA SDK – Utils",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepare": "npm run build"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"zod": "^4.3.5"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"typescript": "^5.2.0"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/fetch.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export async function fetchApi(baseUrl: string, endpoint: string, options: RequestInit = {}, queryParams?: {}) {
|
|
2
|
+
|
|
3
|
+
// Logger.info(`fetchApi > baseUrl: ${baseUrl}, endpoint: ${endpoint}, options: ${JSON.stringify(options)}, queryParams: ${JSON.stringify(queryParams)}`)
|
|
4
|
+
let res: Response
|
|
5
|
+
baseUrl = (baseUrl.endsWith("/")) ? baseUrl : baseUrl + "/";
|
|
6
|
+
if(endpoint.startsWith("/")) endpoint = endpoint.slice(1)
|
|
7
|
+
let url = baseUrl + endpoint
|
|
8
|
+
|
|
9
|
+
if(queryParams) url = url + "?" + (new URLSearchParams(queryParams)).toString()
|
|
10
|
+
|
|
11
|
+
res = await fetch(url, options)
|
|
12
|
+
|
|
13
|
+
if (!res.ok && res.status >= 400) {
|
|
14
|
+
const errorMessage = await res.text()
|
|
15
|
+
if(res.status === 400) throw new Error(`400 Bad request: ${errorMessage}`);
|
|
16
|
+
if(res.status === 401) throw new Error(`401 Unauthorized: ${errorMessage}`);
|
|
17
|
+
if(res.status === 403) throw new Error(`403 Forbidden: ${res.statusText})`);
|
|
18
|
+
if(res.status === 404) throw new Error(`404 Resource not found: ${errorMessage}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return res
|
|
22
|
+
}
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export class Logger {
|
|
2
|
+
static log = (params: { level: string; data: any }) => {
|
|
3
|
+
console.log(`[${Date.now()}] [${params.level}] ${JSON.stringify(params.data)}`)
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
static debug = (data: any) => {
|
|
7
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
8
|
+
this.log({
|
|
9
|
+
level: 'DEBUG',
|
|
10
|
+
data,
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static info = (data: any) =>
|
|
16
|
+
this.log({
|
|
17
|
+
level: 'INFO',
|
|
18
|
+
data,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
static warn = (data: any) =>
|
|
22
|
+
this.log({
|
|
23
|
+
level: 'WARNING',
|
|
24
|
+
data,
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
static notice = (data: any) =>
|
|
28
|
+
this.log({
|
|
29
|
+
level: 'NOTICE',
|
|
30
|
+
data,
|
|
31
|
+
})
|
|
32
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ZodType, ZodTypeAny } from 'zod'
|
|
2
|
+
import { SafeParseReturnType, Schema } from 'zod/v3'
|
|
3
|
+
|
|
4
|
+
export class ValidationUtils {
|
|
5
|
+
static parse<T>(data: unknown, schema: ZodType<T>): T {
|
|
6
|
+
return schema.parse(data)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
static safeParse<T>(data: unknown, schema: ZodTypeAny): [T | null, any] {
|
|
10
|
+
const result = schema.safeParse(data)
|
|
11
|
+
return [result.success ? result.data as T : null, result.error]
|
|
12
|
+
}
|
|
13
|
+
}
|