refactor: 将api网关相关代码放在sdk中做统一管理

在业务方移除api网关的依赖声明
pull/64/head
moonrailgun 2 years ago
parent dd358ac79b
commit c3deea8925

@ -765,7 +765,6 @@ importers:
moleculer: 0.14.23
moleculer-cli: ^0.7.1
moleculer-minio: ^2.0.0
moleculer-web: ^0.10.4
moment: ^2.29.1
mongodb: 4.2.1
mongoose: 6.1.1
@ -810,7 +809,6 @@ importers:
mime: 2.6.0
mkdirp: 1.0.4
moleculer-minio: 2.0.0_77awcwzrgh47fhn6qqq4ghcfau_moleculer@0.14.23
moleculer-web: 0.10.4_moleculer@0.14.23
moment: 2.29.4
mongodb: 4.2.1
mongoose: 6.1.1
@ -908,14 +906,19 @@ importers:
server/packages/sdk:
specifiers:
'@fastify/busboy': ^1.1.0
'@typegoose/typegoose': 9.3.1
accept-language: ^3.0.18
body-parser: ^1.20.1
crc: ^3.8.0
dotenv: ^10.0.0
etag: ^1.8.1
fastest-validator: ^1.12.0
fresh: ^0.5.2
i18next: ^20.3.5
i18next-fs-backend: ^1.1.1
ioredis: ^4.27.6
isstream: ^0.1.2
kleur: ^4.1.4
lodash: ^4.17.21
moleculer: 0.14.23
@ -924,16 +927,22 @@ importers:
moment: ^2.29.1
mongodb: 4.2.1
mongoose: 6.1.1
path-to-regexp: ^6.2.1
typescript: ^4.3.3
dependencies:
'@fastify/busboy': 1.1.0
'@typegoose/typegoose': 9.3.1_mongoose@6.1.1
accept-language: 3.0.18
body-parser: 1.20.1
crc: 3.8.0
dotenv: 10.0.0
etag: 1.8.1
fastest-validator: 1.13.0
fresh: 0.5.2
i18next: 20.6.1
i18next-fs-backend: 1.1.5
ioredis: 4.28.5
isstream: 0.1.2
kleur: 4.1.5
lodash: 4.17.21
moleculer: 0.14.23_ioredis@4.28.5
@ -942,6 +951,7 @@ importers:
moment: 2.29.4
mongodb: 4.2.1
mongoose: 6.1.1
path-to-regexp: 6.2.1
devDependencies:
typescript: 4.7.4
@ -13758,7 +13768,6 @@ packages:
unpipe: 1.0.0
transitivePeerDependencies:
- supports-color
dev: true
/bonjour-service/1.0.13:
resolution: {integrity: sha512-LWKRU/7EqDUC9CTAQtuZl5HzBALoCYwtLhffW3et7vZMwv3bWLpJf8bRYlMD5OCcDpTfnPgNCV4yo9ZIaJGMiA==}
@ -17381,7 +17390,7 @@ packages:
es-to-primitive: 1.2.1
function-bind: 1.1.1
function.prototype.name: 1.1.5
get-intrinsic: 1.1.2
get-intrinsic: 1.1.3
get-symbol-description: 1.0.0
has: 1.0.3
has-property-descriptors: 1.0.0
@ -19691,6 +19700,7 @@ packages:
function-bind: 1.1.1
has: 1.0.3
has-symbols: 1.0.3
dev: true
/get-intrinsic/1.1.3:
resolution: {integrity: sha512-QJVz1Tj7MS099PevUG5jvnt9tSkXN8K14dxQlikJuPt4uD9hHAHjLyLBiLR5zELelBdD9QNRAXZzsJx0WaDL9A==}
@ -21299,7 +21309,7 @@ packages:
resolution: {integrity: sha512-O0DB1JC/sPyZl7cIo78n5dR7eUSwwpYPiXRhTzNxZVAMUuB8vlnRFyLxdrVToks6XPLVnFfbzaVd5WLjhgg+vA==}
engines: {node: '>= 0.4'}
dependencies:
get-intrinsic: 1.1.2
get-intrinsic: 1.1.3
has: 1.0.3
side-channel: 1.0.4
@ -22228,7 +22238,7 @@ packages:
pretty-format: 27.5.1
slash: 3.0.0
strip-json-comments: 3.1.1
ts-node: 10.9.1_nzafxra4mdyuer2ejmql6rdadq
ts-node: 10.9.1_t4lrjbt3sxauai4t5o275zsepa
transitivePeerDependencies:
- bufferutil
- canvas
@ -24815,28 +24825,6 @@ packages:
yargs-parser: 21.1.1
dev: false
/moleculer-web/0.10.4_moleculer@0.14.23:
resolution: {integrity: sha512-WU5jZRoH53D3Cx2eBPFPlY5+7RI4teb5nlupRZO0N9vkRblGIFm4ySahZxBN4xVXvFSF5EWt/j1BdQr5rBocVw==}
engines: {node: '>= 10.x.x'}
peerDependencies:
moleculer: ^0.13.0 || ^0.14.0
dependencies:
'@fastify/busboy': 1.1.0
body-parser: 1.20.0
es6-error: 4.1.1
etag: 1.8.1
fresh: 0.5.2
isstream: 0.1.2
kleur: 4.1.5
lodash: 4.17.21
moleculer: 0.14.23_v2nojfag4zngjtjfzx3xixwhsy
path-to-regexp: 3.2.0
qs: 6.11.0
serve-static: 1.15.0
transitivePeerDependencies:
- supports-color
dev: false
/moleculer/0.14.23_ioredis@4.28.5:
resolution: {integrity: sha512-DE84fy8+1QiA7kUkF9ce4nyDdptBDlLFQfsaUccTScMGAbEjye5e+zfoI9iSA9rznwvshC8tgLhbOcnQBMn/HA==}
engines: {node: '>= 10.x.x'}
@ -26639,10 +26627,6 @@ packages:
resolution: {integrity: sha512-gu9bD6Ta5bwGrrU8muHzVOBFFREpp2iRkVfhBJahwJ6p6Xw20SjT0MxLnwkjOibQmGSYhiUnf2FLe7k+jcFmGQ==}
dev: false
/path-to-regexp/3.2.0:
resolution: {integrity: sha512-jczvQbCUS7XmS7o+y1aEO9OBVFeZBQ1MDSEqmO7xSoPgOPoowY/SxLpZ6Vh97/8qHZOteiCKb7gkG9gA2ZUxJA==}
dev: false
/path-to-regexp/6.2.1:
resolution: {integrity: sha512-JLyh7xT1kizaEvcaXOQwOc2/Yhw6KZOvPf1S8401UyLk86CU79LN3vl7ztXGm/pZ+YjoyAJ4rxmHwbkBXJX+yw==}
dev: false
@ -34122,7 +34106,6 @@ packages:
typescript: 4.7.4
v8-compile-cache-lib: 3.0.1
yn: 3.1.1
dev: false
/ts-node/10.9.1_typescript@4.7.4:
resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==}

