@pgds/api-interface 1.4.1 → 1.4.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/client/HttpClient.js +5 -8
- package/client/HttpClient.js.map +1 -1
- package/lib/config.d.ts +1 -0
- package/lib/config.js +6 -0
- package/lib/config.js.map +1 -0
- package/package.json +2 -2
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -16
- package/dist/lib/constants/constants.d.ts +0 -2
- package/dist/lib/index.d.ts +0 -188
- package/dist/lib/index.js +0 -331
- package/dist/lib/models/Http.d.ts +0 -61
- package/dist/lib/models/User.d.ts +0 -8
- package/dist/lib/utils/jsonSchemaUtils.d.ts +0 -7
- package/dist/lib/utils/logger.d.ts +0 -4
- package/dist/lib/utils/metadataUtils.d.ts +0 -2
- package/dist/lib/utils/urlUtils.d.ts +0 -1
package/client/HttpClient.js
CHANGED
|
@@ -85,18 +85,20 @@ var axios_1 = __importStar(require("axios"));
|
|
|
85
85
|
var constants_1 = require("../constants");
|
|
86
86
|
var lib_1 = require("../lib");
|
|
87
87
|
var asyncHooks_1 = require("../lib/utils/asyncHooks");
|
|
88
|
+
var config_1 = require("../lib/config");
|
|
88
89
|
/**
|
|
89
90
|
* Base class for all types of apis. Provides basic http methods and error handling.
|
|
90
91
|
*/
|
|
91
92
|
var HttpClient = /** @class */ (function () {
|
|
92
93
|
function HttpClient(baseURL, timeout) {
|
|
93
|
-
if (timeout === void 0) { timeout =
|
|
94
|
+
if (timeout === void 0) { timeout = config_1.API_INTERFACE_HTTP_CLIENT_TIMEOUT; }
|
|
94
95
|
var _a;
|
|
95
96
|
this.baseURL = baseURL;
|
|
96
97
|
this.timeout = timeout;
|
|
98
|
+
this.timeout = timeout;
|
|
97
99
|
this.axiosInstance = (_a = axios_1.default.create) === null || _a === void 0 ? void 0 : _a.call(axios_1.default, {
|
|
98
100
|
baseURL: this.baseURL,
|
|
99
|
-
timeout:
|
|
101
|
+
timeout: timeout,
|
|
100
102
|
});
|
|
101
103
|
extractAPIErrorResponse(this.axiosInstance);
|
|
102
104
|
}
|
|
@@ -212,12 +214,6 @@ var HttpClient = /** @class */ (function () {
|
|
|
212
214
|
_m.label = 2;
|
|
213
215
|
case 2:
|
|
214
216
|
_m.trys.push([2, 6, , 7]);
|
|
215
|
-
if (timeout) {
|
|
216
|
-
this.setTimeout(timeout);
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
this.setTimeout(3000);
|
|
220
|
-
}
|
|
221
217
|
_d = [{}];
|
|
222
218
|
return [4 /*yield*/, this.getDefaultHeaders()];
|
|
223
219
|
case 3:
|
|
@@ -237,6 +233,7 @@ var HttpClient = /** @class */ (function () {
|
|
|
237
233
|
method: method,
|
|
238
234
|
url: path + queryString,
|
|
239
235
|
data: body,
|
|
236
|
+
timeout: timeout !== null && timeout !== void 0 ? timeout : this.timeout,
|
|
240
237
|
responseType: (responseType !== null && responseType !== void 0 ? responseType : "json"),
|
|
241
238
|
headers: headersToSend,
|
|
242
239
|
})];
|
package/client/HttpClient.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HttpClient.js","sourceRoot":"","sources":["HttpClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAAsF;AACtF,0CAAiD;AACjD,8BAAgC;AAChC,sDAAsD;
|
|
1
|
+
{"version":3,"file":"HttpClient.js","sourceRoot":"","sources":["HttpClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAAsF;AACtF,0CAAiD;AACjD,8BAAgC;AAChC,sDAAsD;AACtD,wCAAkE;AAiBlE;;GAEG;AACH;IAGE,oBAAsB,OAAe,EAAY,OAAmD;QAAnD,wBAAA,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|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.API_INTERFACE_HTTP_CLIENT_TIMEOUT = void 0;
|
|
5
|
+
exports.API_INTERFACE_HTTP_CLIENT_TIMEOUT = Number.parseInt((_a = process.env.HTTP_CLIENT_TIMEOUT) !== null && _a !== void 0 ? _a : "3000");
|
|
6
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["config.ts"],"names":[],"mappings":";;;;AAAa,QAAA,iCAAiC,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAA,OAAO,CAAC,GAAG,CAAC,mBAAmB,mCAAI,MAAM,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@pgds/api-interface",
|
|
3
3
|
"description": "Api interface for pgds services",
|
|
4
4
|
"authors": "Florry",
|
|
5
|
-
"version": "1.4.
|
|
5
|
+
"version": "1.4.2",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"types": "types/",
|
|
8
8
|
"scripts": {
|
|
@@ -98,4 +98,4 @@
|
|
|
98
98
|
"**/*.js.map"
|
|
99
99
|
]
|
|
100
100
|
}
|
|
101
|
-
}
|
|
101
|
+
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { setupApiInterface, JsonResponse, del, endpointMetadata, get, getRouteHelpers, head, options, patch, post, put, internal, Method } from "./lib";
|
|
2
|
-
import { Next, Request, Response } from "./lib/models/Http";
|
|
3
|
-
import { User } from "./lib/models/User";
|
|
4
|
-
export { endpointMetadata, del, get, getRouteHelpers, head, options, patch, post, put, internal, setupApiInterface, JsonResponse, Request, Response, Next, Method, User, };
|
package/dist/index.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Method = exports.setupApiInterface = exports.internal = exports.put = exports.post = exports.patch = exports.options = exports.head = exports.getRouteHelpers = exports.get = exports.del = exports.endpointMetadata = void 0;
|
|
4
|
-
var lib_1 = require("./lib");
|
|
5
|
-
Object.defineProperty(exports, "setupApiInterface", { enumerable: true, get: function () { return lib_1.setupApiInterface; } });
|
|
6
|
-
Object.defineProperty(exports, "del", { enumerable: true, get: function () { return lib_1.del; } });
|
|
7
|
-
Object.defineProperty(exports, "endpointMetadata", { enumerable: true, get: function () { return lib_1.endpointMetadata; } });
|
|
8
|
-
Object.defineProperty(exports, "get", { enumerable: true, get: function () { return lib_1.get; } });
|
|
9
|
-
Object.defineProperty(exports, "getRouteHelpers", { enumerable: true, get: function () { return lib_1.getRouteHelpers; } });
|
|
10
|
-
Object.defineProperty(exports, "head", { enumerable: true, get: function () { return lib_1.head; } });
|
|
11
|
-
Object.defineProperty(exports, "options", { enumerable: true, get: function () { return lib_1.options; } });
|
|
12
|
-
Object.defineProperty(exports, "patch", { enumerable: true, get: function () { return lib_1.patch; } });
|
|
13
|
-
Object.defineProperty(exports, "post", { enumerable: true, get: function () { return lib_1.post; } });
|
|
14
|
-
Object.defineProperty(exports, "put", { enumerable: true, get: function () { return lib_1.put; } });
|
|
15
|
-
Object.defineProperty(exports, "internal", { enumerable: true, get: function () { return lib_1.internal; } });
|
|
16
|
-
Object.defineProperty(exports, "Method", { enumerable: true, get: function () { return lib_1.Method; } });
|
package/dist/lib/index.d.ts
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
/// <reference path="../../types/hiot.d.ts" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import fs from "fs";
|
|
4
|
-
import App, { Locator } from "hiot";
|
|
5
|
-
import { RouteOptions } from "restify";
|
|
6
|
-
import { Next, Request, Response } from "./models/Http";
|
|
7
|
-
export declare const readDirAsync: typeof fs.readdir.__promisify__;
|
|
8
|
-
export declare enum Method {
|
|
9
|
-
Post = "post",
|
|
10
|
-
Get = "get",
|
|
11
|
-
Put = "put",
|
|
12
|
-
Patch = "patch",
|
|
13
|
-
Del = "del",
|
|
14
|
-
Head = "head",
|
|
15
|
-
Options = "options"
|
|
16
|
-
}
|
|
17
|
-
type OmittedFields = "internal" | "method";
|
|
18
|
-
export declare function get(options: Omit<EndpointOptions, OmittedFields>): {
|
|
19
|
-
(api: App): void;
|
|
20
|
-
/**
|
|
21
|
-
* Makes it possible to access the requestHandler through the exported function.
|
|
22
|
-
* If you need to test the function separately, without starting the server
|
|
23
|
-
*/
|
|
24
|
-
requestHandler: (req: Request<any, any>, res?: Response<any> | undefined, next?: Next | undefined) => Promise<void | JsonResponse<any>>;
|
|
25
|
-
/** Endpoint's full path (including any version number and internal prefix) */
|
|
26
|
-
PATH: string;
|
|
27
|
-
};
|
|
28
|
-
export declare function post(options: Omit<EndpointOptions, OmittedFields>): {
|
|
29
|
-
(api: App): void;
|
|
30
|
-
/**
|
|
31
|
-
* Makes it possible to access the requestHandler through the exported function.
|
|
32
|
-
* If you need to test the function separately, without starting the server
|
|
33
|
-
*/
|
|
34
|
-
requestHandler: (req: Request<any, any>, res?: Response<any> | undefined, next?: Next | undefined) => Promise<void | JsonResponse<any>>;
|
|
35
|
-
/** Endpoint's full path (including any version number and internal prefix) */
|
|
36
|
-
PATH: string;
|
|
37
|
-
};
|
|
38
|
-
export declare function put(options: Omit<EndpointOptions, OmittedFields>): {
|
|
39
|
-
(api: App): void;
|
|
40
|
-
/**
|
|
41
|
-
* Makes it possible to access the requestHandler through the exported function.
|
|
42
|
-
* If you need to test the function separately, without starting the server
|
|
43
|
-
*/
|
|
44
|
-
requestHandler: (req: Request<any, any>, res?: Response<any> | undefined, next?: Next | undefined) => Promise<void | JsonResponse<any>>;
|
|
45
|
-
/** Endpoint's full path (including any version number and internal prefix) */
|
|
46
|
-
PATH: string;
|
|
47
|
-
};
|
|
48
|
-
export declare function patch(options: Omit<EndpointOptions, OmittedFields>): {
|
|
49
|
-
(api: App): void;
|
|
50
|
-
/**
|
|
51
|
-
* Makes it possible to access the requestHandler through the exported function.
|
|
52
|
-
* If you need to test the function separately, without starting the server
|
|
53
|
-
*/
|
|
54
|
-
requestHandler: (req: Request<any, any>, res?: Response<any> | undefined, next?: Next | undefined) => Promise<void | JsonResponse<any>>;
|
|
55
|
-
/** Endpoint's full path (including any version number and internal prefix) */
|
|
56
|
-
PATH: string;
|
|
57
|
-
};
|
|
58
|
-
export declare function del(options: Omit<EndpointOptions, OmittedFields>): {
|
|
59
|
-
(api: App): void;
|
|
60
|
-
/**
|
|
61
|
-
* Makes it possible to access the requestHandler through the exported function.
|
|
62
|
-
* If you need to test the function separately, without starting the server
|
|
63
|
-
*/
|
|
64
|
-
requestHandler: (req: Request<any, any>, res?: Response<any> | undefined, next?: Next | undefined) => Promise<void | JsonResponse<any>>;
|
|
65
|
-
/** Endpoint's full path (including any version number and internal prefix) */
|
|
66
|
-
PATH: string;
|
|
67
|
-
};
|
|
68
|
-
export declare function head(options: Omit<EndpointOptions, OmittedFields>): {
|
|
69
|
-
(api: App): void;
|
|
70
|
-
/**
|
|
71
|
-
* Makes it possible to access the requestHandler through the exported function.
|
|
72
|
-
* If you need to test the function separately, without starting the server
|
|
73
|
-
*/
|
|
74
|
-
requestHandler: (req: Request<any, any>, res?: Response<any> | undefined, next?: Next | undefined) => Promise<void | JsonResponse<any>>;
|
|
75
|
-
/** Endpoint's full path (including any version number and internal prefix) */
|
|
76
|
-
PATH: string;
|
|
77
|
-
};
|
|
78
|
-
export declare function options(options: Omit<EndpointOptions, OmittedFields>): {
|
|
79
|
-
(api: App): void;
|
|
80
|
-
/**
|
|
81
|
-
* Makes it possible to access the requestHandler through the exported function.
|
|
82
|
-
* If you need to test the function separately, without starting the server
|
|
83
|
-
*/
|
|
84
|
-
requestHandler: (req: Request<any, any>, res?: Response<any> | undefined, next?: Next | undefined) => Promise<void | JsonResponse<any>>;
|
|
85
|
-
/** Endpoint's full path (including any version number and internal prefix) */
|
|
86
|
-
PATH: string;
|
|
87
|
-
};
|
|
88
|
-
/** Internal endpoints are not available through the API */
|
|
89
|
-
export declare function internal(options: Omit<EndpointOptions, "internal">): {
|
|
90
|
-
(api: App): void;
|
|
91
|
-
/**
|
|
92
|
-
* Makes it possible to access the requestHandler through the exported function.
|
|
93
|
-
* If you need to test the function separately, without starting the server
|
|
94
|
-
*/
|
|
95
|
-
requestHandler: (req: Request<any, any>, res?: Response<any> | undefined, next?: Next | undefined) => Promise<void | JsonResponse<any>>;
|
|
96
|
-
/** Endpoint's full path (including any version number and internal prefix) */
|
|
97
|
-
PATH: string;
|
|
98
|
-
};
|
|
99
|
-
export interface JsonResponse<T> {
|
|
100
|
-
/** Response body */
|
|
101
|
-
body?: T;
|
|
102
|
-
/** HTTP status code. @default 200 */
|
|
103
|
-
status?: number;
|
|
104
|
-
/** Additional headers */
|
|
105
|
-
headers?: {
|
|
106
|
-
totalcount?: number;
|
|
107
|
-
[key: string]: any;
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
export { Next, Request, Response };
|
|
111
|
-
export interface EndpointOptions {
|
|
112
|
-
/** Whether to add the endpoint to the metadata or not. @default true */
|
|
113
|
-
addToMetadata?: boolean;
|
|
114
|
-
/** Whether the endpoint is internal or not */
|
|
115
|
-
internal?: boolean;
|
|
116
|
-
/** The path to the endpoint */
|
|
117
|
-
path: string;
|
|
118
|
-
/** The activity that is required to access this endpoint */
|
|
119
|
-
authorized?: string;
|
|
120
|
-
/** Whether you need to be logged in or not to access the endpoint. Only needed if authorized is not used. */
|
|
121
|
-
authenticated?: boolean;
|
|
122
|
-
/** HTTP method */
|
|
123
|
-
method: Method;
|
|
124
|
-
/**
|
|
125
|
-
* The request schema, either a JSON schema or the name of a typescript type
|
|
126
|
-
* (See below. The name must match the name of the file in ./api/schemas).
|
|
127
|
-
**/
|
|
128
|
-
requestSchema?: object | string;
|
|
129
|
-
/** Additional route options */
|
|
130
|
-
opts?: RouteOptions;
|
|
131
|
-
/** prefix for the path, e.g. /v1, defaults to what was set in setupApiInterface */
|
|
132
|
-
version?: string;
|
|
133
|
-
/** The documentation for the endpoint */
|
|
134
|
-
documentation?: {
|
|
135
|
-
/** Short description of the endpoint */
|
|
136
|
-
summary: string;
|
|
137
|
-
/** Long description of the endpoint */
|
|
138
|
-
description?: string;
|
|
139
|
-
/** Optional array of tags */
|
|
140
|
-
tags?: string[];
|
|
141
|
-
/**
|
|
142
|
-
* Optional description for query params (can also be defined and described in the json schema,
|
|
143
|
-
* since that's how we validate them)
|
|
144
|
-
**/
|
|
145
|
-
query?: Record<string, string>;
|
|
146
|
-
/**
|
|
147
|
-
* Optional description of url parameters.
|
|
148
|
-
* Usually unnecessary since url params should be self descriptive, e.g. /dashboards/:dashboardId
|
|
149
|
-
**/
|
|
150
|
-
parameters?: Record<string, string>;
|
|
151
|
-
/** Response schemas */
|
|
152
|
-
responses?: {
|
|
153
|
-
/** HTTP status code */
|
|
154
|
-
status: number;
|
|
155
|
-
/** An optional description of the response */
|
|
156
|
-
description?: string;
|
|
157
|
-
/**
|
|
158
|
-
* An optional response schema, either a JSON schema or the name of a typescript type
|
|
159
|
-
* (See below. The name must match the name of the file in ./api/schemas).
|
|
160
|
-
**/
|
|
161
|
-
schema?: any | any[];
|
|
162
|
-
}[];
|
|
163
|
-
};
|
|
164
|
-
/** The request handler function */
|
|
165
|
-
requestHandler: (req: Request<any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
166
|
-
}
|
|
167
|
-
export type EndpointMetadata = Omit<EndpointOptions, "requestHandler" | "opts">;
|
|
168
|
-
export declare const endpointMetadata: {
|
|
169
|
-
[key in Method]: Record<string, EndpointMetadata>;
|
|
170
|
-
};
|
|
171
|
-
export declare function getRouteHelpers(): {
|
|
172
|
-
authenticated: () => App.RequestHandlerType;
|
|
173
|
-
authorized: (activity: string) => App.RequestHandlerType;
|
|
174
|
-
};
|
|
175
|
-
export interface ApiInterfaceOptions {
|
|
176
|
-
/** Name of the service */
|
|
177
|
-
serviceName: string;
|
|
178
|
-
/** Default log level for the service */
|
|
179
|
-
serviceLogLevel: string;
|
|
180
|
-
/** Whether to use typescript or not */
|
|
181
|
-
typescript: boolean;
|
|
182
|
-
/** Global version of the API @default v1. Can be overridden per endpoint */
|
|
183
|
-
apiVersion: string;
|
|
184
|
-
/** The base path to the schemas. @default api/schemas */
|
|
185
|
-
schemaBasePath?: string;
|
|
186
|
-
}
|
|
187
|
-
export declare function getApiOptions(): ApiInterfaceOptions;
|
|
188
|
-
export declare function setupApiInterface(options: ApiInterfaceOptions): (locator: Locator) => Promise<App.Locator>;
|
package/dist/lib/index.js
DELETED
|
@@ -1,331 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
-
if (k2 === undefined) k2 = k;
|
|
15
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
-
}
|
|
19
|
-
Object.defineProperty(o, k2, desc);
|
|
20
|
-
}) : (function(o, m, k, k2) {
|
|
21
|
-
if (k2 === undefined) k2 = k;
|
|
22
|
-
o[k2] = m[k];
|
|
23
|
-
}));
|
|
24
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
25
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
26
|
-
}) : function(o, v) {
|
|
27
|
-
o["default"] = v;
|
|
28
|
-
});
|
|
29
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
30
|
-
if (mod && mod.__esModule) return mod;
|
|
31
|
-
var result = {};
|
|
32
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
33
|
-
__setModuleDefault(result, mod);
|
|
34
|
-
return result;
|
|
35
|
-
};
|
|
36
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
37
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
38
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
39
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
40
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
41
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
42
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
46
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
47
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
48
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
49
|
-
function step(op) {
|
|
50
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
51
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
52
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
53
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
54
|
-
switch (op[0]) {
|
|
55
|
-
case 0: case 1: t = op; break;
|
|
56
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
57
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
58
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
59
|
-
default:
|
|
60
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
61
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
62
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
63
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
64
|
-
if (t[2]) _.ops.pop();
|
|
65
|
-
_.trys.pop(); continue;
|
|
66
|
-
}
|
|
67
|
-
op = body.call(thisArg, _);
|
|
68
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
69
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
73
|
-
var t = {};
|
|
74
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
75
|
-
t[p] = s[p];
|
|
76
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
77
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
78
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
79
|
-
t[p[i]] = s[p[i]];
|
|
80
|
-
}
|
|
81
|
-
return t;
|
|
82
|
-
};
|
|
83
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
84
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
85
|
-
if (ar || !(i in from)) {
|
|
86
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
87
|
-
ar[i] = from[i];
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
91
|
-
};
|
|
92
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
93
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
94
|
-
};
|
|
95
|
-
var _a;
|
|
96
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
97
|
-
exports.setupApiInterface = exports.getApiOptions = exports.getRouteHelpers = exports.endpointMetadata = exports.internal = exports.options = exports.head = exports.del = exports.patch = exports.put = exports.post = exports.get = exports.Method = exports.readDirAsync = void 0;
|
|
98
|
-
var fs_1 = __importDefault(require("fs"));
|
|
99
|
-
var hiot_restify5_1 = require("hiot-restify5");
|
|
100
|
-
var mustbe_1 = __importDefault(require("mustbe"));
|
|
101
|
-
var restify_errors_1 = __importDefault(require("restify-errors"));
|
|
102
|
-
var util_1 = require("util");
|
|
103
|
-
var constants_1 = require("./constants/constants");
|
|
104
|
-
var jsonSchemaUtils_1 = require("./utils/jsonSchemaUtils");
|
|
105
|
-
var logger_1 = __importStar(require("./utils/logger"));
|
|
106
|
-
var urlUtils_1 = require("./utils/urlUtils");
|
|
107
|
-
exports.readDirAsync = (0, util_1.promisify)(fs_1.default.readdir);
|
|
108
|
-
var Method;
|
|
109
|
-
(function (Method) {
|
|
110
|
-
Method["Post"] = "post";
|
|
111
|
-
Method["Get"] = "get";
|
|
112
|
-
Method["Put"] = "put";
|
|
113
|
-
Method["Patch"] = "patch";
|
|
114
|
-
Method["Del"] = "del";
|
|
115
|
-
Method["Head"] = "head";
|
|
116
|
-
Method["Options"] = "options";
|
|
117
|
-
})(Method || (exports.Method = Method = {}));
|
|
118
|
-
function get(options) {
|
|
119
|
-
return registerEndpoint(__assign(__assign({}, options), { method: Method.Get }));
|
|
120
|
-
}
|
|
121
|
-
exports.get = get;
|
|
122
|
-
function post(options) {
|
|
123
|
-
return registerEndpoint(__assign(__assign({}, options), { method: Method.Post }));
|
|
124
|
-
}
|
|
125
|
-
exports.post = post;
|
|
126
|
-
function put(options) {
|
|
127
|
-
return registerEndpoint(__assign(__assign({}, options), { method: Method.Put }));
|
|
128
|
-
}
|
|
129
|
-
exports.put = put;
|
|
130
|
-
function patch(options) {
|
|
131
|
-
return registerEndpoint(__assign(__assign({}, options), { method: Method.Patch }));
|
|
132
|
-
}
|
|
133
|
-
exports.patch = patch;
|
|
134
|
-
function del(options) {
|
|
135
|
-
return registerEndpoint(__assign(__assign({}, options), { method: Method.Del }));
|
|
136
|
-
}
|
|
137
|
-
exports.del = del;
|
|
138
|
-
function head(options) {
|
|
139
|
-
return registerEndpoint(__assign(__assign({}, options), { method: Method.Head }));
|
|
140
|
-
}
|
|
141
|
-
exports.head = head;
|
|
142
|
-
function options(options) {
|
|
143
|
-
return registerEndpoint(__assign(__assign({}, options), { method: Method.Options }));
|
|
144
|
-
}
|
|
145
|
-
exports.options = options;
|
|
146
|
-
/** Internal endpoints are not available through the API */
|
|
147
|
-
function internal(options) {
|
|
148
|
-
return registerEndpoint(__assign(__assign({}, options), { internal: true }));
|
|
149
|
-
}
|
|
150
|
-
exports.internal = internal;
|
|
151
|
-
exports.endpointMetadata = (_a = {},
|
|
152
|
-
_a[Method.Post] = {},
|
|
153
|
-
_a[Method.Get] = {},
|
|
154
|
-
_a[Method.Put] = {},
|
|
155
|
-
_a[Method.Patch] = {},
|
|
156
|
-
_a[Method.Del] = {},
|
|
157
|
-
_a[Method.Head] = {},
|
|
158
|
-
_a.options = {},
|
|
159
|
-
_a);
|
|
160
|
-
var metadataRegistered = false;
|
|
161
|
-
function registerMetadataEndpoint(api) {
|
|
162
|
-
var _this = this;
|
|
163
|
-
if (metadataRegistered) {
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
metadataRegistered = true;
|
|
167
|
-
registerEndpoint({
|
|
168
|
-
path: "/".concat(apiOptions.serviceName, "/metadata"),
|
|
169
|
-
method: Method.Get,
|
|
170
|
-
addToMetadata: false,
|
|
171
|
-
version: "",
|
|
172
|
-
requestHandler: function () { return __awaiter(_this, void 0, void 0, function () {
|
|
173
|
-
return __generator(this, function (_a) {
|
|
174
|
-
// prepare metadata here so that we get schemas and not just ids in the response
|
|
175
|
-
return [2 /*return*/, {
|
|
176
|
-
body: exports.endpointMetadata,
|
|
177
|
-
}];
|
|
178
|
-
});
|
|
179
|
-
}); },
|
|
180
|
-
})(api);
|
|
181
|
-
}
|
|
182
|
-
var mustBe;
|
|
183
|
-
function getRouteHelpers() {
|
|
184
|
-
if (!mustBe) {
|
|
185
|
-
mustBe = mustbe_1.default.routeHelpers();
|
|
186
|
-
}
|
|
187
|
-
return mustBe;
|
|
188
|
-
}
|
|
189
|
-
exports.getRouteHelpers = getRouteHelpers;
|
|
190
|
-
function registerEndpoint(options) {
|
|
191
|
-
var _this = this;
|
|
192
|
-
var _a;
|
|
193
|
-
var requestHandler = options.requestHandler, opts = options.opts, _b = options.addToMetadata, addToMetadata = _b === void 0 ? true : _b, metadata = __rest(options, ["requestHandler", "opts", "addToMetadata"]);
|
|
194
|
-
if (!constants_1.CI) {
|
|
195
|
-
if (addToMetadata) {
|
|
196
|
-
exports.endpointMetadata[options.method][options.path] = metadata;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
var fn = function (api) {
|
|
200
|
-
// registerMetadataEndpoint(api);
|
|
201
|
-
var _a;
|
|
202
|
-
var mustBe = getRouteHelpers();
|
|
203
|
-
var routeOptions = {
|
|
204
|
-
path: (0, urlUtils_1.route)((_a = options.version) !== null && _a !== void 0 ? _a : apiVersion, options.path, options.internal),
|
|
205
|
-
};
|
|
206
|
-
if (options.opts) {
|
|
207
|
-
routeOptions = __assign(__assign({}, routeOptions), options.opts);
|
|
208
|
-
}
|
|
209
|
-
var requestHandlers = [];
|
|
210
|
-
if (options.authenticated) {
|
|
211
|
-
requestHandlers.push(mustBe.authenticated());
|
|
212
|
-
}
|
|
213
|
-
if (options.authorized) {
|
|
214
|
-
requestHandlers.push(mustBe.authorized(options.authorized));
|
|
215
|
-
}
|
|
216
|
-
if (options.requestSchema) {
|
|
217
|
-
var schema = options.requestSchema;
|
|
218
|
-
if (typeof schema === "string") {
|
|
219
|
-
schema = (0, jsonSchemaUtils_1.getSchema)(schema);
|
|
220
|
-
}
|
|
221
|
-
try {
|
|
222
|
-
var validatedSchema = (0, hiot_restify5_1.validate)(schema);
|
|
223
|
-
requestHandlers.push(validatedSchema);
|
|
224
|
-
}
|
|
225
|
-
catch (err) {
|
|
226
|
-
console.error(err);
|
|
227
|
-
throw err;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
var responseSchemas = [];
|
|
231
|
-
if (constants_1.CI) {
|
|
232
|
-
responseSchemas = (0, jsonSchemaUtils_1.prepareResponseSchemas)(options);
|
|
233
|
-
}
|
|
234
|
-
requestHandlers.push(function (req, res, next) { return __awaiter(_this, void 0, void 0, function () {
|
|
235
|
-
var response, header, err_1;
|
|
236
|
-
var _a, _b;
|
|
237
|
-
return __generator(this, function (_c) {
|
|
238
|
-
switch (_c.label) {
|
|
239
|
-
case 0:
|
|
240
|
-
_c.trys.push([0, 2, , 3]);
|
|
241
|
-
return [4 /*yield*/, options.requestHandler(req, res, next)];
|
|
242
|
-
case 1:
|
|
243
|
-
response = _c.sent();
|
|
244
|
-
if (response) {
|
|
245
|
-
res.statusCode = (_a = response.status) !== null && _a !== void 0 ? _a : 200;
|
|
246
|
-
if (response.headers) {
|
|
247
|
-
for (header in response.headers) {
|
|
248
|
-
res.header(header, response.headers[header]);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
if (response.body) {
|
|
252
|
-
if (constants_1.CI && !!responseSchemas.length) {
|
|
253
|
-
(0, jsonSchemaUtils_1.validateResponseBody)(responseSchemas, response.body);
|
|
254
|
-
}
|
|
255
|
-
res.json(response.body);
|
|
256
|
-
}
|
|
257
|
-
else {
|
|
258
|
-
res.end();
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
return [2 /*return*/, next()];
|
|
262
|
-
case 2:
|
|
263
|
-
err_1 = _c.sent();
|
|
264
|
-
if ((_b = err_1 === null || err_1 === void 0 ? void 0 : err_1.body) === null || _b === void 0 ? void 0 : _b.code) {
|
|
265
|
-
if (constants_1.CI) {
|
|
266
|
-
console.error(err_1.toString());
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
logger_1.default.error(err_1);
|
|
270
|
-
}
|
|
271
|
-
return [2 /*return*/, next(err_1)];
|
|
272
|
-
}
|
|
273
|
-
if (constants_1.CI) {
|
|
274
|
-
console.error(err_1.toString ? err_1.toString() : err_1);
|
|
275
|
-
}
|
|
276
|
-
else {
|
|
277
|
-
logger_1.default.error(err_1);
|
|
278
|
-
}
|
|
279
|
-
return [2 /*return*/, next(new restify_errors_1.default.InternalError(err_1))];
|
|
280
|
-
case 3: return [2 /*return*/];
|
|
281
|
-
}
|
|
282
|
-
});
|
|
283
|
-
}); });
|
|
284
|
-
var pendingRoute = {
|
|
285
|
-
method: options.method,
|
|
286
|
-
opts: routeOptions,
|
|
287
|
-
handlers: requestHandlers,
|
|
288
|
-
};
|
|
289
|
-
// @ts-ignore
|
|
290
|
-
api[pendingRoute.method].apply(api, __spreadArray([pendingRoute.opts], pendingRoute.handlers, false));
|
|
291
|
-
};
|
|
292
|
-
/**
|
|
293
|
-
* Makes it possible to access the requestHandler through the exported function.
|
|
294
|
-
* If you need to test the function separately, without starting the server
|
|
295
|
-
*/
|
|
296
|
-
fn.requestHandler = options.requestHandler;
|
|
297
|
-
/** Endpoint's full path (including any version number and internal prefix) */
|
|
298
|
-
fn.PATH = (0, urlUtils_1.route)((_a = options.version) !== null && _a !== void 0 ? _a : apiVersion, options.path, options.internal);
|
|
299
|
-
return fn;
|
|
300
|
-
}
|
|
301
|
-
var apiVersion = "v1";
|
|
302
|
-
var apiOptions = {};
|
|
303
|
-
function getApiOptions() {
|
|
304
|
-
return apiOptions;
|
|
305
|
-
}
|
|
306
|
-
exports.getApiOptions = getApiOptions;
|
|
307
|
-
function setupApiInterface(options) {
|
|
308
|
-
var _this = this;
|
|
309
|
-
apiOptions = options;
|
|
310
|
-
return function (locator) { return __awaiter(_this, void 0, void 0, function () {
|
|
311
|
-
var files;
|
|
312
|
-
var _a;
|
|
313
|
-
return __generator(this, function (_b) {
|
|
314
|
-
switch (_b.label) {
|
|
315
|
-
case 0:
|
|
316
|
-
if (!(options.typescript &&
|
|
317
|
-
/** This check is only applicable during tests, so it's only run once */
|
|
318
|
-
jsonSchemaUtils_1.schemaCache.size === 0)) return [3 /*break*/, 2];
|
|
319
|
-
return [4 /*yield*/, (0, exports.readDirAsync)((_a = options.schemaBasePath) !== null && _a !== void 0 ? _a : constants_1.SCHEMA_BASE_PATH)];
|
|
320
|
-
case 1:
|
|
321
|
-
files = _b.sent();
|
|
322
|
-
(0, jsonSchemaUtils_1.getSchemas)(files.map(function (file) { return file.replace(".ts", ""); }));
|
|
323
|
-
_b.label = 2;
|
|
324
|
-
case 2:
|
|
325
|
-
(0, logger_1.setupLogger)(options.serviceName, options.serviceLogLevel);
|
|
326
|
-
return [2 /*return*/, locator];
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
|
-
}); };
|
|
330
|
-
}
|
|
331
|
-
exports.setupApiInterface = setupApiInterface;
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { User } from "./User";
|
|
2
|
-
import { Request as RestifyRequest, Response as RestifyResponse, Next as RestifyNext } from "restify";
|
|
3
|
-
export interface Request<BodyParams = Record<string, any>, QueryParams = any> extends RestifyRequest {
|
|
4
|
-
params: BodyParams;
|
|
5
|
-
user: User;
|
|
6
|
-
query: QueryParams;
|
|
7
|
-
}
|
|
8
|
-
export interface Next extends RestifyNext {
|
|
9
|
-
(err?: any): void;
|
|
10
|
-
}
|
|
11
|
-
export interface Response<Body = Record<string, any>> extends RestifyResponse {
|
|
12
|
-
/**
|
|
13
|
-
* short hand method for:
|
|
14
|
-
* res.contentType = 'json';
|
|
15
|
-
* res.send({hello: 'world'});
|
|
16
|
-
* @param code http status code
|
|
17
|
-
* @param body value to json.stringify
|
|
18
|
-
* @param [headers] headers to set on the response
|
|
19
|
-
*/
|
|
20
|
-
json(code: number, body: Body, headers?: {
|
|
21
|
-
[header: string]: string;
|
|
22
|
-
}): any;
|
|
23
|
-
/**
|
|
24
|
-
* short hand method for:
|
|
25
|
-
* res.contentType = 'json';
|
|
26
|
-
* res.send({hello: 'world'});
|
|
27
|
-
* @param code http status code
|
|
28
|
-
*/
|
|
29
|
-
json(code: number): any;
|
|
30
|
-
/**
|
|
31
|
-
* short hand method for:
|
|
32
|
-
* res.contentType = 'json';
|
|
33
|
-
* res.send({hello: 'world'});
|
|
34
|
-
* @param body value to json.stringify
|
|
35
|
-
* @param [headers] headers to set on the response
|
|
36
|
-
*/
|
|
37
|
-
json(body: Body, headers?: {
|
|
38
|
-
[header: string]: string;
|
|
39
|
-
}): any;
|
|
40
|
-
/**
|
|
41
|
-
* sends the response object. pass through to internal __send that uses a
|
|
42
|
-
* formatter based on the content-type header.
|
|
43
|
-
* @param [code] http status code
|
|
44
|
-
* @param [body] the content to send
|
|
45
|
-
* @param [headers] any add'l headers to set
|
|
46
|
-
* @returns the response object
|
|
47
|
-
*/
|
|
48
|
-
send(code?: number, body?: Body, headers?: {
|
|
49
|
-
[header: string]: string;
|
|
50
|
-
}): any;
|
|
51
|
-
/**
|
|
52
|
-
* sends the response object. pass through to internal __send that uses a
|
|
53
|
-
* formatter based on the content-type header.
|
|
54
|
-
* @param [body] the content to send
|
|
55
|
-
* @param [headers] any add'l headers to set
|
|
56
|
-
* @returns the response object
|
|
57
|
-
*/
|
|
58
|
-
send(body?: Body, headers?: {
|
|
59
|
-
[header: string]: string;
|
|
60
|
-
}): any;
|
|
61
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { EndpointOptions } from "..";
|
|
2
|
-
/** Speeds up loading schemas when testing by only loading the schemas once instead of once per test file */
|
|
3
|
-
export declare const schemaCache: Map<string, object>;
|
|
4
|
-
export declare function getSchemas(schemaNames: string[]): void;
|
|
5
|
-
export declare function getSchema(schemaName: string): object | undefined;
|
|
6
|
-
export declare function prepareResponseSchemas(options: EndpointOptions): object[];
|
|
7
|
-
export declare function validateResponseBody(schemas: object[], body: object): void;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function route(version: string, path: string, internal?: boolean): string;
|