softkave-node-utils 0.3.0 → 0.4.0
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/build/common/error.d.ts +24 -0
- package/build/common/error.js +36 -0
- package/build/common/error.js.map +1 -0
- package/build/common/getClientConfig.d.ts +9 -0
- package/build/common/getClientConfig.js +23 -0
- package/build/common/getClientConfig.js.map +1 -0
- package/build/common/index.d.ts +4 -0
- package/build/common/index.js +4 -0
- package/build/common/index.js.map +1 -0
- package/build/common/logger/fimidx-auth-logger.d.ts +3 -0
- package/build/common/logger/fimidx-auth-logger.js +7 -0
- package/build/common/logger/fimidx-auth-logger.js.map +1 -0
- package/build/common/logger/fimidx-console-logger.d.ts +3 -0
- package/build/common/logger/fimidx-console-logger.js +11 -0
- package/build/common/logger/fimidx-console-logger.js.map +1 -0
- package/build/common/logger/fimidx-logger.d.ts +3 -0
- package/build/common/logger/fimidx-logger.js +10 -0
- package/build/common/logger/fimidx-logger.js.map +1 -0
- package/build/common/logger/index.d.ts +4 -0
- package/build/common/logger/index.js +4 -0
- package/build/common/logger/index.js.map +1 -0
- package/build/mongo/MongooseMongoDBConnection.d.ts +11 -0
- package/build/mongo/MongooseMongoDBConnection.js +20 -0
- package/build/mongo/MongooseMongoDBConnection.js.map +1 -0
- package/build/mongo/NoopMongoDBConnection.d.ts +8 -0
- package/build/mongo/NoopMongoDBConnection.js +13 -0
- package/build/mongo/NoopMongoDBConnection.js.map +1 -0
- package/build/mongo/getLazyMongooseMongoDBConnection.d.ts +10 -0
- package/build/mongo/getLazyMongooseMongoDBConnection.js +14 -0
- package/build/mongo/getLazyMongooseMongoDBConnection.js.map +1 -0
- package/build/mongo/getOpinionatedMongoDBConfig.d.ts +5 -0
- package/build/mongo/getOpinionatedMongoDBConfig.js +12 -0
- package/build/mongo/getOpinionatedMongoDBConfig.js.map +1 -0
- package/build/mongo/index.d.ts +7 -0
- package/build/mongo/index.js +7 -0
- package/build/mongo/index.js.map +1 -0
- package/build/mongo/types.d.ts +6 -0
- package/build/mongo/types.js +2 -0
- package/build/mongo/types.js.map +1 -0
- package/build/mongo/utils.d.ts +11 -0
- package/build/mongo/utils.js +20 -0
- package/build/mongo/utils.js.map +1 -0
- package/build/next/index.d.ts +3 -0
- package/build/next/index.js +3 -0
- package/build/next/index.js.map +1 -0
- package/build/next/wrapAuthenticated.d.ts +17 -0
- package/build/next/wrapAuthenticated.js +50 -0
- package/build/next/wrapAuthenticated.js.map +1 -0
- package/build/next/wrapRoute.d.ts +7 -0
- package/build/next/wrapRoute.js +36 -0
- package/build/next/wrapRoute.js.map +1 -0
- package/build/server/index.d.ts +2 -0
- package/build/server/index.js +2 -0
- package/build/server/index.js.map +1 -0
- package/build/server/jwt/encodeJWTToken.d.ts +13 -0
- package/build/server/jwt/encodeJWTToken.js +25 -0
- package/build/server/jwt/encodeJWTToken.js.map +1 -0
- package/build/server/jwt/index.d.ts +3 -0
- package/build/server/jwt/index.js +3 -0
- package/build/server/jwt/index.js.map +1 -0
- package/build/server/jwt/types.d.ts +6 -0
- package/build/server/jwt/types.js +2 -0
- package/build/server/jwt/types.js.map +1 -0
- package/package.json +22 -4
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AnyObject } from 'softkave-js-utils';
|
|
2
|
+
export declare const kOwnError: unique symbol;
|
|
3
|
+
export declare const kOwnServerError: unique symbol;
|
|
4
|
+
export declare class OwnError extends Error {
|
|
5
|
+
static isOwnError(error: unknown): error is OwnError;
|
|
6
|
+
[kOwnError]: true;
|
|
7
|
+
meta?: AnyObject;
|
|
8
|
+
constructor(message: string, meta?: AnyObject);
|
|
9
|
+
}
|
|
10
|
+
export declare class OwnServerError extends OwnError {
|
|
11
|
+
static isOwnServerError(error: unknown): error is OwnServerError;
|
|
12
|
+
[kOwnServerError]: true;
|
|
13
|
+
statusCode: number;
|
|
14
|
+
constructor(message: string, statusCode: number, meta?: AnyObject);
|
|
15
|
+
}
|
|
16
|
+
export declare const kOwnServerErrorCodes: {
|
|
17
|
+
readonly InvalidRequest: 400;
|
|
18
|
+
readonly Unauthorized: 401;
|
|
19
|
+
readonly Forbidden: 403;
|
|
20
|
+
readonly NotFound: 404;
|
|
21
|
+
readonly MethodNotAllowed: 405;
|
|
22
|
+
readonly InternalServerError: 500;
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=error.d.ts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
var _a, _b;
|
|
2
|
+
import { isObject } from 'lodash-es';
|
|
3
|
+
export const kOwnError = Symbol('OwnError');
|
|
4
|
+
export const kOwnServerError = Symbol('OwnServerError');
|
|
5
|
+
export class OwnError extends Error {
|
|
6
|
+
static isOwnError(error) {
|
|
7
|
+
return isObject(error) && error[kOwnError] === true;
|
|
8
|
+
}
|
|
9
|
+
constructor(message, meta) {
|
|
10
|
+
super(message);
|
|
11
|
+
this[_a] = true;
|
|
12
|
+
this.meta = meta;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
_a = kOwnError;
|
|
16
|
+
export class OwnServerError extends OwnError {
|
|
17
|
+
static isOwnServerError(error) {
|
|
18
|
+
return (OwnError.isOwnError(error) &&
|
|
19
|
+
error[kOwnServerError] === true);
|
|
20
|
+
}
|
|
21
|
+
constructor(message, statusCode, meta) {
|
|
22
|
+
super(message, meta);
|
|
23
|
+
this[_b] = true;
|
|
24
|
+
this.statusCode = statusCode;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
_b = kOwnServerError;
|
|
28
|
+
export const kOwnServerErrorCodes = {
|
|
29
|
+
InvalidRequest: 400,
|
|
30
|
+
Unauthorized: 401,
|
|
31
|
+
Forbidden: 403,
|
|
32
|
+
NotFound: 404,
|
|
33
|
+
MethodNotAllowed: 405,
|
|
34
|
+
InternalServerError: 500,
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/common/error.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,WAAW,CAAC;AAGnC,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AAC5C,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAExD,MAAM,OAAO,QAAS,SAAQ,KAAK;IACjC,MAAM,CAAC,UAAU,CAAC,KAAc;QAC9B,OAAO,QAAQ,CAAC,KAAK,CAAC,IAAK,KAAmB,CAAC,SAAS,CAAC,KAAK,IAAI,CAAC;IACrE,CAAC;IAKD,YAAY,OAAe,EAAE,IAAgB;QAC3C,KAAK,CAAC,OAAO,CAAC,CAAC;QAJjB,QAAW,GAAS,IAAa,CAAC;QAKhC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;KAPE,SAAS;AASZ,MAAM,OAAO,cAAe,SAAQ,QAAQ;IAC1C,MAAM,CAAC,gBAAgB,CAAC,KAAc;QACpC,OAAO,CACL,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC;YACzB,KAAmB,CAAC,eAAe,CAAC,KAAK,IAAI,CAC/C,CAAC;IACJ,CAAC;IAKD,YAAY,OAAe,EAAE,UAAkB,EAAE,IAAgB;QAC/D,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAJvB,QAAiB,GAAS,IAAa,CAAC;QAKtC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;CACF;KAPE,eAAe;AASlB,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,cAAc,EAAE,GAAG;IACnB,YAAY,EAAE,GAAG;IACjB,SAAS,EAAE,GAAG;IACd,QAAQ,EAAE,GAAG;IACb,gBAAgB,EAAE,GAAG;IACrB,mBAAmB,EAAE,GAAG;CAChB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import assert from 'assert';
|
|
2
|
+
const publicURL = process.env.NEXT_PUBLIC_URL;
|
|
3
|
+
const fimidxAppId = process.env.NEXT_PUBLIC_FIMIDX_LOGGER_APP_ID;
|
|
4
|
+
const fimidxClientToken = process.env.NEXT_PUBLIC_FIMIDX_LOGGER_CLIENT_TOKEN;
|
|
5
|
+
const fimidxServerUrl = process.env.NEXT_PUBLIC_FIMIDX_LOGGER_SERVER_URL;
|
|
6
|
+
const nodeEnv = process.env.NEXT_PUBLIC_APP_ENV;
|
|
7
|
+
const fimidxLoggerEnabled = process.env.NEXT_PUBLIC_FIMIDX_LOGGER_ENABLED === 'true';
|
|
8
|
+
assert(publicURL, 'NEXT_PUBLIC_URL is not set');
|
|
9
|
+
assert(fimidxAppId, 'NEXT_PUBLIC_FIMIDX_LOGGER_APP_ID is not set');
|
|
10
|
+
assert(fimidxClientToken, 'NEXT_PUBLIC_FIMIDX_LOGGER_CLIENT_TOKEN is not set');
|
|
11
|
+
// assert(fimidxServerUrl, "NEXT_PUBLIC_FIMIDX_LOGGER_SERVER_URL is not set");
|
|
12
|
+
assert(nodeEnv, 'NEXT_PUBLIC_APP_ENV is not set');
|
|
13
|
+
export const getClientConfig = () => {
|
|
14
|
+
return {
|
|
15
|
+
publicURL,
|
|
16
|
+
fimidxAppId,
|
|
17
|
+
fimidxClientToken,
|
|
18
|
+
fimidxServerUrl,
|
|
19
|
+
nodeEnv,
|
|
20
|
+
fimidxLoggerEnabled,
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=getClientConfig.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getClientConfig.js","sourceRoot":"","sources":["../../src/common/getClientConfig.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC;AAC9C,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,gCAAgC,CAAC;AACjE,MAAM,iBAAiB,GAAG,OAAO,CAAC,GAAG,CAAC,sCAAsC,CAAC;AAC7E,MAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,oCAAoC,CAAC;AACzE,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC;AAChD,MAAM,mBAAmB,GACvB,OAAO,CAAC,GAAG,CAAC,iCAAiC,KAAK,MAAM,CAAC;AAE3D,MAAM,CAAC,SAAS,EAAE,4BAA4B,CAAC,CAAC;AAChD,MAAM,CAAC,WAAW,EAAE,6CAA6C,CAAC,CAAC;AACnE,MAAM,CAAC,iBAAiB,EAAE,mDAAmD,CAAC,CAAC;AAC/E,8EAA8E;AAC9E,MAAM,CAAC,OAAO,EAAE,gCAAgC,CAAC,CAAC;AAElD,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,EAAE;IAClC,OAAO;QACL,SAAS;QACT,WAAW;QACX,iBAAiB;QACjB,eAAe;QACf,OAAO;QACP,mBAAmB;KACpB,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { FimidxNextAuthLogger } from 'fimidx';
|
|
2
|
+
import { fimidxConsoleLogger } from './fimidx-console-logger.js';
|
|
3
|
+
export const fimidxNextAuthLogger = new FimidxNextAuthLogger({
|
|
4
|
+
fimidxConsoleLogger,
|
|
5
|
+
debug: false,
|
|
6
|
+
});
|
|
7
|
+
//# sourceMappingURL=fimidx-auth-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fimidx-auth-logger.js","sourceRoot":"","sources":["../../../src/common/logger/fimidx-auth-logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,oBAAoB,EAAC,MAAM,QAAQ,CAAC;AAC5C,OAAO,EAAC,mBAAmB,EAAC,MAAM,4BAA4B,CAAC;AAE/D,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,oBAAoB,CAAC;IAC3D,mBAAmB;IACnB,KAAK,EAAE,KAAK;CACb,CAAC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FimidxConsoleLikeLogger } from 'fimidx';
|
|
2
|
+
import { getClientConfig } from '../getClientConfig.js';
|
|
3
|
+
import { fimidxLogger } from './fimidx-logger.js';
|
|
4
|
+
const { nodeEnv, fimidxLoggerEnabled } = getClientConfig();
|
|
5
|
+
export const fimidxConsoleLogger = new FimidxConsoleLikeLogger({
|
|
6
|
+
fimidxLogger: fimidxLogger,
|
|
7
|
+
// Enable console fallback in development
|
|
8
|
+
enableConsoleFallback: nodeEnv === 'development',
|
|
9
|
+
enabled: fimidxLoggerEnabled,
|
|
10
|
+
});
|
|
11
|
+
//# sourceMappingURL=fimidx-console-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fimidx-console-logger.js","sourceRoot":"","sources":["../../../src/common/logger/fimidx-console-logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,uBAAuB,EAAC,MAAM,QAAQ,CAAC;AAC/C,OAAO,EAAC,eAAe,EAAC,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAC,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAEhD,MAAM,EAAC,OAAO,EAAE,mBAAmB,EAAC,GAAG,eAAe,EAAE,CAAC;AAEzD,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,uBAAuB,CAAC;IAC7D,YAAY,EAAE,YAAY;IAC1B,yCAAyC;IACzC,qBAAqB,EAAE,OAAO,KAAK,aAAa;IAChD,OAAO,EAAE,mBAAmB;CAC7B,CAAC,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FimidxLogger } from 'fimidx';
|
|
2
|
+
import { getClientConfig } from '../getClientConfig.js';
|
|
3
|
+
const { fimidxAppId, fimidxClientToken } = getClientConfig();
|
|
4
|
+
export const fimidxLogger = new FimidxLogger({
|
|
5
|
+
appId: fimidxAppId,
|
|
6
|
+
clientToken: fimidxClientToken,
|
|
7
|
+
consoleLogOnError: true,
|
|
8
|
+
logRemoteErrors: true,
|
|
9
|
+
});
|
|
10
|
+
//# sourceMappingURL=fimidx-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fimidx-logger.js","sourceRoot":"","sources":["../../../src/common/logger/fimidx-logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAC,eAAe,EAAC,MAAM,uBAAuB,CAAC;AAEtD,MAAM,EAAC,WAAW,EAAE,iBAAiB,EAAC,GAAG,eAAe,EAAE,CAAC;AAE3D,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,YAAY,CAAC;IAC3C,KAAK,EAAE,WAAW;IAClB,WAAW,EAAE,iBAAiB;IAC9B,iBAAiB,EAAE,IAAI;IACvB,eAAe,EAAE,IAAI;CACtB,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/common/logger/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Connection } from 'mongoose';
|
|
2
|
+
import { type MongoDBConnection } from './types.js';
|
|
3
|
+
export declare class MongooseMongoDBConnection implements MongoDBConnection<Connection> {
|
|
4
|
+
protected connection: Connection;
|
|
5
|
+
protected promise: Promise<Connection>;
|
|
6
|
+
constructor(uri: string, dbName: string);
|
|
7
|
+
get: <TConnection = Connection>() => TConnection;
|
|
8
|
+
wait: <TConnection = Connection>() => Promise<TConnection>;
|
|
9
|
+
close: () => Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=MongooseMongoDBConnection.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Connection } from 'mongoose';
|
|
2
|
+
import {} from './types.js';
|
|
3
|
+
import { getMongooseMongoConnection } from './utils.js';
|
|
4
|
+
export class MongooseMongoDBConnection {
|
|
5
|
+
constructor(uri, dbName) {
|
|
6
|
+
this.get = () => {
|
|
7
|
+
return this.connection;
|
|
8
|
+
};
|
|
9
|
+
this.wait = () => {
|
|
10
|
+
return this.promise;
|
|
11
|
+
};
|
|
12
|
+
this.close = async () => {
|
|
13
|
+
await this.connection.close();
|
|
14
|
+
};
|
|
15
|
+
const { connection, promise } = getMongooseMongoConnection(uri, dbName);
|
|
16
|
+
this.connection = connection;
|
|
17
|
+
this.promise = promise;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=MongooseMongoDBConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongooseMongoDBConnection.js","sourceRoot":"","sources":["../../src/mongo/MongooseMongoDBConnection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AACpC,OAAO,EAAwB,MAAM,YAAY,CAAC;AAClD,OAAO,EAAC,0BAA0B,EAAC,MAAM,YAAY,CAAC;AAEtD,MAAM,OAAO,yBAAyB;IAMpC,YAAY,GAAW,EAAE,MAAc;QAMvC,QAAG,GAAG,GAA6B,EAAE;YACnC,OAAO,IAAI,CAAC,UAAyB,CAAC;QACxC,CAAC,CAAC;QAEF,SAAI,GAAG,GAA6B,EAAE;YACpC,OAAO,IAAI,CAAC,OAA+B,CAAC;QAC9C,CAAC,CAAC;QAEF,UAAK,GAAG,KAAK,IAAI,EAAE;YACjB,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;QAChC,CAAC,CAAC;QAfA,MAAM,EAAC,UAAU,EAAE,OAAO,EAAC,GAAG,0BAA0B,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QACtE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CAaF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { noopAsync } from 'softkave-js-utils';
|
|
2
|
+
import type { MongoDBConnection } from './types.js';
|
|
3
|
+
export declare class NoopMongoDBConnection implements MongoDBConnection<undefined> {
|
|
4
|
+
get: <TConnection = undefined>() => TConnection;
|
|
5
|
+
wait: <TConnection = undefined>() => TConnection;
|
|
6
|
+
close: typeof noopAsync;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=NoopMongoDBConnection.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { noopAsync } from 'softkave-js-utils';
|
|
2
|
+
export class NoopMongoDBConnection {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.get = () => {
|
|
5
|
+
return undefined;
|
|
6
|
+
};
|
|
7
|
+
this.wait = () => {
|
|
8
|
+
return undefined;
|
|
9
|
+
};
|
|
10
|
+
this.close = noopAsync;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=NoopMongoDBConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NoopMongoDBConnection.js","sourceRoot":"","sources":["../../src/mongo/NoopMongoDBConnection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAG5C,MAAM,OAAO,qBAAqB;IAAlC;QACE,QAAG,GAAG,GAA4B,EAAE;YAClC,OAAO,SAAwB,CAAC;QAClC,CAAC,CAAC;QAEF,SAAI,GAAG,GAA4B,EAAE;YACnC,OAAO,SAAwB,CAAC;QAClC,CAAC,CAAC;QAEF,UAAK,GAAG,SAAS,CAAC;IACpB,CAAC;CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MongooseMongoDBConnection } from './MongooseMongoDBConnection.js';
|
|
2
|
+
export declare function getLazyMongooseMongoDBConnection(config?: {
|
|
3
|
+
uri: string;
|
|
4
|
+
dbName: string;
|
|
5
|
+
}): MongooseMongoDBConnection;
|
|
6
|
+
export declare function getLazyMongooseConnection(config?: {
|
|
7
|
+
uri: string;
|
|
8
|
+
dbName: string;
|
|
9
|
+
}): Promise<import("mongoose").Connection>;
|
|
10
|
+
//# sourceMappingURL=getLazyMongooseMongoDBConnection.d.ts.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MongooseMongoDBConnection } from './MongooseMongoDBConnection.js';
|
|
2
|
+
import { getOpinionatedMongoDBConfigFromEnv } from './getOpinionatedMongoDBConfig.js';
|
|
3
|
+
let connection = null;
|
|
4
|
+
export function getLazyMongooseMongoDBConnection(config) {
|
|
5
|
+
const { uri, dbName } = config ?? getOpinionatedMongoDBConfigFromEnv();
|
|
6
|
+
if (!connection) {
|
|
7
|
+
connection = new MongooseMongoDBConnection(uri, dbName);
|
|
8
|
+
}
|
|
9
|
+
return connection;
|
|
10
|
+
}
|
|
11
|
+
export function getLazyMongooseConnection(config) {
|
|
12
|
+
return getLazyMongooseMongoDBConnection(config).wait();
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=getLazyMongooseMongoDBConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getLazyMongooseMongoDBConnection.js","sourceRoot":"","sources":["../../src/mongo/getLazyMongooseMongoDBConnection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,yBAAyB,EAAC,MAAM,gCAAgC,CAAC;AACzE,OAAO,EAAC,kCAAkC,EAAC,MAAM,kCAAkC,CAAC;AAEpF,IAAI,UAAU,GAAqC,IAAI,CAAC;AAExD,MAAM,UAAU,gCAAgC,CAAC,MAGhD;IACC,MAAM,EAAC,GAAG,EAAE,MAAM,EAAC,GAAG,MAAM,IAAI,kCAAkC,EAAE,CAAC;IACrE,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,UAAU,GAAG,IAAI,yBAAyB,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAC1D,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,MAGzC;IACC,OAAO,gCAAgC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import assert from 'assert';
|
|
2
|
+
export function getOpinionatedMongoDBConfigFromEnv() {
|
|
3
|
+
const mongoDBURI = process.env.MONGO_DB_URI;
|
|
4
|
+
const mongoDBDatabaseName = process.env.MONGO_DB_DATABASE_NAME;
|
|
5
|
+
assert(mongoDBURI, 'MONGO_DB_URI is required');
|
|
6
|
+
assert(mongoDBDatabaseName, 'MONGO_DB_DATABASE_NAME is required');
|
|
7
|
+
return {
|
|
8
|
+
uri: mongoDBURI,
|
|
9
|
+
dbName: mongoDBDatabaseName,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=getOpinionatedMongoDBConfig.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getOpinionatedMongoDBConfig.js","sourceRoot":"","sources":["../../src/mongo/getOpinionatedMongoDBConfig.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,UAAU,kCAAkC;IAChD,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC;IAC5C,MAAM,mBAAmB,GAAG,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC;IAE/D,MAAM,CAAC,UAAU,EAAE,0BAA0B,CAAC,CAAC;IAC/C,MAAM,CAAC,mBAAmB,EAAE,oCAAoC,CAAC,CAAC;IAElE,OAAO;QACL,GAAG,EAAE,UAAU;QACf,MAAM,EAAE,mBAAmB;KAC5B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './getLazyMongooseMongoDBConnection.js';
|
|
2
|
+
export * from './getOpinionatedMongoDBConfig.js';
|
|
3
|
+
export * from './MongooseMongoDBConnection.js';
|
|
4
|
+
export * from './NoopMongoDBConnection.js';
|
|
5
|
+
export * from './types.js';
|
|
6
|
+
export * from './utils.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './getLazyMongooseMongoDBConnection.js';
|
|
2
|
+
export * from './getOpinionatedMongoDBConfig.js';
|
|
3
|
+
export * from './MongooseMongoDBConnection.js';
|
|
4
|
+
export * from './NoopMongoDBConnection.js';
|
|
5
|
+
export * from './types.js';
|
|
6
|
+
export * from './utils.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/mongo/index.ts"],"names":[],"mappings":"AAAA,cAAc,uCAAuC,CAAC;AACtD,cAAc,kCAAkC,CAAC;AACjD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/mongo/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ClientSession, type Connection, type SchemaDefinitionProperty } from 'mongoose';
|
|
2
|
+
export declare function getMongooseMongoConnection(uri: string, dbName: string): {
|
|
3
|
+
connection: Connection;
|
|
4
|
+
promise: Promise<Connection>;
|
|
5
|
+
};
|
|
6
|
+
export declare function isMongooseMongoConnection(connection: unknown): connection is Connection;
|
|
7
|
+
export declare function isMongooseMongoClientSession(session: unknown): session is ClientSession;
|
|
8
|
+
export declare function ensureMongoTypeFields<T extends object>(schema: {
|
|
9
|
+
[path in keyof Required<T>]: SchemaDefinitionProperty<T[path]>;
|
|
10
|
+
}): { [path in keyof Required<T>]: SchemaDefinitionProperty<T[path]>; };
|
|
11
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createConnection, } from 'mongoose';
|
|
2
|
+
export function getMongooseMongoConnection(uri, dbName) {
|
|
3
|
+
const connection = createConnection(uri, { dbName, autoIndex: false });
|
|
4
|
+
const promise = connection.asPromise();
|
|
5
|
+
return { connection, promise };
|
|
6
|
+
}
|
|
7
|
+
export function isMongooseMongoConnection(connection) {
|
|
8
|
+
// Not an exhaustive check, but useful enough
|
|
9
|
+
return !!connection.collections;
|
|
10
|
+
}
|
|
11
|
+
export function isMongooseMongoClientSession(session) {
|
|
12
|
+
return !!session?.toBSON;
|
|
13
|
+
}
|
|
14
|
+
// ensures all the fields defined in the type are added to the schema
|
|
15
|
+
// TODO: do deep check to make sure that internal schemas are checked too
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
17
|
+
export function ensureMongoTypeFields(schema) {
|
|
18
|
+
return schema;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/mongo/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,gBAAgB,GAEjB,MAAM,UAAU,CAAC;AAElB,MAAM,UAAU,0BAA0B,CAAC,GAAW,EAAE,MAAc;IACpE,MAAM,UAAU,GAAG,gBAAgB,CAAC,GAAG,EAAE,EAAC,MAAM,EAAE,SAAS,EAAE,KAAK,EAAC,CAAC,CAAC;IACrE,MAAM,OAAO,GAAG,UAAU,CAAC,SAAS,EAAE,CAAC;IACvC,OAAO,EAAC,UAAU,EAAE,OAAO,EAAC,CAAC;AAC/B,CAAC;AAED,MAAM,UAAU,yBAAyB,CACvC,UAAmB;IAEnB,6CAA6C;IAC7C,OAAO,CAAC,CAAE,UAAyB,CAAC,WAAW,CAAC;AAClD,CAAC;AAED,MAAM,UAAU,4BAA4B,CAC1C,OAAgB;IAEhB,OAAO,CAAC,CAAE,OAAyB,EAAE,MAAM,CAAC;AAC9C,CAAC;AAED,qEAAqE;AACrE,yEAAyE;AACzE,wDAAwD;AACxD,MAAM,UAAU,qBAAqB,CAAmB,MAEvD;IACC,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/next/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { NextRequest } from 'next/server.js';
|
|
2
|
+
import { type AnyFn, type AnyObject } from 'softkave-js-utils';
|
|
3
|
+
import type { JWTTokenContent } from '../server/index.js';
|
|
4
|
+
import { type IRouteContext } from './wrapRoute.js';
|
|
5
|
+
export interface JWTTokenAuthenticatedRequest {
|
|
6
|
+
jwtContent: JWTTokenContent;
|
|
7
|
+
}
|
|
8
|
+
export declare function wrapJWTTokenAuthenticated<T extends JWTTokenContent, TTransformJWTContent extends (decodedToken: JWTTokenContent) => Promise<T>>(params: {
|
|
9
|
+
routeFn: AnyFn<[
|
|
10
|
+
NextRequest,
|
|
11
|
+
IRouteContext,
|
|
12
|
+
Awaited<ReturnType<TTransformJWTContent>>
|
|
13
|
+
], Promise<void | AnyObject>>;
|
|
14
|
+
getJWTSecret: () => string;
|
|
15
|
+
transformJWTContent?: TTransformJWTContent;
|
|
16
|
+
}): (req: NextRequest, ctx: IRouteContext) => Promise<Response>;
|
|
17
|
+
//# sourceMappingURL=wrapAuthenticated.d.ts.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import assert from 'assert';
|
|
2
|
+
import jwt from 'jsonwebtoken';
|
|
3
|
+
import { isString } from 'lodash-es';
|
|
4
|
+
import { NextRequest } from 'next/server.js';
|
|
5
|
+
import {} from 'softkave-js-utils';
|
|
6
|
+
import { OwnServerError } from '../common/error.js';
|
|
7
|
+
import { wrapRoute } from './wrapRoute.js';
|
|
8
|
+
async function tryGetJWTTokenAuthenticatedRequest(params) {
|
|
9
|
+
const { req, getJWTSecret, transformJWTContent } = params;
|
|
10
|
+
const rawToken = req.headers.get('authorization');
|
|
11
|
+
if (!rawToken) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
const inputToken = rawToken.startsWith('Bearer ')
|
|
15
|
+
? rawToken.slice(7)
|
|
16
|
+
: rawToken;
|
|
17
|
+
assert(isString(inputToken), new OwnServerError('Unauthorized', 401));
|
|
18
|
+
try {
|
|
19
|
+
const decodedToken = jwt.verify(inputToken, getJWTSecret());
|
|
20
|
+
const authenticatedRequest = {
|
|
21
|
+
jwtContent: decodedToken,
|
|
22
|
+
};
|
|
23
|
+
const transformedRequest = transformJWTContent
|
|
24
|
+
? await transformJWTContent(decodedToken)
|
|
25
|
+
: authenticatedRequest;
|
|
26
|
+
return transformedRequest;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error instanceof jwt.JsonWebTokenError) {
|
|
30
|
+
throw new OwnServerError('Unauthorized', 401);
|
|
31
|
+
}
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function getJWTTokenAuthenticatedRequest(params) {
|
|
36
|
+
const jwtTokenAuthenticatedRequest = await tryGetJWTTokenAuthenticatedRequest(params);
|
|
37
|
+
assert(jwtTokenAuthenticatedRequest, new OwnServerError('Unauthorized', 401));
|
|
38
|
+
return jwtTokenAuthenticatedRequest;
|
|
39
|
+
}
|
|
40
|
+
export function wrapJWTTokenAuthenticated(params) {
|
|
41
|
+
return wrapRoute(async (req, ctx) => {
|
|
42
|
+
const jwtTokenAuthenticatedRequest = await getJWTTokenAuthenticatedRequest({
|
|
43
|
+
req,
|
|
44
|
+
getJWTSecret: params.getJWTSecret,
|
|
45
|
+
transformJWTContent: params.transformJWTContent,
|
|
46
|
+
});
|
|
47
|
+
return params.routeFn(req, ctx, jwtTokenAuthenticatedRequest);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=wrapAuthenticated.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapAuthenticated.js","sourceRoot":"","sources":["../../src/next/wrapAuthenticated.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,GAAG,MAAM,cAAc,CAAC;AAC/B,OAAO,EAAC,QAAQ,EAAC,MAAM,WAAW,CAAC;AACnC,OAAO,EAAC,WAAW,EAAC,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAA4B,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAC,cAAc,EAAC,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAqB,SAAS,EAAC,MAAM,gBAAgB,CAAC;AAM7D,KAAK,UAAU,kCAAkC,CAG/C,MAID;IACC,MAAM,EAAC,GAAG,EAAE,YAAY,EAAE,mBAAmB,EAAC,GAAG,MAAM,CAAC;IACxD,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAClD,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAC,SAAS,CAAC;QAC/C,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;QACnB,CAAC,CAAC,QAAQ,CAAC;IACb,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,IAAI,cAAc,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC,CAAC;IAEtE,IAAI,CAAC;QACH,MAAM,YAAY,GAAG,GAAG,CAAC,MAAM,CAC7B,UAAU,EACV,YAAY,EAAE,CACI,CAAC;QACrB,MAAM,oBAAoB,GAAiC;YACzD,UAAU,EAAE,YAAY;SACzB,CAAC;QACF,MAAM,kBAAkB,GAAG,mBAAmB;YAC5C,CAAC,CAAC,MAAM,mBAAmB,CAAC,YAAY,CAAC;YACzC,CAAC,CAAC,oBAAoB,CAAC;QACzB,OAAO,kBAA+D,CAAC;IACzE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,GAAG,CAAC,iBAAiB,EAAE,CAAC;YAC3C,MAAM,IAAI,cAAc,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC;QAChD,CAAC;QAED,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAED,KAAK,UAAU,+BAA+B,CAG5C,MAID;IACC,MAAM,4BAA4B,GAAG,MAAM,kCAAkC,CAG3E,MAAM,CAAC,CAAC;IACV,MAAM,CAAC,4BAA4B,EAAE,IAAI,cAAc,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC,CAAC;IAC9E,OAAO,4BAA4B,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,yBAAyB,CAGvC,MAOD;IACC,OAAO,SAAS,CAAC,KAAK,EAAE,GAAgB,EAAE,GAAkB,EAAE,EAAE;QAC9D,MAAM,4BAA4B,GAAG,MAAM,+BAA+B,CAAC;YACzE,GAAG;YACH,YAAY,EAAE,MAAM,CAAC,YAAY;YACjC,mBAAmB,EAAE,MAAM,CAAC,mBAAmB;SAChD,CAAC,CAAC;QACH,OAAO,MAAM,CAAC,OAAO,CACnB,GAAG,EACH,GAAG,EACH,4BAAyE,CAC1E,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { NextRequest } from 'next/server.js';
|
|
2
|
+
import type { AnyFn } from 'softkave-js-utils';
|
|
3
|
+
export interface IRouteContext<T = unknown> {
|
|
4
|
+
params: Promise<T>;
|
|
5
|
+
}
|
|
6
|
+
export declare const wrapRoute: <TRequest extends NextRequest>(routeFn: AnyFn) => (req: TRequest, ctx: IRouteContext) => Promise<Response>;
|
|
7
|
+
//# sourceMappingURL=wrapRoute.d.ts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { AssertionError } from 'assert';
|
|
2
|
+
import { ZodError } from 'zod';
|
|
3
|
+
import { OwnError, OwnServerError } from '../common/error.js';
|
|
4
|
+
import { fimidxConsoleLogger } from '../common/index.js';
|
|
5
|
+
export const wrapRoute = (routeFn) => async (req, ctx) => {
|
|
6
|
+
try {
|
|
7
|
+
// fimidxConsoleLogger.info("Route called", {
|
|
8
|
+
// path: req.nextUrl.pathname,
|
|
9
|
+
// method: req.method,
|
|
10
|
+
// params: ctx.params,
|
|
11
|
+
// });
|
|
12
|
+
let result = await routeFn(req, ctx);
|
|
13
|
+
result = result || {};
|
|
14
|
+
return Response.json(result, {
|
|
15
|
+
status: 200,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
fimidxConsoleLogger.error(error);
|
|
20
|
+
if (OwnServerError.isOwnServerError(error)) {
|
|
21
|
+
return Response.json({ message: error.message, name: 'OwnServerError' }, { status: error.statusCode });
|
|
22
|
+
}
|
|
23
|
+
else if (OwnError.isOwnError(error)) {
|
|
24
|
+
return Response.json({ message: error.message, name: 'OwnError' }, { status: 500 });
|
|
25
|
+
}
|
|
26
|
+
else if (error instanceof ZodError) {
|
|
27
|
+
const formattedErrors = error.format();
|
|
28
|
+
return Response.json({ message: formattedErrors, name: 'ZodError' }, { status: 400 });
|
|
29
|
+
}
|
|
30
|
+
else if (error instanceof AssertionError) {
|
|
31
|
+
return Response.json({ message: error.message, name: 'AssertionError' }, { status: 400 });
|
|
32
|
+
}
|
|
33
|
+
return Response.json({ message: 'Internal Server Error', name: 'UnknownError' }, { status: 500 });
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=wrapRoute.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapRoute.js","sourceRoot":"","sources":["../../src/next/wrapRoute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,QAAQ,CAAC;AAGtC,OAAO,EAAC,QAAQ,EAAC,MAAM,KAAK,CAAC;AAC7B,OAAO,EAAC,QAAQ,EAAE,cAAc,EAAC,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAC,mBAAmB,EAAC,MAAM,oBAAoB,CAAC;AAMvD,MAAM,CAAC,MAAM,SAAS,GACpB,CAA+B,OAAc,EAAE,EAAE,CACjD,KAAK,EAAE,GAAa,EAAE,GAAkB,EAAE,EAAE;IAC1C,IAAI,CAAC;QACH,6CAA6C;QAC7C,gCAAgC;QAChC,wBAAwB;QACxB,wBAAwB;QACxB,MAAM;QACN,IAAI,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACrC,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;QACtB,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE;YAC3B,MAAM,EAAE,GAAG;SACZ,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,mBAAmB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAEjC,IAAI,cAAc,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;YAC3C,OAAO,QAAQ,CAAC,IAAI,CAClB,EAAC,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAC,EAChD,EAAC,MAAM,EAAE,KAAK,CAAC,UAAU,EAAC,CAC3B,CAAC;QACJ,CAAC;aAAM,IAAI,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;YACtC,OAAO,QAAQ,CAAC,IAAI,CAClB,EAAC,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,UAAU,EAAC,EAC1C,EAAC,MAAM,EAAE,GAAG,EAAC,CACd,CAAC;QACJ,CAAC;aAAM,IAAI,KAAK,YAAY,QAAQ,EAAE,CAAC;YACrC,MAAM,eAAe,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;YACvC,OAAO,QAAQ,CAAC,IAAI,CAClB,EAAC,OAAO,EAAE,eAAe,EAAE,IAAI,EAAE,UAAU,EAAC,EAC5C,EAAC,MAAM,EAAE,GAAG,EAAC,CACd,CAAC;QACJ,CAAC;aAAM,IAAI,KAAK,YAAY,cAAc,EAAE,CAAC;YAC3C,OAAO,QAAQ,CAAC,IAAI,CAClB,EAAC,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAC,EAChD,EAAC,MAAM,EAAE,GAAG,EAAC,CACd,CAAC;QACJ,CAAC;QAED,OAAO,QAAQ,CAAC,IAAI,CAClB,EAAC,OAAO,EAAE,uBAAuB,EAAE,IAAI,EAAE,cAAc,EAAC,EACxD,EAAC,MAAM,EAAE,GAAG,EAAC,CACd,CAAC;IACJ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const kDefaultExpiresAtDurationMs: number;
|
|
2
|
+
export declare function encodeJWTToken(params: {
|
|
3
|
+
id: string;
|
|
4
|
+
refresh?: boolean;
|
|
5
|
+
expiresAt?: Date;
|
|
6
|
+
durationMs?: number;
|
|
7
|
+
otherContent?: Record<string, string>;
|
|
8
|
+
getJWTSecret: () => string;
|
|
9
|
+
}): Promise<{
|
|
10
|
+
token: string;
|
|
11
|
+
refreshToken: string | undefined;
|
|
12
|
+
}>;
|
|
13
|
+
//# sourceMappingURL=encodeJWTToken.d.ts.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
import jwt from 'jsonwebtoken';
|
|
3
|
+
import { v7 as uuidv7 } from 'uuid';
|
|
4
|
+
export const kDefaultExpiresAtDurationMs = 1000 * 60 * 60 * 24 * 30; // 30 days
|
|
5
|
+
export async function encodeJWTToken(params) {
|
|
6
|
+
const { id, refresh, expiresAt: inputExpiresAt, durationMs, otherContent, getJWTSecret, } = params;
|
|
7
|
+
const refreshToken = refresh
|
|
8
|
+
? createHash('sha256').update(uuidv7()).digest('hex')
|
|
9
|
+
: undefined;
|
|
10
|
+
const expiresAt = inputExpiresAt
|
|
11
|
+
? new Date(inputExpiresAt)
|
|
12
|
+
: refreshToken
|
|
13
|
+
? new Date(Date.now() + (durationMs ?? kDefaultExpiresAtDurationMs))
|
|
14
|
+
: undefined;
|
|
15
|
+
const duration = expiresAt ? expiresAt.getTime() - Date.now() : undefined;
|
|
16
|
+
const content = {
|
|
17
|
+
id,
|
|
18
|
+
refreshToken,
|
|
19
|
+
duration,
|
|
20
|
+
...otherContent,
|
|
21
|
+
};
|
|
22
|
+
const token = jwt.sign(content, getJWTSecret(), duration ? { expiresIn: `${duration}ms` } : undefined);
|
|
23
|
+
return { token, refreshToken };
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=encodeJWTToken.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encodeJWTToken.js","sourceRoot":"","sources":["../../../src/server/jwt/encodeJWTToken.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,QAAQ,CAAC;AAClC,OAAO,GAAG,MAAM,cAAc,CAAC;AAC/B,OAAO,EAAC,EAAE,IAAI,MAAM,EAAC,MAAM,MAAM,CAAC;AAGlC,MAAM,CAAC,MAAM,2BAA2B,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,UAAU;AAE/E,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,MAOpC;IACC,MAAM,EACJ,EAAE,EACF,OAAO,EACP,SAAS,EAAE,cAAc,EACzB,UAAU,EACV,YAAY,EACZ,YAAY,GACb,GAAG,MAAM,CAAC;IAEX,MAAM,YAAY,GAAG,OAAO;QAC1B,CAAC,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;QACrD,CAAC,CAAC,SAAS,CAAC;IACd,MAAM,SAAS,GAAG,cAAc;QAC9B,CAAC,CAAC,IAAI,IAAI,CAAC,cAAc,CAAC;QAC1B,CAAC,CAAC,YAAY;YACZ,CAAC,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,UAAU,IAAI,2BAA2B,CAAC,CAAC;YACpE,CAAC,CAAC,SAAS,CAAC;IAChB,MAAM,QAAQ,GAAG,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;IAC1E,MAAM,OAAO,GAAoB;QAC/B,EAAE;QACF,YAAY;QACZ,QAAQ;QACR,GAAG,YAAY;KAChB,CAAC;IACF,MAAM,KAAK,GAAG,GAAG,CAAC,IAAI,CACpB,OAAO,EACP,YAAY,EAAE,EACd,QAAQ,CAAC,CAAC,CAAC,EAAC,SAAS,EAAE,GAAG,QAAQ,IAAI,EAAC,CAAC,CAAC,CAAC,SAAS,CACpD,CAAC;IAEF,OAAO,EAAC,KAAK,EAAE,YAAY,EAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/jwt/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/server/jwt/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "softkave-node-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Softkave Node.js utilities",
|
|
5
5
|
"author": {
|
|
6
6
|
"email": "ywordk@gmail.com",
|
|
@@ -19,8 +19,20 @@
|
|
|
19
19
|
"type": "module",
|
|
20
20
|
"exports": {
|
|
21
21
|
"./common": {
|
|
22
|
-
"types": "./build/common.d.ts",
|
|
23
|
-
"default": "./build/common.js"
|
|
22
|
+
"types": "./build/common/index.d.ts",
|
|
23
|
+
"default": "./build/common/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./next": {
|
|
26
|
+
"types": "./build/next/index.d.ts",
|
|
27
|
+
"default": "./build/next/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./mongo": {
|
|
30
|
+
"types": "./build/mongo/index.d.ts",
|
|
31
|
+
"default": "./build/mongo/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./server": {
|
|
34
|
+
"types": "./build/server/index.d.ts",
|
|
35
|
+
"default": "./build/server/index.js"
|
|
24
36
|
}
|
|
25
37
|
},
|
|
26
38
|
"files": [
|
|
@@ -37,7 +49,7 @@
|
|
|
37
49
|
"scripts": {
|
|
38
50
|
"test": "vitest run --config ./vitest.config.ts",
|
|
39
51
|
"lint": "gts lint",
|
|
40
|
-
"clean": "gts clean",
|
|
52
|
+
"clean": "gts clean && rm -f tsconfig.tsbuildinfo",
|
|
41
53
|
"compile": "npm run clean && tsc",
|
|
42
54
|
"fix": "gts fix",
|
|
43
55
|
"pretest": "npm run compile",
|
|
@@ -57,10 +69,16 @@
|
|
|
57
69
|
"zod": "^4.1.11"
|
|
58
70
|
},
|
|
59
71
|
"devDependencies": {
|
|
72
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
60
73
|
"@typescript-eslint/eslint-plugin": "^6.13.2",
|
|
61
74
|
"@typescript-eslint/parser": "^6.13.2",
|
|
62
75
|
"gts": "^6.0.2",
|
|
63
76
|
"prettier": "^3.6.2",
|
|
64
77
|
"typescript": "^5.9.3"
|
|
78
|
+
},
|
|
79
|
+
"dependencies": {
|
|
80
|
+
"jsonwebtoken": "^9.0.2",
|
|
81
|
+
"mongoose": "^8.19.0",
|
|
82
|
+
"uuid": "^13.0.0"
|
|
65
83
|
}
|
|
66
84
|
}
|