@ -57,7 +57,6 @@
"mime": "^2.5.2",
"mkdirp": "^1.0.4",
"moleculer-minio": "^2.0.0",
"moleculer-web": "^0.10.4",
"moment": "^2.29.1",
"mongodb": "4.2.1",
"mongoose": "6.1.1",

@ -32,14 +32,19 @@
"typescript": "^4.3.3"
},
"dependencies": {
"@fastify/busboy": "^1.1.0",
"@typegoose/typegoose": "9.3.1",
"accept-language": "^3.0.18",
"body-parser": "^1.20.1",
"crc": "^3.8.0",
"dotenv": "^10.0.0",
"etag": "^1.8.1",
"fastest-validator": "^1.12.0",
"fresh": "^0.5.2",
"i18next": "^20.3.5",
"i18next-fs-backend": "^1.1.1",
"ioredis": "^4.27.6",
"isstream": "^0.1.2",
"kleur": "^4.1.4",
"lodash": "^4.17.21",
"moleculer": "0.14.23",
@ -47,6 +52,7 @@
"moleculer-repl": "^0.7.2",
"moment": "^2.29.1",
"mongodb": "4.2.1",
"mongoose": "6.1.1"
"mongoose": "6.1.1",
"path-to-regexp": "^6.2.1"
}
}

