pangea-server 1.0.38 → 1.0.39

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.
@@ -2,16 +2,16 @@ import { AccessToken } from './access-token.class';
2
2
  import { User } from '../database';
3
3
  import type { UserCtor, AuthMap } from './authentication.types';
4
4
  type AuthHeader = string | undefined;
5
- export declare function login<U extends User>(authHeader: AuthHeader, tx: Tx, accessToken: AccessToken, userCtor: BaseModelCtor<U>, extraWhere?: Where<U>): Promise<{
5
+ export declare function login<U extends User>(authHeader: AuthHeader, db: Db, accessToken: AccessToken, userCtor: BaseModelCtor<U>, extraWhere?: Where<U>): Promise<{
6
6
  accessToken: string;
7
7
  user: User;
8
8
  }>;
9
- export declare function validateAccessToken(authHeader: AuthHeader, tx: Tx, accessToken: AccessToken, userCtor: UserCtor): Promise<{
9
+ export declare function validateAccessToken(authHeader: AuthHeader, db: Db, accessToken: AccessToken, userCtor: UserCtor): Promise<{
10
10
  accessToken: string;
11
11
  user: User;
12
12
  }>;
13
- export declare function getUserFromToken(authHeader: AuthHeader, tx: Tx, accessToken: AccessToken, userCtor: UserCtor): Promise<User | null> | null;
14
- export declare function getUsersFromToken(authHeader: AuthHeader, tx: Tx, accessToken: AccessToken, authMap: AuthMap): Promise<{
13
+ export declare function getUserFromToken(authHeader: AuthHeader, db: Db, accessToken: AccessToken, userCtor: UserCtor): Promise<User | null> | null;
14
+ export declare function getUsersFromToken(authHeader: AuthHeader, db: Db, accessToken: AccessToken, authMap: AuthMap): Promise<{
15
15
  [k: string]: User | null;
16
16
  }>;
17
17
  export {};
@@ -3,9 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getUsersFromToken = exports.getUserFromToken = exports.validateAccessToken = exports.login = void 0;
4
4
  // helpers
5
5
  const helpers_1 = require("../helpers");
6
- // database
7
- const database_1 = require("../database");
8
- async function login(authHeader, tx, accessToken, userCtor, extraWhere = {}) {
6
+ async function login(authHeader, db, accessToken, userCtor, extraWhere = {}) {
9
7
  if (!authHeader?.startsWith('Basic '))
10
8
  helpers_1.AppError.ThrowInvalidCredentials();
11
9
  const base64 = authHeader.slice(6);
@@ -13,7 +11,6 @@ async function login(authHeader, tx, accessToken, userCtor, extraWhere = {}) {
13
11
  const [username, password] = decoded.split(':');
14
12
  if (!username || !password)
15
13
  helpers_1.AppError.ThrowInvalidCredentials();
16
- const db = new database_1.Db(tx);
17
14
  const where = { ...extraWhere, username };
18
15
  const user = await db.findOneOrNull(userCtor, where, { scopes: ['withExcludedAttributes'] });
19
16
  if (!user)
@@ -24,14 +21,14 @@ async function login(authHeader, tx, accessToken, userCtor, extraWhere = {}) {
24
21
  return accessToken.getTokenData(userCtor, user);
25
22
  }
26
23
  exports.login = login;
27
- async function validateAccessToken(authHeader, tx, accessToken, userCtor) {
28
- const user = await getUserFromToken(authHeader, tx, accessToken, userCtor);
24
+ async function validateAccessToken(authHeader, db, accessToken, userCtor) {
25
+ const user = await getUserFromToken(authHeader, db, accessToken, userCtor);
29
26
  if (!user)
30
27
  helpers_1.AppError.ThrowUnauthorized();
31
28
  return accessToken.getTokenData(userCtor, user);
32
29
  }
33
30
  exports.validateAccessToken = validateAccessToken;
34
- function getUserFromToken(authHeader, tx, accessToken, userCtor) {
31
+ function getUserFromToken(authHeader, db, accessToken, userCtor) {
35
32
  try {
36
33
  const token = getBearerToken(authHeader);
37
34
  const payload = accessToken.verifyToken(token);
@@ -39,7 +36,6 @@ function getUserFromToken(authHeader, tx, accessToken, userCtor) {
39
36
  const id = payload[key];
40
37
  if (!id)
41
38
  return null;
42
- const db = new database_1.Db(tx);
43
39
  return db.findOneOrNull(userCtor, id);
44
40
  }
45
41
  catch {
@@ -47,7 +43,7 @@ function getUserFromToken(authHeader, tx, accessToken, userCtor) {
47
43
  }
48
44
  }
49
45
  exports.getUserFromToken = getUserFromToken;
50
- async function getUsersFromToken(authHeader, tx, accessToken, authMap) {
46
+ async function getUsersFromToken(authHeader, db, accessToken, authMap) {
51
47
  try {
52
48
  const token = getBearerToken(authHeader);
53
49
  const payload = accessToken.verifyToken(token);
@@ -56,7 +52,6 @@ async function getUsersFromToken(authHeader, tx, accessToken, authMap) {
56
52
  const id = payload[key];
57
53
  if (!id)
58
54
  return [type, null];
59
- const db = new database_1.Db(tx);
60
55
  const user = await db.findOneOrNull(userCtor, id);
61
56
  return [type, user];
62
57
  }));
@@ -7,9 +7,8 @@ type SetArrayConfig<T extends BaseModel, M extends BaseModel> = {
7
7
  relationName: string;
8
8
  };
9
9
  export declare abstract class BaseService {
10
- protected tx: Tx;
11
10
  protected db: Db;
12
- constructor(tx: Tx);
11
+ constructor(db: Db);
13
12
  protected __setArray<T extends BaseModel, M extends BaseModel>(config: SetArrayConfig<T, M>): Promise<void>;
14
13
  }
15
14
  export {};
@@ -1,11 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseService = void 0;
4
- const db_class_1 = require("./db.class");
5
4
  class BaseService {
6
- constructor(tx) {
7
- this.tx = tx;
8
- this.db = new db_class_1.Db(tx);
5
+ constructor(db) {
6
+ this.db = db;
9
7
  }
10
8
  async __setArray(config) {
11
9
  const { instances, getManyFn, keyName, relationName } = config;
@@ -1,5 +1,6 @@
1
1
  import * as seq from 'sequelize-typescript';
2
2
  import { BaseModel } from './models';
3
+ import { Db as _Db } from './db.class';
3
4
  import type { OptionalFields, OptionalIfNullish } from 'pangea-helpers';
4
5
  import type { InferCreationAttributes, CreationOptional, NonAttribute, AbstractDataTypeConstructor, AbstractDataType, Transaction, WhereOptions } from 'sequelize';
5
6
  import type { ModelCtor } from 'sequelize-typescript';
@@ -12,10 +13,11 @@ declare global {
12
13
  __brand: 'excluded';
13
14
  };
14
15
  type ColVirtual<T> = NonAttribute<T>;
15
- type Tx = Transaction;
16
16
  type Where<BM extends BaseModel> = WhereOptions<BM> & Record<symbol, any>;
17
17
  type InsertParams<BM extends BaseModel> = OptionalIfNullish<OptionalIfOptional<StripRelations<OptionalFields<IncludeExcluded<Omit<InferCreationAttributes<BM>, '__brand'>>, Exclude<keyof InferCreationAttributes<BM>, '__brand'> & 'id'>>>>;
18
18
  type UpdateParams<BM extends BaseModel> = Partial<InsertParams<BM>>;
19
+ type Tx = Transaction;
20
+ type Db = _Db;
19
21
  }
20
22
  export type Models = Record<ModelName, BaseModelCtor>;
21
23
  export type Seeds<M extends Models> = {
@@ -1,4 +1,5 @@
1
- type RunFn = (tx: Tx) => Promise<void>;
1
+ import { Db } from '../database';
2
+ type RunFn = (db: Db) => Promise<void>;
2
3
  export declare abstract class Job {
3
4
  static Run(runFn: RunFn): () => Promise<void>;
4
5
  }
@@ -7,7 +7,8 @@ class Job {
7
7
  return async () => {
8
8
  const tx = await (0, database_1.getDbClient)().transaction();
9
9
  try {
10
- await runFn(tx);
10
+ const db = new database_1.Db(tx);
11
+ await runFn(db);
11
12
  await tx.commit();
12
13
  }
13
14
  catch (err) {
@@ -16,10 +16,11 @@ function callController(controller, validate, authConfig) {
16
16
  const ctx = { headers: req.headers, file: req.file, files: req.files, ...inputs };
17
17
  const tx = await (0, database_1.getDbClient)().transaction();
18
18
  try {
19
+ const db = new database_1.Db(tx);
19
20
  const { authMap, authCtor, accessToken } = authConfig;
20
- const authUsers = await (0, authentication_1.getUsersFromToken)(req.headers.authorization, tx, accessToken, authMap);
21
+ const authUsers = await (0, authentication_1.getUsersFromToken)(req.headers.authorization, db, accessToken, authMap);
21
22
  const auth = new authCtor(authUsers);
22
- const result = await controller(ctx, tx, auth);
23
+ const result = await controller(ctx, db, auth);
23
24
  await tx.commit();
24
25
  let data = result;
25
26
  let totalCount;
@@ -2,7 +2,7 @@ import { BaseAuth, AccessToken } from '../authentication';
2
2
  import type { AuthMap, AuthUsers, AuthCtor } from '../authentication/authentication.types';
3
3
  import type { RouteValidator } from '../validator/validator.types';
4
4
  export type Middleware = (req: Req, res: Res, next: Next) => void;
5
- export type Controller<V extends RouteValidator, AM extends AuthMap, BA extends BaseAuth<AM>> = (ctx: Ctx<V>, tx: Tx, auth: BA) => unknown;
5
+ export type Controller<V extends RouteValidator, AM extends AuthMap, BA extends BaseAuth<AM>> = (ctx: Ctx<V>, db: Db, auth: BA) => unknown;
6
6
  export type SetRouteArgs<V extends RouteValidator, AM extends AuthMap, BA extends BaseAuth<AM>> = [
7
7
  ...Middleware[],
8
8
  Controller<V, AM, BA>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pangea-server",
3
3
  "description": "",
4
- "version": "1.0.38",
4
+ "version": "1.0.39",
5
5
  "files": [
6
6
  "dist"
7
7
  ],