@pgds/api-interface 1.2.5
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/README.md +410 -0
- package/client/FairtrailClient.d.ts +38 -0
- package/client/FairtrailClient.js +219 -0
- package/client/FairtrailClient.js.map +1 -0
- package/client/HttpClient.d.ts +36 -0
- package/client/HttpClient.js +360 -0
- package/client/HttpClient.js.map +1 -0
- package/client/index.d.ts +3 -0
- package/client/index.js +8 -0
- package/client/index.js.map +1 -0
- package/constants/index.d.ts +8 -0
- package/constants/index.js +13 -0
- package/constants/index.js.map +1 -0
- package/errors/index.d.ts +1 -0
- package/errors/index.js +72 -0
- package/errors/index.js.map +1 -0
- package/index.d.ts +7 -0
- package/index.js +23 -0
- package/lib/auth/mustbeConfig.d.ts +13 -0
- package/lib/auth/mustbeConfig.js +167 -0
- package/lib/auth/mustbeConfig.js.map +1 -0
- package/lib/constants/constants.d.ts +3 -0
- package/lib/constants/constants.js +8 -0
- package/lib/constants/constants.js.map +1 -0
- package/lib/index.d.ts +255 -0
- package/lib/index.js +462 -0
- package/lib/index.js.map +1 -0
- package/lib/models/Http.d.ts +62 -0
- package/lib/models/Http.js +3 -0
- package/lib/models/Http.js.map +1 -0
- package/lib/models/ReqUser.d.ts +10 -0
- package/lib/models/ReqUser.js +3 -0
- package/lib/models/ReqUser.js.map +1 -0
- package/lib/server/server.d.ts +13 -0
- package/lib/server/server.js +154 -0
- package/lib/server/server.js.map +1 -0
- package/lib/utils/asyncHooks.d.ts +28 -0
- package/lib/utils/asyncHooks.js +99 -0
- package/lib/utils/asyncHooks.js.map +1 -0
- package/lib/utils/jsonSchemaUtils.d.ts +14 -0
- package/lib/utils/jsonSchemaUtils.js +187 -0
- package/lib/utils/jsonSchemaUtils.js.map +1 -0
- package/lib/utils/logger.d.ts +5 -0
- package/lib/utils/logger.js +33 -0
- package/lib/utils/logger.js.map +1 -0
- package/lib/utils/urlUtils.d.ts +1 -0
- package/lib/utils/urlUtils.js +14 -0
- package/lib/utils/urlUtils.js.map +1 -0
- package/package.json +104 -0
- package/types/hiot.d.ts +368 -0
- package/types/mustbe/config/activities.d.ts +11 -0
- package/types/mustbe/config/index.d.ts +77 -0
- package/types/mustbe/config/parameter-map.d.ts +8 -0
- package/types/mustbe/config/route-helpers.d.ts +17 -0
- package/types/mustbe/config/user-identity.d.ts +7 -0
- package/types/mustbe/core.d.ts +23 -0
- package/types/mustbe/identities/userIdentity.d.ts +10 -0
- package/types/mustbe/index.d.ts +6 -0
- package/types/mustbe/principals/index.d.ts +10 -0
- package/types/mustbe/registry.d.ts +11 -0
- package/types/mustbe/routeHelpers/index.d.ts +20 -0
- package/types/mustbe/verifier/index.d.ts +10 -0
- package/utils/index.d.ts +13 -0
- package/utils/index.js +32 -0
- package/utils/index.js.map +1 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.routeHelpers = void 0;
|
|
40
|
+
exports.configureMustBe = configureMustBe;
|
|
41
|
+
exports.isUserAuthorized = isUserAuthorized;
|
|
42
|
+
var constants_1 = require("../../constants");
|
|
43
|
+
var restifyErrors = __importStar(require("restify-errors"));
|
|
44
|
+
var jsonSchemaUtils_1 = require("../utils/jsonSchemaUtils");
|
|
45
|
+
var logger_1 = __importDefault(require("../utils/logger"));
|
|
46
|
+
var mustbe = require("mustbe");
|
|
47
|
+
/** Only needed if used along side legacy setup */
|
|
48
|
+
var routeHelpers = (function () {
|
|
49
|
+
var routeHelpers = mustbe.routeHelpers.bind(mustbe)();
|
|
50
|
+
return {
|
|
51
|
+
authorized: routeHelpers.authorized.bind(routeHelpers),
|
|
52
|
+
authenticated: routeHelpers.authenticated.bind(routeHelpers),
|
|
53
|
+
requestSchema: function (schema) { return (0, jsonSchemaUtils_1.jsonSchemaValidationMiddleware)(schema, jsonSchemaUtils_1.RequestKey.Body); },
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
exports.routeHelpers = routeHelpers;
|
|
57
|
+
function configureMustBe() {
|
|
58
|
+
mustbe.configure(function (config /* Configurator */) {
|
|
59
|
+
config.routeHelpers(function (rh) {
|
|
60
|
+
rh.getUser(function (req, cb) {
|
|
61
|
+
try {
|
|
62
|
+
var user = parseUser(req);
|
|
63
|
+
cb(null, user);
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
return cb(err, null);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
rh.notAuthorized(function (_req, _res, next) { return next(new restifyErrors.ResourceNotFoundError()); });
|
|
70
|
+
rh.notAuthenticated(function (_req, _res, next) { return next(new restifyErrors.InvalidCredentialsError()); });
|
|
71
|
+
});
|
|
72
|
+
config.activities(function (activities) {
|
|
73
|
+
activities.allow(function (identity, activity, next) {
|
|
74
|
+
var user = identity.user;
|
|
75
|
+
if (userIsUndefined(user)) {
|
|
76
|
+
return next(new restifyErrors.InvalidCredentialsError(), false);
|
|
77
|
+
}
|
|
78
|
+
if (user === null || user === void 0 ? void 0 : user.userId) {
|
|
79
|
+
if (isUserAuthorized(user.activities, activity)) {
|
|
80
|
+
return next(null, true);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return next(new restifyErrors.ForbiddenError("not authorized for activity: ".concat(activity)), false);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
config.userIdentity(function (id) { return id.isAuthenticated(function (user, cb) { return cb(null, !!user.userId); }); });
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function parseUser(req) {
|
|
90
|
+
var userId = req.header(constants_1.AUTH_USER_HEADER, undefined);
|
|
91
|
+
var organizationId = req.header(constants_1.AUTH_ORGANIZATION_HEADER, undefined);
|
|
92
|
+
var childOrganizationIds = req
|
|
93
|
+
.header(constants_1.AUTH_CHILD_ORGANIZATIONS_HEADER, "")
|
|
94
|
+
.split(",")
|
|
95
|
+
.filter(function (id) { return id !== ""; });
|
|
96
|
+
var activities = parseActivities(req.header(constants_1.AUTH_ACTIVITIES_HEADER, ""));
|
|
97
|
+
var activeLogin = req.header(constants_1.AUTH_ACTIVE_LOGIN_HEADER, undefined);
|
|
98
|
+
var user = {
|
|
99
|
+
userId: userId,
|
|
100
|
+
organizationId: organizationId,
|
|
101
|
+
childOrganizationIds: childOrganizationIds,
|
|
102
|
+
activities: activities,
|
|
103
|
+
activeLogin: activeLogin,
|
|
104
|
+
};
|
|
105
|
+
// @ts-ignore
|
|
106
|
+
req.user = user;
|
|
107
|
+
return user;
|
|
108
|
+
}
|
|
109
|
+
function userIsUndefined(user) {
|
|
110
|
+
if (!user) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
var userId = user.userId, organizationId = user.organizationId, activeLogin = user.activeLogin, childOrganizationIds = user.childOrganizationIds;
|
|
114
|
+
if (!userId && !organizationId && !activeLogin && !(childOrganizationIds === null || childOrganizationIds === void 0 ? void 0 : childOrganizationIds.length)) {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
function parseActivities(header) {
|
|
120
|
+
if (!header) {
|
|
121
|
+
return {};
|
|
122
|
+
}
|
|
123
|
+
var activities = header.split(",").reduce(function (acc, activity) {
|
|
124
|
+
acc[activity] = true;
|
|
125
|
+
return acc;
|
|
126
|
+
}, {});
|
|
127
|
+
return activities;
|
|
128
|
+
}
|
|
129
|
+
function isUserAuthorized(activities, requiredActivitiesString) {
|
|
130
|
+
var _a;
|
|
131
|
+
if (activities.global) {
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
var requiredActivities = Array.isArray(requiredActivitiesString)
|
|
135
|
+
? requiredActivitiesString
|
|
136
|
+
: requiredActivitiesString.split(",");
|
|
137
|
+
var isAuthorized = requiredActivities.every(function (requiredActivity) {
|
|
138
|
+
return isActivityAuthorized(activities, requiredActivity);
|
|
139
|
+
});
|
|
140
|
+
if (!isAuthorized) {
|
|
141
|
+
// TODO: move this to utils/index.ts instead to not have dependency to this file in utils/index.ts
|
|
142
|
+
(_a = logger_1.default === null || logger_1.default === void 0 ? void 0 : logger_1.default.debug) === null || _a === void 0 ? void 0 : _a.call(logger_1.default, "user is not authorized", { activities: activities, requiredActivities: requiredActivities });
|
|
143
|
+
}
|
|
144
|
+
return isAuthorized;
|
|
145
|
+
}
|
|
146
|
+
function isActivityAuthorized(activities, requiredActivity) {
|
|
147
|
+
if (activities[requiredActivity]) {
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
var isAuthorized = Object.keys(activities).some(function (activity) {
|
|
151
|
+
var isWildcard = (requiredActivity.includes("*") || activity.includes("*")) && !requiredActivity.includes(constants_1.GLOBAL_ACTIVITY);
|
|
152
|
+
return isWildcard ? isMatchingWildcard(activity, requiredActivity) : false;
|
|
153
|
+
});
|
|
154
|
+
return isAuthorized;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Matches two activity against each other with two way wildcard matching.
|
|
158
|
+
*/
|
|
159
|
+
function isMatchingWildcard(activity, activity2) {
|
|
160
|
+
var wildcardActivity = activity.replace("*", ".*");
|
|
161
|
+
var wildcardActivity2 = activity2.replace("*", ".*");
|
|
162
|
+
var activityRegex = new RegExp("^".concat(wildcardActivity, "$"));
|
|
163
|
+
var activity2Regex = new RegExp("^".concat(wildcardActivity2, "$"));
|
|
164
|
+
var isMatching = activityRegex.test(activity2) || activity2Regex.test(activity);
|
|
165
|
+
return isMatching;
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=mustbeConfig.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mustbeConfig.js","sourceRoot":"","sources":["mustbeConfig.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,0CAqCC;AAqDD,4CAmBC;AApJD,6CAOyB;AACzB,4DAAgD;AAKhD,4DAAsF;AACtF,2DAAqC;AAErC,IAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAUjC,kDAAkD;AAClD,IAAM,YAAY,GAAG,CA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AACtB,CAAC;AAED;;GAEG;AACH,SAAS,kBAAkB,CAAC,QAAgB,EAAE,SAAiB;IAC7D,IAAM,gBAAgB,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IACrD,IAAM,iBAAiB,GAAG,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IAEvD,IAAM,aAAa,GAAG,IAAI,MAAM,CAAC,WAAI,gBAAgB,MAAG,CAAC,CAAC;IAC1D,IAAM,cAAc,GAAG,IAAI,MAAM,CAAC,WAAI,iBAAiB,MAAG,CAAC,CAAC;IAE5D,IAAM,UAAU,GAAG,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,cAAc,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAElF,OAAO,UAAU,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.INTERNAL_ENDPOINT_PREFIX = exports.SCHEMA_BASE_PATH = exports.CI = void 0;
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
exports.CI = typeof global.it === "function";
|
|
6
|
+
exports.SCHEMA_BASE_PATH = "api/schemas";
|
|
7
|
+
exports.INTERNAL_ENDPOINT_PREFIX = "/internal";
|
|
8
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["constants.ts"],"names":[],"mappings":";;;AAAA,aAAa;AACA,QAAA,EAAE,GAAG,OAAO,MAAM,CAAC,EAAE,KAAK,UAAU,CAAC;AAErC,QAAA,gBAAgB,GAAG,aAAa,CAAC;AAEjC,QAAA,wBAAwB,GAAG,WAAW,CAAC"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { Db } from "mongodb";
|
|
2
|
+
import type { RouteOptions, Server } from "restify";
|
|
3
|
+
import type { Next, Request, Response } from "./models/Http";
|
|
4
|
+
interface Locator {
|
|
5
|
+
db: Db;
|
|
6
|
+
broker: any;
|
|
7
|
+
api: Server;
|
|
8
|
+
}
|
|
9
|
+
export declare enum Method {
|
|
10
|
+
Post = "post",
|
|
11
|
+
Get = "get",
|
|
12
|
+
Put = "put",
|
|
13
|
+
Patch = "patch",
|
|
14
|
+
Del = "del",
|
|
15
|
+
Head = "head",
|
|
16
|
+
Options = "options"
|
|
17
|
+
}
|
|
18
|
+
type OmittedFields = "internal" | "method";
|
|
19
|
+
export declare function get(options: Omit<EndpointOptions, OmittedFields>): {
|
|
20
|
+
(legacyServer?: Server): void;
|
|
21
|
+
/**
|
|
22
|
+
* Makes it possible to access the requestHandler through the exported function.
|
|
23
|
+
* If you need to test the function separately, without starting the server
|
|
24
|
+
*/
|
|
25
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
26
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
27
|
+
PATH: string;
|
|
28
|
+
unregister(): void;
|
|
29
|
+
};
|
|
30
|
+
export declare function post(options: Omit<EndpointOptions, OmittedFields>): {
|
|
31
|
+
(legacyServer?: Server): void;
|
|
32
|
+
/**
|
|
33
|
+
* Makes it possible to access the requestHandler through the exported function.
|
|
34
|
+
* If you need to test the function separately, without starting the server
|
|
35
|
+
*/
|
|
36
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
37
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
38
|
+
PATH: string;
|
|
39
|
+
unregister(): void;
|
|
40
|
+
};
|
|
41
|
+
export declare function put(options: Omit<EndpointOptions, OmittedFields>): {
|
|
42
|
+
(legacyServer?: Server): void;
|
|
43
|
+
/**
|
|
44
|
+
* Makes it possible to access the requestHandler through the exported function.
|
|
45
|
+
* If you need to test the function separately, without starting the server
|
|
46
|
+
*/
|
|
47
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
48
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
49
|
+
PATH: string;
|
|
50
|
+
unregister(): void;
|
|
51
|
+
};
|
|
52
|
+
export declare function patch(options: Omit<EndpointOptions, OmittedFields>): {
|
|
53
|
+
(legacyServer?: Server): void;
|
|
54
|
+
/**
|
|
55
|
+
* Makes it possible to access the requestHandler through the exported function.
|
|
56
|
+
* If you need to test the function separately, without starting the server
|
|
57
|
+
*/
|
|
58
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
59
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
60
|
+
PATH: string;
|
|
61
|
+
unregister(): void;
|
|
62
|
+
};
|
|
63
|
+
export declare function del(options: Omit<EndpointOptions, OmittedFields>): {
|
|
64
|
+
(legacyServer?: Server): void;
|
|
65
|
+
/**
|
|
66
|
+
* Makes it possible to access the requestHandler through the exported function.
|
|
67
|
+
* If you need to test the function separately, without starting the server
|
|
68
|
+
*/
|
|
69
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
70
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
71
|
+
PATH: string;
|
|
72
|
+
unregister(): void;
|
|
73
|
+
};
|
|
74
|
+
/** Internal endpoints are not available through the API
|
|
75
|
+
* @deprecated Use internal.get, internal.post, internal.put, internal.patch, internal.del instead
|
|
76
|
+
*/
|
|
77
|
+
export declare function internal(options: Omit<EndpointOptions, "internal">): {
|
|
78
|
+
(legacyServer?: Server): void;
|
|
79
|
+
/**
|
|
80
|
+
* Makes it possible to access the requestHandler through the exported function.
|
|
81
|
+
* If you need to test the function separately, without starting the server
|
|
82
|
+
*/
|
|
83
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
84
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
85
|
+
PATH: string;
|
|
86
|
+
unregister(): void;
|
|
87
|
+
};
|
|
88
|
+
export declare namespace internal {
|
|
89
|
+
var get: (options: Omit<EndpointOptions, OmittedFields>) => {
|
|
90
|
+
(legacyServer?: Server): 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>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
96
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
97
|
+
PATH: string;
|
|
98
|
+
unregister(): void;
|
|
99
|
+
};
|
|
100
|
+
var post: (options: Omit<EndpointOptions, OmittedFields>) => {
|
|
101
|
+
(legacyServer?: Server): void;
|
|
102
|
+
/**
|
|
103
|
+
* Makes it possible to access the requestHandler through the exported function.
|
|
104
|
+
* If you need to test the function separately, without starting the server
|
|
105
|
+
*/
|
|
106
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
107
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
108
|
+
PATH: string;
|
|
109
|
+
unregister(): void;
|
|
110
|
+
};
|
|
111
|
+
var put: (options: Omit<EndpointOptions, OmittedFields>) => {
|
|
112
|
+
(legacyServer?: Server): void;
|
|
113
|
+
/**
|
|
114
|
+
* Makes it possible to access the requestHandler through the exported function.
|
|
115
|
+
* If you need to test the function separately, without starting the server
|
|
116
|
+
*/
|
|
117
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
118
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
119
|
+
PATH: string;
|
|
120
|
+
unregister(): void;
|
|
121
|
+
};
|
|
122
|
+
var patch: (options: Omit<EndpointOptions, OmittedFields>) => {
|
|
123
|
+
(legacyServer?: Server): void;
|
|
124
|
+
/**
|
|
125
|
+
* Makes it possible to access the requestHandler through the exported function.
|
|
126
|
+
* If you need to test the function separately, without starting the server
|
|
127
|
+
*/
|
|
128
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
129
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
130
|
+
PATH: string;
|
|
131
|
+
unregister(): void;
|
|
132
|
+
};
|
|
133
|
+
var del: (options: Omit<EndpointOptions, OmittedFields>) => {
|
|
134
|
+
(legacyServer?: Server): void;
|
|
135
|
+
/**
|
|
136
|
+
* Makes it possible to access the requestHandler through the exported function.
|
|
137
|
+
* If you need to test the function separately, without starting the server
|
|
138
|
+
*/
|
|
139
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
140
|
+
/** Endpoint's full path (including any version number and internal prefix) */
|
|
141
|
+
PATH: string;
|
|
142
|
+
unregister(): void;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
export interface JsonResponse<T> {
|
|
146
|
+
/** Response body */
|
|
147
|
+
body?: T;
|
|
148
|
+
/** HTTP status code. @default 200 */
|
|
149
|
+
status?: number;
|
|
150
|
+
/** Additional headers */
|
|
151
|
+
headers?: {
|
|
152
|
+
totalcount?: number;
|
|
153
|
+
[key: string]: any;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
export { Next, Request, Response };
|
|
157
|
+
export interface EndpointOptions {
|
|
158
|
+
/** Whether to add the endpoint to the metadata or not. @default true */
|
|
159
|
+
addToMetadata?: boolean;
|
|
160
|
+
/** Whether the endpoint is internal or not */
|
|
161
|
+
internal?: boolean;
|
|
162
|
+
/** The path to the endpoint */
|
|
163
|
+
path: string;
|
|
164
|
+
/** The activity that is required to access this endpoint */
|
|
165
|
+
authorized?: string | string[];
|
|
166
|
+
/** Whether you need to be logged in or not to access the endpoint. Only needed if authorized is not used. */
|
|
167
|
+
authenticated?: boolean;
|
|
168
|
+
/** HTTP method */
|
|
169
|
+
method: Method;
|
|
170
|
+
/**
|
|
171
|
+
* The request body schema, either a JSON schema or the name of a typescript type
|
|
172
|
+
* (See below. The name must match the name of the file in ./api/schemas).
|
|
173
|
+
**/
|
|
174
|
+
requestSchema?: object | string;
|
|
175
|
+
/**
|
|
176
|
+
* The query schema, either a JSON schema or the name of a typescript type
|
|
177
|
+
* (See below. The name must match the name of the file in ./api/schemas).
|
|
178
|
+
*/
|
|
179
|
+
querySchema?: object | string;
|
|
180
|
+
/**
|
|
181
|
+
* The url param schema, either a JSON schema or the name of a typescript type
|
|
182
|
+
* (See below. The name must match the name of the file in ./api/schemas).
|
|
183
|
+
*/
|
|
184
|
+
urlParamsSchema?: object | string;
|
|
185
|
+
/** Additional route options */
|
|
186
|
+
opts?: RouteOptions;
|
|
187
|
+
requestHandlers?: any[];
|
|
188
|
+
/** prefix for the path, e.g. /v1, defaults to what was set in setupApiInterface */
|
|
189
|
+
version?: string;
|
|
190
|
+
/** The documentation for the endpoint */
|
|
191
|
+
documentation?: {
|
|
192
|
+
/** Short description of the endpoint */
|
|
193
|
+
summary: string;
|
|
194
|
+
/** Long description of the endpoint */
|
|
195
|
+
description?: string;
|
|
196
|
+
/** Optional array of tags */
|
|
197
|
+
tags?: string[];
|
|
198
|
+
/**
|
|
199
|
+
* Optional description for query params (can also be defined and described in the json schema,
|
|
200
|
+
* since that's how we validate them)
|
|
201
|
+
**/
|
|
202
|
+
query?: Record<string, string>;
|
|
203
|
+
/**
|
|
204
|
+
* Optional description of url parameters.
|
|
205
|
+
* Usually unnecessary since url params should be self descriptive, e.g. /dashboards/:dashboardId
|
|
206
|
+
**/
|
|
207
|
+
parameters?: Record<string, string>;
|
|
208
|
+
/** Response schemas */
|
|
209
|
+
responses?: {
|
|
210
|
+
/** HTTP status code */
|
|
211
|
+
status: number;
|
|
212
|
+
/** An optional description of the response */
|
|
213
|
+
description?: string;
|
|
214
|
+
/**
|
|
215
|
+
* An optional response schema, either a JSON schema or the name of a typescript type
|
|
216
|
+
* (See below. The name must match the name of the file in ./api/schemas).
|
|
217
|
+
**/
|
|
218
|
+
schema?: any | any[];
|
|
219
|
+
}[];
|
|
220
|
+
};
|
|
221
|
+
/** The request handler function */
|
|
222
|
+
requestHandler: (req: Request<any, any>, res?: Response<any>, next?: Next) => Promise<JsonResponse<any> | void>;
|
|
223
|
+
}
|
|
224
|
+
export type EndpointMetadata = Omit<EndpointOptions, "requestHandler" | "opts">;
|
|
225
|
+
export declare const endpointMetadata: {
|
|
226
|
+
[key in Method]: Record<string, EndpointMetadata>;
|
|
227
|
+
};
|
|
228
|
+
export interface ApiInterfaceOptions {
|
|
229
|
+
/** Name of the service */
|
|
230
|
+
serviceName: string;
|
|
231
|
+
/** Default log level for the service */
|
|
232
|
+
serviceLogLevel: string;
|
|
233
|
+
/** Whether to use typescript or not */
|
|
234
|
+
typescript: boolean;
|
|
235
|
+
/** Global version of the API @default v1. Can be overridden per endpoint */
|
|
236
|
+
apiVersion: string;
|
|
237
|
+
/** The base path to the schemas. @default api/schemas */
|
|
238
|
+
schemaBasePath?: string;
|
|
239
|
+
/** The port to start the server on */
|
|
240
|
+
port: number;
|
|
241
|
+
/** Whether to register the metadata endpoint or not. @default true */
|
|
242
|
+
registerMetadataEndpoint?: boolean;
|
|
243
|
+
onUncaughtException?: (err: Error) => void;
|
|
244
|
+
/** Maximum length of a parameter. @default 2000 after which the default route is used */
|
|
245
|
+
maxParamLength?: number;
|
|
246
|
+
legacyMode?: boolean;
|
|
247
|
+
}
|
|
248
|
+
export declare function getApiOptions(): ApiInterfaceOptions;
|
|
249
|
+
export declare function setupApiInterface(options: Omit<ApiInterfaceOptions, "legacyMode">): Promise<Server>;
|
|
250
|
+
/**
|
|
251
|
+
* Legacy option to enable legacy mode where the server is started with hiot-restify5.
|
|
252
|
+
* Should only be used (as a temporary solution) where the new server setup is not possible without a complete refactor
|
|
253
|
+
*/
|
|
254
|
+
export declare function setupInterfaceLegacy(options: Omit<ApiInterfaceOptions, "legacyMode">): (locator: Locator) => Promise<Locator>;
|
|
255
|
+
export declare function getServiceName(): string;
|