@ -12,6 +12,8 @@ export type {
} from './services/types';
export { parseLanguageFromHead } from './services/lib/i18n/parser';
export { t } from './services/lib/i18n';
export { ApiGatewayMixin } from './services/lib/moleculer-web';
export * as ApiGatewayErrors from './services/lib/moleculer-web/errors';
export * from './services/lib/errors';
export { PERMISSION, allPermission } from './services/lib/role';
export { call } from './services/lib/call';

@ -0,0 +1,3 @@
Fork from https://github.com/moleculerjs/moleculer-web
Hash: f375dbb4f8bff8aa16e95024e5c65463b626fa45

@ -0,0 +1,312 @@
/*
* moleculer
* Copyright (c) 2021 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/
import { pathToRegexp } from 'path-to-regexp';
import Busboy from '@fastify/busboy';
import kleur from 'kleur';
import _ from 'lodash';
import { PayloadTooLarge } from './errors';
import { Errors } from 'moleculer';
const { MoleculerClientError } = Errors;
import {
removeTrailingSlashes,
addSlashes,
decodeParam,
compose,
} from './utils';
export class Alias {
service;
route;
type = 'call';
method = '*';
path = null;
handler = null;
action = null;
fullPath;
keys;
re;
busboyConfig;
/**
* Constructor of Alias
*
* @param {Service} service
* @param {Object} route
* @param {Object} opts
* @param {any} action
*/
constructor(service, route, opts, action) {
this.service = service;
this.route = route;
if (_.isString(opts)) {
// Parse alias string
if (opts.indexOf(' ') !== -1) {
const p = opts.split(/\s+/);
this.method = p[0];
this.path = p[1];
} else {
this.path = opts;
}
} else if (_.isObject(opts)) {
Object.assign(this, _.cloneDeep(opts));
}
if (_.isString(action)) {
// Parse type from action name
if (action.indexOf(':') > 0) {
const p = action.split(':');
this.type = p[0];
this.action = p[1];
} else {
this.action = action;
}
} else if (_.isFunction(action)) {
this.handler = action;
this.action = null;
} else if (Array.isArray(action)) {
const mws = _.compact(
action.map((mw) => {
if (_.isString(mw)) this.action = mw;
else if (_.isFunction(mw)) return mw;
})
);
this.handler = compose.call(service, ...mws);
} else if (action != null) {
Object.assign(this, _.cloneDeep(action));
}
this.type = this.type || 'call';
this.path = removeTrailingSlashes(this.path);
this.fullPath = this.fullPath || addSlashes(this.route.path) + this.path;
if (this.fullPath !== '/' && this.fullPath.endsWith('/')) {
this.fullPath = this.fullPath.slice(0, -1);
}
this.keys = [];
this.re = pathToRegexp(
this.fullPath,
this.keys,
route.opts.pathToRegexpOptions || {}
); // Options: https://github.com/pillarjs/path-to-regexp#usage
if (this.type == 'multipart') {
// Handle file upload in multipart form
this.handler = this.multipartHandler.bind(this);
}
}
/**
*
* @param {*} url
*/
match(url) {
const m = this.re.exec(url);
if (!m) return false;
const params = {};
let key, param;
for (let i = 0; i < this.keys.length; i++) {
key = this.keys[i];
param = m[i + 1];
if (!param) continue;
params[key.name] = decodeParam(param);
if (key.repeat) params[key.name] = params[key.name].split(key.delimiter);
}
return params;
}
/**
*
* @param {*} method
*/
isMethod(method) {
return this.method === '*' || this.method === method;
}
/**
*
*/
printPath() {
/* istanbul ignore next */
return `${this.method} ${this.fullPath}`;
}
/**
*
*/
toString() {
return (
kleur.magenta(_.padStart(this.method, 6)) +
' ' +
kleur.cyan(this.fullPath) +
kleur.grey(' => ') +
(this.handler != null && this.type !== 'multipart'
? '<Function>'
: this.action)
);
}
/**
*
* @param {*} req
* @param {*} res
*/
multipartHandler(req, res) {
const ctx = req.$ctx;
ctx.meta.$multipart = {};
const promises = [];
let numOfFiles = 0;
let hasField = false;
const busboyOptions = _.defaultsDeep(
{ headers: req.headers },
this.busboyConfig,
this.route.opts.busboyConfig
);
const busboy = new Busboy(busboyOptions);
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
file.on('limit', () => {
// This file reached the file size limit.
if (_.isFunction(busboyOptions.onFileSizeLimit)) {
busboyOptions.onFileSizeLimit.call(this.service, file, busboy);
}
file.destroy(
new PayloadTooLarge({ fieldname, filename, encoding, mimetype })
);
});
numOfFiles++;
promises.push(
ctx
.call(
this.action,
file,
_.defaultsDeep({}, this.route.opts.callOptions, {
meta: {
fieldname: fieldname,
filename: filename,
encoding: encoding,
mimetype: mimetype,
$params: req.$params,
},
})
)
.catch((err) => {
file.resume(); // Drain file stream to continue processing form
busboy.emit('error', err);
return err;
})
);
});
busboy.on('field', (field, value) => {
hasField = true;
ctx.meta.$multipart[field] = value;
});
busboy.on('finish', async () => {
/* istanbul ignore next */
if (!busboyOptions.empty && numOfFiles == 0)
return this.service.sendError(
req,
res,
new MoleculerClientError('File missing in the request')
);
// Call the action if no files but multipart fields
if (numOfFiles == 0 && hasField) {
promises.push(
ctx.call(
this.action,
{},
_.defaultsDeep({}, this.route.opts.callOptions, {
meta: {
$params: req.$params,
},
})
)
);
}
try {
let data = await this.service.Promise.all(promises);
const fileLimit =
busboyOptions.limits && busboyOptions.limits.files != null
? busboyOptions.limits.files
: null;
if (numOfFiles == 1 && fileLimit == 1) {
// Remove the array wrapping
data = data[0];
}
if (this.route.onAfterCall)
data = await this.route.onAfterCall.call(
this,
ctx,
this.route,
req,
res,
data
);
this.service.sendResponse(req, res, data, {});
} catch (err) {
/* istanbul ignore next */
this.service.sendError(req, res, err);
}
});
/* istanbul ignore next */
busboy.on('error', (err) => {
req.unpipe(req.busboy);
req.resume();
this.service.sendError(req, res, err);
});
// Add limit event handlers
if (_.isFunction(busboyOptions.onPartsLimit)) {
busboy.on('partsLimit', () =>
busboyOptions.onPartsLimit.call(
this.service,
busboy,
this,
this.service
)
);
}
if (_.isFunction(busboyOptions.onFilesLimit)) {
busboy.on('filesLimit', () =>
busboyOptions.onFilesLimit.call(
this.service,
busboy,
this,
this.service
)
);
}
if (_.isFunction(busboyOptions.onFieldsLimit)) {
busboy.on('fieldsLimit', () =>
busboyOptions.onFieldsLimit.call(
this.service,
busboy,
this,
this.service
)
);
}
req.pipe(busboy);
}
}

