@metarisc/metarisc-js 0.0.1-alpha.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.
package/.eslintrc ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "root": true,
3
+ "parser": "@typescript-eslint/parser",
4
+ "plugins": [
5
+ "@typescript-eslint"
6
+ ],
7
+ "extends": [
8
+ "eslint:recommended",
9
+ "plugin:@typescript-eslint/eslint-recommended",
10
+ "plugin:@typescript-eslint/recommended"
11
+ ]
12
+ }
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Metarisc JS
2
+
3
+ La librairie Metarisc JS offre un accès simple et pratique à l'API Metarisc à partir d'applications écrites en langage JS. Elle comprend un ensemble de classes et de fonctions pour l'ensemble des ressources de l'API.
package/lib/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { Metarisc } from './metarisc';
package/lib/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Metarisc = void 0;
4
+ var metarisc_1 = require("./metarisc");
5
+ Object.defineProperty(exports, "Metarisc", { enumerable: true, get: function () { return metarisc_1.Metarisc; } });
@@ -0,0 +1,19 @@
1
+ import { AxiosResponse } from "axios";
2
+ interface RequestConfig {
3
+ body?: any;
4
+ headers?: string | {
5
+ [name: string]: string | string[];
6
+ };
7
+ params?: {
8
+ [param: string]: string | string[];
9
+ };
10
+ endpoint?: string;
11
+ method?: string;
12
+ }
13
+ export declare class Metarisc {
14
+ private axios;
15
+ constructor();
16
+ request<T>(config: RequestConfig): Promise<AxiosResponse<T>>;
17
+ autoPagingIterator<T>(config: RequestConfig): AsyncGenerator<T, void, unknown>;
18
+ }
19
+ export {};
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Metarisc = void 0;
4
+ const axios_1 = require("axios");
5
+ class Metarisc {
6
+ constructor() {
7
+ this.axios = axios_1.default.create({
8
+ baseURL: 'https://api.metarisc.fr/'
9
+ });
10
+ }
11
+ async request(config) {
12
+ return this.axios.request({
13
+ method: config.method || 'GET',
14
+ url: config.endpoint || '/',
15
+ params: config.params,
16
+ data: config.body
17
+ });
18
+ }
19
+ async *autoPagingIterator(config) {
20
+ let current_page = config.params && 'page' in config.params ? parseInt(config.params['page'].toString()) : 1;
21
+ const per_page = config.params && 'per_page' in config.params ? parseInt(config.params['per_page'].toString()) : 25;
22
+ while (true) {
23
+ const response = await this.request({ ...config, ...{ params: { page: current_page.toString(), per_page: per_page.toString() } } });
24
+ for (const element of response.data.data) {
25
+ yield element;
26
+ }
27
+ if (response.data.meta.pagination.current_page === response.data.meta.pagination.total_pages) {
28
+ break;
29
+ }
30
+ current_page++;
31
+ }
32
+ }
33
+ }
34
+ exports.Metarisc = Metarisc;
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@metarisc/metarisc-js",
3
+ "main": "lib/index.js",
4
+ "types": "lib/index.d.ts",
5
+ "version": "0.0.1-alpha.1",
6
+ "scripts": {
7
+ "lint": "eslint . --ext .ts",
8
+ "compile": "tsc"
9
+ },
10
+ "devDependencies": {
11
+ "@types/node": "^18.14.4",
12
+ "@typescript-eslint/eslint-plugin": "^5.54.0",
13
+ "@typescript-eslint/parser": "^5.54.0",
14
+ "eslint": "^8.35.0",
15
+ "typescript": "^4.9.5"
16
+ },
17
+ "dependencies": {
18
+ "axios": "^1.4.0"
19
+ }
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { Metarisc } from './metarisc';
@@ -0,0 +1,62 @@
1
+ import axios, { AxiosInstance, AxiosResponse } from "axios";
2
+
3
+ interface RequestConfig {
4
+ body ?: any,
5
+ headers ?: string | {[name: string]: string | string[]},
6
+ params ?: {[param: string]: string | string[]},
7
+ endpoint ?: string,
8
+ method ?: string
9
+ }
10
+
11
+ export class Metarisc
12
+ {
13
+ private axios : AxiosInstance;
14
+
15
+ constructor()
16
+ {
17
+ this.axios = axios.create({
18
+ baseURL: 'https://api.metarisc.fr/'
19
+ });
20
+ }
21
+
22
+ async request<T>(config : RequestConfig) : Promise<AxiosResponse<T>>
23
+ {
24
+ return this.axios.request<T>({
25
+ method: config.method || 'GET',
26
+ url: config.endpoint || '/',
27
+ params: config.params,
28
+ data: config.body
29
+ });
30
+ }
31
+
32
+ async * autoPagingIterator<T>(config : RequestConfig) : AsyncGenerator<T, void, unknown>
33
+ {
34
+ let current_page : number = config.params && 'page' in config.params ? parseInt(config.params['page'].toString()) : 1;
35
+ const per_page : number = config.params && 'per_page' in config.params ? parseInt(config.params['per_page'].toString()) : 25;
36
+
37
+ while (true) {
38
+ const response = <AxiosResponse<{
39
+ data?: Array<T>;
40
+ meta?: {
41
+ pagination?: {
42
+ total?: number;
43
+ count?: number;
44
+ per_page?: number;
45
+ current_page?: number;
46
+ total_pages?: number;
47
+ };
48
+ };
49
+ }>> await this.request({...config, ...{params: {page: current_page.toString(), per_page: per_page.toString()}}});
50
+
51
+ for (const element of response.data.data) {
52
+ yield element;
53
+ }
54
+
55
+ if (response.data.meta.pagination.current_page === response.data.meta.pagination.total_pages) {
56
+ break;
57
+ }
58
+
59
+ current_page++;
60
+ }
61
+ }
62
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./lib",
4
+ "module": "commonjs",
5
+ "target": "es2020",
6
+ "declaration": true
7
+ },
8
+ "include": [
9
+ "src/**/*"
10
+ ]
11
+ }