@sdflc/backend-helpers 1.0.78 → 1.0.79
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/BaseCore.js +0 -3
- package/build/BaseGateway.js +0 -2
- package/build/RedisDataLoader.js +7 -12
- package/build/commonValidators.js +3 -9
- package/build/esm/BaseCore.d.ts +88 -0
- package/build/esm/BaseCore.js +810 -0
- package/build/esm/BaseGateway.d.ts +49 -0
- package/build/esm/BaseGateway.js +476 -0
- package/build/esm/DataCache.d.ts +14 -0
- package/build/esm/DataCache.js +98 -0
- package/build/esm/Database.d.ts +32 -0
- package/build/esm/Database.js +192 -0
- package/build/esm/DbMigratorAndSeeder.d.ts +49 -0
- package/build/esm/DbMigratorAndSeeder.js +199 -0
- package/build/esm/RedisDataLoader.d.ts +41 -0
- package/build/esm/RedisDataLoader.js +237 -0
- package/build/esm/apiContext.d.ts +11 -0
- package/build/esm/apiContext.js +135 -0
- package/build/esm/baseController.d.ts +17 -0
- package/build/esm/baseController.js +60 -0
- package/build/esm/checkItHelpers.d.ts +3 -0
- package/build/esm/checkItHelpers.js +23 -0
- package/build/esm/commonValidators.d.ts +17 -0
- package/build/esm/commonValidators.js +273 -0
- package/build/esm/configBulder.d.ts +2 -0
- package/build/esm/configBulder.js +28 -0
- package/build/esm/constants.d.ts +30 -0
- package/build/esm/constants.js +69 -0
- package/build/esm/dbHelpers.d.ts +43 -0
- package/build/esm/dbHelpers.js +154 -0
- package/build/esm/dbSeeder.d.ts +2 -0
- package/build/esm/dbSeeder.js +53 -0
- package/build/esm/dbTableHelpers.d.ts +11 -0
- package/build/esm/dbTableHelpers.js +99 -0
- package/build/esm/downloadHelpers.d.ts +2 -0
- package/build/esm/downloadHelpers.js +76 -0
- package/build/esm/index.d.ts +21 -0
- package/build/esm/index.js +21 -0
- package/build/esm/interfaces/ApiContext.d.ts +24 -0
- package/build/esm/interfaces/ApiContext.js +1 -0
- package/build/esm/interfaces/ApiContextInterface.d.ts +12 -0
- package/build/esm/interfaces/ApiContextInterface.js +1 -0
- package/build/esm/interfaces/BaseCoreActionsInterface.d.ts +38 -0
- package/build/esm/interfaces/BaseCoreActionsInterface.js +1 -0
- package/build/esm/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -0
- package/build/esm/interfaces/BaseCoreDoAuthOnActionInterface.js +1 -0
- package/build/esm/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -0
- package/build/esm/interfaces/BaseCoreDoingWhatInterface.js +1 -0
- package/build/esm/interfaces/BaseCorePropsInterface.d.ts +36 -0
- package/build/esm/interfaces/BaseCorePropsInterface.js +1 -0
- package/build/esm/interfaces/BaseCoreValidatorsInterface.d.ts +13 -0
- package/build/esm/interfaces/BaseCoreValidatorsInterface.js +1 -0
- package/build/esm/interfaces/BaseGatewayPropsInterface.d.ts +39 -0
- package/build/esm/interfaces/BaseGatewayPropsInterface.js +1 -0
- package/build/esm/interfaces/BuildConfigOptions.d.ts +6 -0
- package/build/esm/interfaces/BuildConfigOptions.js +1 -0
- package/build/esm/interfaces/BuildDefaultResolversOptions.d.ts +17 -0
- package/build/esm/interfaces/BuildDefaultResolversOptions.js +1 -0
- package/build/esm/interfaces/BuildListQueryArgsInterface.d.ts +6 -0
- package/build/esm/interfaces/BuildListQueryArgsInterface.js +1 -0
- package/build/esm/interfaces/CacheInterface.d.ts +15 -0
- package/build/esm/interfaces/CacheInterface.js +1 -0
- package/build/esm/interfaces/CommonValidatorArgs.d.ts +3 -0
- package/build/esm/interfaces/CommonValidatorArgs.js +1 -0
- package/build/esm/interfaces/CoreActionInterface.d.ts +14 -0
- package/build/esm/interfaces/CoreActionInterface.js +1 -0
- package/build/esm/interfaces/CreateUserTokenArgs.d.ts +11 -0
- package/build/esm/interfaces/CreateUserTokenArgs.js +1 -0
- package/build/esm/interfaces/CreateUserTokenResult.d.ts +4 -0
- package/build/esm/interfaces/CreateUserTokenResult.js +1 -0
- package/build/esm/interfaces/DatabaseOptions.d.ts +22 -0
- package/build/esm/interfaces/DatabaseOptions.js +1 -0
- package/build/esm/interfaces/DatabaseReadInterface.d.ts +14 -0
- package/build/esm/interfaces/DatabaseReadInterface.js +1 -0
- package/build/esm/interfaces/DbFieldAddDefaultInterface.d.ts +9 -0
- package/build/esm/interfaces/DbFieldAddDefaultInterface.js +1 -0
- package/build/esm/interfaces/DownloadFileArgs.d.ts +6 -0
- package/build/esm/interfaces/DownloadFileArgs.js +1 -0
- package/build/esm/interfaces/ExtractCredentialsArgs.d.ts +19 -0
- package/build/esm/interfaces/ExtractCredentialsArgs.js +1 -0
- package/build/esm/interfaces/JwtTokenSecretAndSettings.d.ts +5 -0
- package/build/esm/interfaces/JwtTokenSecretAndSettings.js +1 -0
- package/build/esm/interfaces/ListArgsInterface.d.ts +5 -0
- package/build/esm/interfaces/ListArgsInterface.js +1 -0
- package/build/esm/interfaces/LoaderInterface.d.ts +4 -0
- package/build/esm/interfaces/LoaderInterface.js +1 -0
- package/build/esm/interfaces/PaginationInterface.d.ts +7 -0
- package/build/esm/interfaces/PaginationInterface.js +1 -0
- package/build/esm/interfaces/SortItemInterface.d.ts +4 -0
- package/build/esm/interfaces/SortItemInterface.js +1 -0
- package/build/esm/interfaces/UserTokenParams.d.ts +8 -0
- package/build/esm/interfaces/UserTokenParams.js +1 -0
- package/build/esm/interfaces/VerifyUserTokenArgs.d.ts +13 -0
- package/build/esm/interfaces/VerifyUserTokenArgs.js +1 -0
- package/build/esm/interfaces/index.d.ts +28 -0
- package/build/esm/interfaces/index.js +27 -0
- package/build/esm/knexConfigBuilder.d.ts +1 -0
- package/build/esm/knexConfigBuilder.js +73 -0
- package/build/esm/passwordHelpers.d.ts +5 -0
- package/build/esm/passwordHelpers.js +22 -0
- package/build/esm/redisClient.d.ts +17 -0
- package/build/esm/redisClient.js +94 -0
- package/build/esm/resolversHelpers.d.ts +3 -0
- package/build/esm/resolversHelpers.js +59 -0
- package/build/esm/userToken.d.ts +8 -0
- package/build/esm/userToken.js +74 -0
- package/build/esm/utils.d.ts +2 -0
- package/build/esm/utils.js +13 -0
- package/package.json +13 -5
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface ExtractCredentialsArgs {
|
|
2
|
+
/**
|
|
3
|
+
* Req is a request object of the Express library
|
|
4
|
+
*/
|
|
5
|
+
req: any;
|
|
6
|
+
/**
|
|
7
|
+
* Space is an object with configuration where to get data from
|
|
8
|
+
*/
|
|
9
|
+
space: any;
|
|
10
|
+
/**
|
|
11
|
+
* Authorization is a token from the Authorization header
|
|
12
|
+
*/
|
|
13
|
+
authorization?: string;
|
|
14
|
+
fingerprint?: string;
|
|
15
|
+
/**
|
|
16
|
+
* An object with parameters passed to the authenticate methods
|
|
17
|
+
*/
|
|
18
|
+
params?: any;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface VerifyUserTokenArgs {
|
|
2
|
+
algorithms?: any;
|
|
3
|
+
audience?: string | string[];
|
|
4
|
+
issuer?: string | string[];
|
|
5
|
+
ignoreExpiration?: boolean;
|
|
6
|
+
ignoreNotBefore?: boolean;
|
|
7
|
+
subject?: string;
|
|
8
|
+
clockTolerance?: number;
|
|
9
|
+
maxAge?: number;
|
|
10
|
+
clockTimestamp?: number;
|
|
11
|
+
nonce?: string;
|
|
12
|
+
fingerprint?: string;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export { CacheInterface } from './CacheInterface';
|
|
2
|
+
export * from './LoaderInterface';
|
|
3
|
+
export * from './BaseGatewayPropsInterface';
|
|
4
|
+
export * from './ListArgsInterface';
|
|
5
|
+
export * from './SortItemInterface';
|
|
6
|
+
export * from './PaginationInterface';
|
|
7
|
+
export * from './BaseCorePropsInterface';
|
|
8
|
+
export * from './CoreActionInterface';
|
|
9
|
+
export * from './ApiContextInterface';
|
|
10
|
+
export * from './ApiContext';
|
|
11
|
+
export * from './DbFieldAddDefaultInterface';
|
|
12
|
+
export * from './BuildConfigOptions';
|
|
13
|
+
export * from './DatabaseOptions';
|
|
14
|
+
export * from './DatabaseReadInterface';
|
|
15
|
+
export * from './BuildDefaultResolversOptions';
|
|
16
|
+
export * from './BaseCoreValidatorsInterface';
|
|
17
|
+
export * from './UserTokenParams';
|
|
18
|
+
export * from './CreateUserTokenArgs';
|
|
19
|
+
export * from './VerifyUserTokenArgs';
|
|
20
|
+
export * from './BaseCoreActionsInterface';
|
|
21
|
+
export * from './BaseCoreDoingWhatInterface';
|
|
22
|
+
export * from './JwtTokenSecretAndSettings';
|
|
23
|
+
export * from './CommonValidatorArgs';
|
|
24
|
+
export * from './CreateUserTokenResult';
|
|
25
|
+
export * from './ExtractCredentialsArgs';
|
|
26
|
+
export * from './DownloadFileArgs';
|
|
27
|
+
export * from './BuildListQueryArgsInterface';
|
|
28
|
+
export * from './BaseCoreDoAuthOnActionInterface';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export * from './LoaderInterface';
|
|
2
|
+
export * from './BaseGatewayPropsInterface';
|
|
3
|
+
export * from './ListArgsInterface';
|
|
4
|
+
export * from './SortItemInterface';
|
|
5
|
+
export * from './PaginationInterface';
|
|
6
|
+
export * from './BaseCorePropsInterface';
|
|
7
|
+
export * from './CoreActionInterface';
|
|
8
|
+
export * from './ApiContextInterface';
|
|
9
|
+
export * from './ApiContext';
|
|
10
|
+
export * from './DbFieldAddDefaultInterface';
|
|
11
|
+
export * from './BuildConfigOptions';
|
|
12
|
+
export * from './DatabaseOptions';
|
|
13
|
+
export * from './DatabaseReadInterface';
|
|
14
|
+
export * from './BuildDefaultResolversOptions';
|
|
15
|
+
export * from './BaseCoreValidatorsInterface';
|
|
16
|
+
export * from './UserTokenParams';
|
|
17
|
+
export * from './CreateUserTokenArgs';
|
|
18
|
+
export * from './VerifyUserTokenArgs';
|
|
19
|
+
export * from './BaseCoreActionsInterface';
|
|
20
|
+
export * from './BaseCoreDoingWhatInterface';
|
|
21
|
+
export * from './JwtTokenSecretAndSettings';
|
|
22
|
+
export * from './CommonValidatorArgs';
|
|
23
|
+
export * from './CreateUserTokenResult';
|
|
24
|
+
export * from './ExtractCredentialsArgs';
|
|
25
|
+
export * from './DownloadFileArgs';
|
|
26
|
+
export * from './BuildListQueryArgsInterface';
|
|
27
|
+
export * from './BaseCoreDoAuthOnActionInterface';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const buildKnexConfig: (dirName: string, appConfig: any) => any;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { snakeCase, get } from 'lodash';
|
|
3
|
+
import { camelKeys } from '@sdflc/utils';
|
|
4
|
+
const defaultMigrationsTable = 'migrations';
|
|
5
|
+
const defaultMigrationsFolder = 'migrations';
|
|
6
|
+
const defaultMigrationsTemplateFileName = 'migrations-template.js';
|
|
7
|
+
const defaultSeedsFolder = 'seeds';
|
|
8
|
+
const camelResponse = (result) => {
|
|
9
|
+
if (!result || result.rows) {
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
return camelKeys(result);
|
|
13
|
+
};
|
|
14
|
+
export const buildKnexConfig = (dirName, appConfig) => {
|
|
15
|
+
const { dbType, dbHost, dbPort, dbName, dbSchema, dbUser, dbPassword, dbPoolMin, dbPoolMax, debug, nodeEnv, migrationsTemplateFileName, migrationsFolder, migrationsTable, useSchemaForMigrationsTable, seedsFolder, seedsFolderDev, seedsFolderProd, wrapIdentifierOverrideMap, dbPool, } = appConfig;
|
|
16
|
+
const sharedConfig = {
|
|
17
|
+
client: dbType || 'pg',
|
|
18
|
+
postProcessResponse: camelResponse,
|
|
19
|
+
wrapIdentifier: (value, origImpl) => {
|
|
20
|
+
if (value === '*') {
|
|
21
|
+
return origImpl(value);
|
|
22
|
+
}
|
|
23
|
+
const value2use = (wrapIdentifierOverrideMap === null || wrapIdentifierOverrideMap === void 0 ? void 0 : wrapIdentifierOverrideMap[value]) || origImpl(snakeCase(value));
|
|
24
|
+
return value2use;
|
|
25
|
+
},
|
|
26
|
+
connection: {
|
|
27
|
+
host: dbHost,
|
|
28
|
+
port: dbPort,
|
|
29
|
+
user: dbUser,
|
|
30
|
+
password: dbPassword,
|
|
31
|
+
database: dbName,
|
|
32
|
+
//ssl: database.ssl,
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
if (typeof dbPool === 'object') {
|
|
36
|
+
sharedConfig.pool = dbPool;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
sharedConfig.pool = {
|
|
40
|
+
min: dbPoolMin != null && !isNaN(dbPoolMin) ? dbPoolMin : 0,
|
|
41
|
+
max: dbPoolMax != null && !isNaN(dbPoolMax) ? dbPoolMax : 50,
|
|
42
|
+
acquireTimeoutMillis: 30000,
|
|
43
|
+
createTimeoutMillis: 30000,
|
|
44
|
+
destroyTimeoutMillis: 5000,
|
|
45
|
+
idleTimeoutMillis: 30000,
|
|
46
|
+
reapIntervalMillis: 1000,
|
|
47
|
+
createRetryIntervalMillis: 200,
|
|
48
|
+
propagateCreateError: false,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const config = {
|
|
52
|
+
development: Object.assign(Object.assign({}, sharedConfig), { debug, migrations: {
|
|
53
|
+
stub: path.join(dirName, migrationsTemplateFileName || defaultMigrationsTemplateFileName),
|
|
54
|
+
directory: path.join(dirName, migrationsFolder || defaultMigrationsFolder),
|
|
55
|
+
tableName: migrationsTable ||
|
|
56
|
+
`${useSchemaForMigrationsTable ? `${dbSchema}.` : ''}${migrationsTable || defaultMigrationsTable}`,
|
|
57
|
+
}, seeds: {
|
|
58
|
+
directory: path.join(dirName, seedsFolderDev || seedsFolder || defaultSeedsFolder),
|
|
59
|
+
} }),
|
|
60
|
+
production: Object.assign(Object.assign({}, sharedConfig), { debug, migrations: {
|
|
61
|
+
directory: path.join(dirName, migrationsFolder || defaultMigrationsFolder),
|
|
62
|
+
tableName: migrationsTable ||
|
|
63
|
+
`${useSchemaForMigrationsTable ? `${dbSchema}.` : ''}${migrationsTable || defaultMigrationsTable}`,
|
|
64
|
+
}, seeds: {
|
|
65
|
+
directory: path.join(dirName, seedsFolderProd || seedsFolder || defaultSeedsFolder),
|
|
66
|
+
} }),
|
|
67
|
+
};
|
|
68
|
+
let config2use = get(config, nodeEnv);
|
|
69
|
+
if (!config2use) {
|
|
70
|
+
config2use = config.development;
|
|
71
|
+
}
|
|
72
|
+
return config2use;
|
|
73
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import bcrypt from 'bcrypt';
|
|
11
|
+
export const getSaltAndHash = (password_1, ...args_1) => __awaiter(void 0, [password_1, ...args_1], void 0, function* (password, saltRounds = 10) {
|
|
12
|
+
const salt = yield bcrypt.genSalt(saltRounds);
|
|
13
|
+
const hash = yield bcrypt.hash(password, salt);
|
|
14
|
+
return {
|
|
15
|
+
salt,
|
|
16
|
+
hash,
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
export const checkPassword = (password, hashedPassword) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
const isCorrect = yield bcrypt.compare(password, hashedPassword);
|
|
21
|
+
return isCorrect;
|
|
22
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface CreateRedisClientInterface {
|
|
2
|
+
redisHost: string;
|
|
3
|
+
redisPort: string | number;
|
|
4
|
+
redisPassword: string;
|
|
5
|
+
redisSsl: boolean;
|
|
6
|
+
retryAttempts?: number;
|
|
7
|
+
firstTimeout?: number;
|
|
8
|
+
pingInterval?: number;
|
|
9
|
+
connectionTimeout?: number;
|
|
10
|
+
onConnecting?(): void;
|
|
11
|
+
onConnected?(): void;
|
|
12
|
+
onDisconnected?(): void;
|
|
13
|
+
onReconnecting?(): void;
|
|
14
|
+
onError?(err: any): void;
|
|
15
|
+
}
|
|
16
|
+
declare const createRedisClient: (args: CreateRedisClientInterface) => any;
|
|
17
|
+
export { createRedisClient };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { createClient } from 'redis';
|
|
2
|
+
// const DEFAULT_RETRY_ATTEMPTS = 10;
|
|
3
|
+
// const DEFAULT_FIRST_TIMEOUT = 3000;
|
|
4
|
+
const createRedisClient = (args) => {
|
|
5
|
+
const { redisHost, redisPort, redisPassword, redisSsl,
|
|
6
|
+
//retryAttempts,
|
|
7
|
+
//firstTimeout,
|
|
8
|
+
pingInterval, connectionTimeout, onConnecting, onConnected, onDisconnected, onReconnecting, onError, } = args || {};
|
|
9
|
+
if (!redisHost) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
const cfg = {
|
|
13
|
+
socket: {
|
|
14
|
+
host: redisHost,
|
|
15
|
+
port: Number(redisPort),
|
|
16
|
+
family: 0,
|
|
17
|
+
connectTimeout: connectionTimeout != undefined ? connectionTimeout : 5000,
|
|
18
|
+
noDelay: true,
|
|
19
|
+
keepAlive: pingInterval != undefined ? pingInterval : 5000,
|
|
20
|
+
tls: redisSsl,
|
|
21
|
+
rejectUnauthorized: false,
|
|
22
|
+
reconnectStrategy: (retries) => Math.min(retries * 50, 500),
|
|
23
|
+
},
|
|
24
|
+
password: redisPassword || undefined,
|
|
25
|
+
};
|
|
26
|
+
const redisClient = createClient(cfg);
|
|
27
|
+
redisClient.on('connect', () => {
|
|
28
|
+
if (typeof onConnecting === 'function') {
|
|
29
|
+
onConnecting();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
redisClient.on('ready', () => {
|
|
33
|
+
if (typeof onConnected === 'function') {
|
|
34
|
+
onConnected();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
redisClient.on('end', () => {
|
|
38
|
+
if (typeof onDisconnected === 'function') {
|
|
39
|
+
onDisconnected();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
redisClient.on('reconnecting', () => {
|
|
43
|
+
if (typeof onReconnecting === 'function') {
|
|
44
|
+
onReconnecting();
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
redisClient.on('error', (err) => {
|
|
48
|
+
if (typeof onError === 'function') {
|
|
49
|
+
onError(err);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
// const redisClientOld = redis.createClient({
|
|
53
|
+
// host: redisHost,
|
|
54
|
+
// port: Number(redisPort),
|
|
55
|
+
// password: redisPassword || undefined,
|
|
56
|
+
// no_ready_check: true,
|
|
57
|
+
// retry_strategy: function (options) {
|
|
58
|
+
// if (options.error && options.error.code === 'ECONNREFUSED') {
|
|
59
|
+
// // End reconnecting on a specific error and flush all commands with
|
|
60
|
+
// // a individual error
|
|
61
|
+
// return new Error('The server refused the connection');
|
|
62
|
+
// }
|
|
63
|
+
// if (options.total_retry_time > 1000 * 60 * 60) {
|
|
64
|
+
// // End reconnecting after a specific timeout and flush all commands
|
|
65
|
+
// // with a individual error
|
|
66
|
+
// return new Error('Retry time exhausted');
|
|
67
|
+
// }
|
|
68
|
+
// const attempts =
|
|
69
|
+
// !retryAttempts || retryAttempts == null || isNaN(retryAttempts)
|
|
70
|
+
// ? DEFAULT_RETRY_ATTEMPTS
|
|
71
|
+
// : Number(retryAttempts);
|
|
72
|
+
// if (options.attempt > attempts) {
|
|
73
|
+
// // End reconnecting with built in error
|
|
74
|
+
// return undefined;
|
|
75
|
+
// }
|
|
76
|
+
// const timeout =
|
|
77
|
+
// !firstTimeout || firstTimeout == null || isNaN(firstTimeout) ? DEFAULT_FIRST_TIMEOUT : Number(firstTimeout);
|
|
78
|
+
// // reconnect after
|
|
79
|
+
// return Math.min(options.attempt * 100, timeout);
|
|
80
|
+
// },
|
|
81
|
+
// tls: redisSsl
|
|
82
|
+
// ? {
|
|
83
|
+
// servername: redisHost,
|
|
84
|
+
// }
|
|
85
|
+
// : undefined,
|
|
86
|
+
// });
|
|
87
|
+
// if (pingInterval && !isNaN(pingInterval) && pingInterval > 1000) {
|
|
88
|
+
// setInterval(async () => {
|
|
89
|
+
// redisClient.ping();
|
|
90
|
+
// }, pingInterval);
|
|
91
|
+
// }
|
|
92
|
+
return redisClient;
|
|
93
|
+
};
|
|
94
|
+
export { createRedisClient };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const buildDefaultResolvers = (args) => {
|
|
2
|
+
const { prefix, core, query, mutation, types, disableList, disableGet, disableGetMany, disableCreate, disableCreateMany, disableUpdate, disableUpdateMany, disableSet, disableRemove, disableRemoveMany, } = args || {};
|
|
3
|
+
const Query = Object.assign({}, (query || {}));
|
|
4
|
+
const Mutation = Object.assign({}, (mutation || {}));
|
|
5
|
+
const coreToUse = core || `${prefix}Core`;
|
|
6
|
+
if (disableList !== true) {
|
|
7
|
+
Query[`${prefix}List`] = (parent, args, context) => {
|
|
8
|
+
return context.cores[coreToUse].list(args);
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
if (disableGet !== true) {
|
|
12
|
+
Query[`${prefix}Get`] = (parent, args, context) => {
|
|
13
|
+
return context.cores[coreToUse].get(args);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
if (disableGetMany !== true) {
|
|
17
|
+
Query[`${prefix}GetMany`] = (parent, args, context) => {
|
|
18
|
+
return context.cores[coreToUse].getMany(args);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (disableCreate !== true) {
|
|
22
|
+
Mutation[`${prefix}Create`] = (parent, args, context) => {
|
|
23
|
+
return context.cores[coreToUse].create(args);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
if (disableCreateMany !== true) {
|
|
27
|
+
Mutation[`${prefix}CreateMany`] = (parent, args, context) => {
|
|
28
|
+
return context.cores[coreToUse].createMany(args);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (disableUpdate !== true) {
|
|
32
|
+
Mutation[`${prefix}Update`] = (parent, args, context) => {
|
|
33
|
+
return context.cores[coreToUse].update(args);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
if (disableUpdateMany !== true) {
|
|
37
|
+
Mutation[`${prefix}UpdateMany`] = (parent, args, context) => {
|
|
38
|
+
return context.cores[coreToUse].updateMany(args);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
if (disableSet !== true) {
|
|
42
|
+
Mutation[`${prefix}Set`] = (parent, args, context) => {
|
|
43
|
+
return context.cores[coreToUse].set(args);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
if (disableRemove !== true) {
|
|
47
|
+
Mutation[`${prefix}Remove`] = (parent, args, context) => {
|
|
48
|
+
return context.cores[coreToUse].remove(args);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
if (disableRemoveMany !== true) {
|
|
52
|
+
Mutation[`${prefix}RemoveMany`] = (parent, args, context) => {
|
|
53
|
+
return context.cores[coreToUse].removeMany(args);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return Object.assign({ Query,
|
|
57
|
+
Mutation }, (types || {}));
|
|
58
|
+
};
|
|
59
|
+
export { buildDefaultResolvers };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { OpResult } from '@sdflc/api-helpers';
|
|
2
|
+
import { UserTokenParams, CreateUserTokenArgs, VerifyUserTokenArgs } from './interfaces';
|
|
3
|
+
export declare const setDefaultJwtAlgorithm: (algorithm: string) => void;
|
|
4
|
+
export declare const getDefaultJwtAlgorithm: () => string;
|
|
5
|
+
export declare const setDefaultJwtIssuer: (issuer: string) => void;
|
|
6
|
+
export declare const getDefaultJwtIssuer: () => string;
|
|
7
|
+
export declare const createUserToken: (params: UserTokenParams, secret: string, opt: CreateUserTokenArgs) => any;
|
|
8
|
+
export declare const verifyUserToken: (token: string, secret: string, args?: VerifyUserTokenArgs) => OpResult;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import jwt from 'jsonwebtoken';
|
|
2
|
+
import { v4 as uuid } from 'uuid';
|
|
3
|
+
import { OpResult, OP_RESULT_CODES } from '@sdflc/api-helpers';
|
|
4
|
+
import { DEFAULT_JWT_ISSUER, DEFAULT_JWT_ALGORITHM } from './constants';
|
|
5
|
+
import dayjs from 'dayjs';
|
|
6
|
+
let defaultJwtAlgorithm = DEFAULT_JWT_ALGORITHM;
|
|
7
|
+
let defaultIssuer = DEFAULT_JWT_ISSUER;
|
|
8
|
+
export const setDefaultJwtAlgorithm = (algorithm) => {
|
|
9
|
+
defaultJwtAlgorithm = algorithm;
|
|
10
|
+
};
|
|
11
|
+
export const getDefaultJwtAlgorithm = () => {
|
|
12
|
+
return defaultJwtAlgorithm;
|
|
13
|
+
};
|
|
14
|
+
export const setDefaultJwtIssuer = (issuer) => {
|
|
15
|
+
defaultIssuer = issuer;
|
|
16
|
+
};
|
|
17
|
+
export const getDefaultJwtIssuer = () => {
|
|
18
|
+
return defaultIssuer;
|
|
19
|
+
};
|
|
20
|
+
export const createUserToken = (params, secret, opt) => {
|
|
21
|
+
const { algorithm, audience, issuer, jwtid, noTimestamp, header, keyid, expiresIn, useFingerprint } = opt || {};
|
|
22
|
+
if (!params || !secret) {
|
|
23
|
+
return OpResult.fail(OP_RESULT_CODES.VALIDATION_FAILED, null, 'Failed to create user JWT');
|
|
24
|
+
}
|
|
25
|
+
if (useFingerprint) {
|
|
26
|
+
const fingerprint = uuid();
|
|
27
|
+
params['fingerprint'] = fingerprint;
|
|
28
|
+
}
|
|
29
|
+
const token = jwt.sign(params, secret, {
|
|
30
|
+
algorithm: algorithm || defaultJwtAlgorithm,
|
|
31
|
+
audience,
|
|
32
|
+
issuer: issuer || defaultIssuer,
|
|
33
|
+
jwtid: jwtid || uuid(),
|
|
34
|
+
subject: params.userId,
|
|
35
|
+
noTimestamp: noTimestamp !== null && noTimestamp !== void 0 ? noTimestamp : false,
|
|
36
|
+
header,
|
|
37
|
+
keyid,
|
|
38
|
+
expiresIn: expiresIn && !isNaN(expiresIn) ? expiresIn : undefined,
|
|
39
|
+
});
|
|
40
|
+
return OpResult.ok({ token, fingerprint: params.fingerprint });
|
|
41
|
+
};
|
|
42
|
+
export const verifyUserToken = (token, secret, args) => {
|
|
43
|
+
const { algorithms, audience, issuer, ignoreExpiration, ignoreNotBefore, subject, clockTolerance, maxAge, clockTimestamp, nonce, fingerprint, } = args || {};
|
|
44
|
+
if (!args) {
|
|
45
|
+
return OpResult.fail(OP_RESULT_CODES.VALIDATION_FAILED, null, 'Cannot verify user token as there are no parameters specified');
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const obj = jwt.verify(token, secret, {
|
|
49
|
+
algorithms: algorithms || [defaultJwtAlgorithm],
|
|
50
|
+
audience,
|
|
51
|
+
issuer: issuer || defaultIssuer,
|
|
52
|
+
ignoreExpiration,
|
|
53
|
+
ignoreNotBefore,
|
|
54
|
+
subject,
|
|
55
|
+
clockTolerance,
|
|
56
|
+
maxAge,
|
|
57
|
+
clockTimestamp,
|
|
58
|
+
nonce,
|
|
59
|
+
});
|
|
60
|
+
if ((obj === null || obj === void 0 ? void 0 : obj.fingerprint) && obj.fingerprint != fingerprint) {
|
|
61
|
+
return OpResult.fail(OP_RESULT_CODES.UNAUTHORIZED, null, 'User unauthenticated');
|
|
62
|
+
}
|
|
63
|
+
return OpResult.ok(obj);
|
|
64
|
+
}
|
|
65
|
+
catch (ex) {
|
|
66
|
+
if (ex.name === 'TokenExpiredError') {
|
|
67
|
+
return OpResult.fail(OP_RESULT_CODES.EXPIRED, null, `User authorization has expired at ${dayjs(ex.expiredAt).toDate()}`);
|
|
68
|
+
}
|
|
69
|
+
else if (ex.name === 'JsonWebTokenError') {
|
|
70
|
+
return OpResult.fail(OP_RESULT_CODES.EXPIRED, null, `JWT is invalid, reason: ${ex.message}`);
|
|
71
|
+
}
|
|
72
|
+
return OpResult.fail(OP_RESULT_CODES.UNAUTHORIZED, null, 'User unauthenticated');
|
|
73
|
+
}
|
|
74
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import check from 'check-types';
|
|
2
|
+
export const setFunction = (func, defaultFunc) => {
|
|
3
|
+
return check.function(func) ? func : defaultFunc;
|
|
4
|
+
};
|
|
5
|
+
export const setFunctions = (obj, defaultFuncs = {}) => {
|
|
6
|
+
const keys = Object.keys(defaultFuncs || {});
|
|
7
|
+
return keys.reduce((acc, key) => {
|
|
8
|
+
if (check.function(obj[key])) {
|
|
9
|
+
acc[key] = obj[key];
|
|
10
|
+
}
|
|
11
|
+
return acc;
|
|
12
|
+
}, {});
|
|
13
|
+
};
|
package/package.json
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdflc/backend-helpers",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"type": "module",
|
|
3
|
+
"version": "1.0.79",
|
|
5
4
|
"description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
|
|
6
|
-
"
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./build/esm/index.js",
|
|
9
|
+
"require": "./build/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"main": "./build/index.js",
|
|
13
|
+
"module": "./build/esm/index.js",
|
|
7
14
|
"types": "build/index.d.ts",
|
|
8
15
|
"files": [
|
|
9
16
|
"build/**/*"
|
|
10
17
|
],
|
|
11
18
|
"scripts": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
19
|
+
"build1": "rimraf ./build && tsc",
|
|
20
|
+
"build": "rimraf ./build && tsc && tsc -p tsconfig.esm.json",
|
|
21
|
+
"lint": "ESLINT_USE_FLAT_CONFIG=true eslint .",
|
|
14
22
|
"test": "echo No Tests Available Yet",
|
|
15
23
|
"testToDo": "jest --config jest.config.ts",
|
|
16
24
|
"format": "prettier --write \"src/**/*.ts\"",
|