@ -0,0 +1,218 @@
/*
* moleculer
* Copyright (c) 2021 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/
import { Errors } from 'moleculer';
const { MoleculerError, MoleculerClientError } = Errors;
export { MoleculerError, MoleculerClientError };
const ERR_NO_TOKEN = 'NO_TOKEN';
const ERR_INVALID_TOKEN = 'INVALID_TOKEN';
const ERR_UNABLE_DECODE_PARAM = 'UNABLE_DECODE_PARAM';
const ERR_ORIGIN_NOT_FOUND = 'ORIGIN_NOT_FOUND';
const ERR_ORIGIN_NOT_ALLOWED = 'ORIGIN_NOT_ALLOWED';
/**
* Invalid request body
*
* @class InvalidRequestBodyError
* @extends {Error}
*/
class InvalidRequestBodyError extends MoleculerError {
/**
* Creates an instance of InvalidRequestBodyError.
*
* @param {any} body
* @param {any} error
*
* @memberOf InvalidRequestBodyError
*/
constructor(body, error) {
super('Invalid request body', 400, 'INVALID_REQUEST_BODY', {
body,
error,
});
}
}
/**
* Invalid response type
*
* @class InvalidResponseTypeError
* @extends {Error}
*/
class InvalidResponseTypeError extends MoleculerError {
/**
* Creates an instance of InvalidResponseTypeError.
*
* @param {String} dataType
*
* @memberOf InvalidResponseTypeError
*/
constructor(dataType) {
super(`Invalid response type '${dataType}'`, 500, 'INVALID_RESPONSE_TYPE', {
dataType,
});
}
}
/**
* Unauthorized HTTP error
*
* @class UnAuthorizedError
* @extends {Error}
*/
class UnAuthorizedError extends MoleculerError {
/**
* Creates an instance of UnAuthorizedError.
*
* @param {String} type
* @param {any} data
*
* @memberOf UnAuthorizedError
*/
constructor(type, data) {
super('Unauthorized', 401, type || ERR_INVALID_TOKEN, data);
}
}
/**
* Forbidden HTTP error
*
* @class ForbiddenError
* @extends {Error}
*/
class ForbiddenError extends MoleculerError {
/**
* Creates an instance of ForbiddenError.
*
* @param {String} type
* @param {any} data
*
* @memberOf ForbiddenError
*/
constructor(type, data?) {
super('Forbidden', 403, type, data);
}
}
/**
* Bad request HTTP error
*
* @class BadRequestError
* @extends {Error}
*/
class BadRequestError extends MoleculerError {
/**
* Creates an instance of BadRequestError.
*
* @param {String} type
* @param {any} data
*
* @memberOf BadRequestError
*/
constructor(type, data) {
super('Bad request', 400, type, data);
}
}
/**
* Not found HTTP error
*
* @class NotFoundError
* @extends {Error}
*/
class NotFoundError extends MoleculerError {
/**
* Creates an instance of NotFoundError.
*
* @param {String} type
* @param {any} data
*
* @memberOf NotFoundError
*/
constructor(type?, data?) {
super('Not found', 404, type || 'NOT_FOUND', data);
}
}
/**
* Payload is too large HTTP error
*
* @class PayloadTooLarge
* @extends {Error}
*/
class PayloadTooLarge extends MoleculerClientError {
/**
* Creates an instance of PayloadTooLarge.
*
* @param {String} type
* @param {any} data
*
* @memberOf PayloadTooLarge
*/
constructor(data) {
super('Payload too large', 413, 'PAYLOAD_TOO_LARGE', data);
}
}
/**
* Rate limit exceeded HTTP error
*
* @class RateLimitExceeded
* @extends {Error}
*/
class RateLimitExceeded extends MoleculerClientError {
/**
* Creates an instance of RateLimitExceeded.
*
* @param {String} type
* @param {any} data
*
* @memberOf RateLimitExceeded
*/
constructor(type?, data?) {
super('Rate limit exceeded', 429, type, data);
}
}
/**
* Service unavailable HTTP error
*
* @class ForbiddenError
* @extends {Error}
*/
class ServiceUnavailableError extends MoleculerError {
/**
* Creates an instance of ForbiddenError.
*
* @param {String} type
* @param {any} data
*
* @memberOf ForbiddenError
*/
constructor(type?, data?) {
super('Service unavailable', 503, type, data);
}
}
export {
InvalidRequestBodyError,
InvalidResponseTypeError,
UnAuthorizedError,
ForbiddenError,
BadRequestError,
NotFoundError,
PayloadTooLarge,
RateLimitExceeded,
ServiceUnavailableError,
ERR_NO_TOKEN,
ERR_INVALID_TOKEN,
ERR_UNABLE_DECODE_PARAM,
ERR_ORIGIN_NOT_FOUND,
ERR_ORIGIN_NOT_ALLOWED,
};

File diff suppressed because it is too large Load Diff

@ -0,0 +1,53 @@
/**
* Memory store for Rate limiter
*
* Inspired by https://github.com/dotcypress/micro-ratelimit/
*
* @class MemoryStore
*/
export class MemoryStore {
hits;
resetTime;
timer;
/**
* Creates an instance of MemoryStore.
*
* @param {Number} clearPeriod
* @memberof MemoryStore
*/
constructor(clearPeriod) {
this.hits = new Map();
this.resetTime = Date.now() + clearPeriod;
this.timer = setInterval(() => {
this.resetTime = Date.now() + clearPeriod;
this.reset();
}, clearPeriod);
this.timer.unref();
}
/**
* Increment the counter by key
*
* @param {String} key
* @returns {Number}
* @memberof MemoryStore
*/
inc(key) {
let counter = this.hits.get(key) || 0;
counter++;
this.hits.set(key, counter);
return counter;
}
/**
* Reset all counters
*
* @memberof MemoryStore
*/
reset() {
this.hits.clear();
}
}

@ -0,0 +1,156 @@
/*
* moleculer
* Copyright (c) 2021 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/
import _ from 'lodash';
import fresh from 'fresh';
import etag from 'etag';
import {
BadRequestError,
ERR_UNABLE_DECODE_PARAM,
MoleculerError,
} from './errors';
/**
* Decode URI encoded param
* @param {String} param
*/
function decodeParam(param) {
try {
return decodeURIComponent(param);
} catch (_) {
/* istanbul ignore next */
throw new BadRequestError(ERR_UNABLE_DECODE_PARAM, { param });
}
}
// Remove slashes "/" from the left & right sides and remove double "//" slashes
function removeTrailingSlashes(s) {
if (s.startsWith('/')) s = s.slice(1);
if (s.endsWith('/')) s = s.slice(0, -1);
return s; //.replace(/\/\//g, "/");
}
// Add slashes "/" to the left & right sides
function addSlashes(s) {
return (s.startsWith('/') ? '' : '/') + s + (s.endsWith('/') ? '' : '/');
}
// Normalize URL path (remove multiple slashes //)
function normalizePath(s) {
return s.replace(/\/{2,}/g, '/');
}
/**
* Compose middlewares
*
* @param {...Function} mws
*/
function compose(...mws) {
const self = this as any;
return (req, res, done) => {
const next = (i, err?) => {
if (i >= mws.length) {
if (_.isFunction(done)) return done.call(self, err);
/* istanbul ignore next */
return;
}
if (err) {
// Call only error middlewares (err, req, res, next)
if (mws[i].length == 4)
mws[i].call(self, err, req, res, (err) => next(i + 1, err));
else next(i + 1, err);
} else {
if (mws[i].length < 4)
mws[i].call(self, req, res, (err) => next(i + 1, err));
else next(i + 1);
}
};
return next(0);
};
}
/**
* Compose middlewares and return Promise
* @param {...Function} mws
* @returns {Promise}
*/
function composeThen(req, res, ...mws) {
return new Promise<void>((resolve, reject) => {
compose.call(this, ...mws)(req, res, (err) => {
if (err) {
/* istanbul ignore next */
if (err instanceof MoleculerError) return reject(err);
/* istanbul ignore next */
if (err instanceof Error)
return reject(
new MoleculerError(
err.message,
(err as any).code || (err as any).status,
(err as any).type
)
); // TODO err.stack
/* istanbul ignore next */
return reject(new MoleculerError(err));
}
resolve();
});
});
}
/**
* Generate ETag from content.
*
* @param {any} body
* @param {Boolean|String|Function?} opt
*
* @returns {String}
*/
function generateETag(body, opt) {
if (_.isFunction(opt)) return opt.call(this, body);
const buf = !Buffer.isBuffer(body) ? Buffer.from(body) : body;
return etag(buf, opt === true || opt === 'weak' ? { weak: true } : null);
}
/**
* Check the data freshness.
*
* @param {*} req
* @param {*} res
*
* @returns {Boolean}
*/
function isFresh(req, res) {
if (
(res.statusCode >= 200 && res.statusCode < 300) ||
304 === res.statusCode
) {
return fresh(req.headers, {
etag: res.getHeader('ETag'),
'last-modified': res.getHeader('Last-Modified'),
});
}
return false;
}
export {
removeTrailingSlashes,
addSlashes,
normalizePath,
decodeParam,
compose,
composeThen,
generateETag,
isFresh,
};

@ -1,5 +1,4 @@
import type { IncomingMessage, ServerResponse } from 'http';
import ApiGateway from 'moleculer-web';
import _ from 'lodash';
import { TcSocketIOService } from '../../mixins/socketio.mixin';
import {
@ -10,6 +9,8 @@ import {
parseLanguageFromHead,
builtinAuthWhitelist,
PureContext,
ApiGatewayMixin,
ApiGatewayErrors,
} from 'tailchat-server-sdk';
import { TcHealth } from '../../mixins/health.mixin';
import type { Readable } from 'stream';
@ -26,7 +27,7 @@ export default class ApiService extends TcService {
}
onInit() {
this.registerMixin(ApiGateway);
this.registerMixin(ApiGatewayMixin);
this.registerMixin(
TcSocketIOService({
userAuth: async (token) => {
@ -355,8 +356,8 @@ export default class ApiService extends TcService {
const token = req.headers['x-token'] as string;
if (typeof token !== 'string') {
throw new ApiGateway.Errors.UnAuthorizedError(
ApiGateway.Errors.ERR_NO_TOKEN,
throw new ApiGatewayErrors.UnAuthorizedError(
ApiGatewayErrors.ERR_NO_TOKEN,
{
error: 'No Token',
}
@ -379,8 +380,8 @@ export default class ApiService extends TcService {
throw new Error(t('Token不合规'));
}
} catch (err) {
throw new ApiGateway.Errors.UnAuthorizedError(
ApiGateway.Errors.ERR_INVALID_TOKEN,
throw new ApiGatewayErrors.UnAuthorizedError(
ApiGatewayErrors.ERR_INVALID_TOKEN,
{
error: 'Invalid Token:' + String(err),
}

@ -1,6 +1,5 @@
import ApiGateway from 'moleculer-web';
import { Provider, Configuration, InteractionResults } from 'oidc-provider';
import { config, TcService } from 'tailchat-server-sdk';
import { config, TcService, ApiGatewayMixin } from 'tailchat-server-sdk';
import type { IncomingMessage, ServerResponse } from 'http';
import ejs from 'ejs';
import path from 'path';
@ -93,7 +92,7 @@ class OIDCService extends TcService {
}
protected onInit(): void {
this.registerMixin(ApiGateway);
this.registerMixin(ApiGatewayMixin);
this.registerSetting('port', PORT);
this.registerSetting('routes', this.getRoutes());

Loading…
Cancel
Save