@data-fair/lib-common-types 0.1.0 → 0.2.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.
@@ -0,0 +1 @@
1
+ export * from './.type/index.js';
package/account/index.js CHANGED
@@ -1 +1 @@
1
- export * from './.type/index.js'
1
+ export * from './.type/index.js';
@@ -0,0 +1 @@
1
+ export * from './.type/index.js';
@@ -1 +1 @@
1
- export * from './.type/index.js'
1
+ export * from './.type/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-common-types",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Shared schemas and built type definitions in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "devDependencies": {},
@@ -0,0 +1,34 @@
1
+ import type { AxiosInstance } from 'axios';
2
+ import type { DataFairWsClient } from '@data-fair/lib-node/ws-client.js';
3
+ /**
4
+ * Processing context.
5
+ */
6
+ export interface ProcessingContext {
7
+ processingConfig: any;
8
+ pluginConfig: any;
9
+ processingId: string;
10
+ dir: string;
11
+ tmpDir: string;
12
+ log: LogFunctions;
13
+ axios: AxiosInstance;
14
+ ws: DataFairWsClient;
15
+ sendMail: (mail: string) => Promise<void>;
16
+ patchConfig: (patch: {
17
+ datasetMode: string;
18
+ dataset: any;
19
+ }) => Promise<void>;
20
+ }
21
+ /**
22
+ * Log functions.
23
+ */
24
+ export interface LogFunctions {
25
+ step: (msg: string) => Promise<void>;
26
+ error: (msg: string, extra?: any) => Promise<void>;
27
+ warning: (msg: string, extra?: any) => Promise<void>;
28
+ info: (msg: string, extra?: any) => Promise<void>;
29
+ debug: (msg: string, extra?: any) => Promise<void>;
30
+ task: (name: string) => Promise<void>;
31
+ progress: (taskName: string, progress: number, total: number) => Promise<void>;
32
+ testInfo?: (msg: any, extra?: any) => void;
33
+ testDebug?: (msg: any, extra?: any) => void;
34
+ }
package/processings.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import type { AccountKeys } from '../account/index.js';
2
+ import { type SessionState } from './.type/index.js';
3
+ export type SessionStateAuthenticated = SessionState & Required<Pick<SessionState, 'user' | 'account' | 'accountRole'>>;
4
+ export * from './.type/index.js';
5
+ export declare function isAuthenticated(sessionState: SessionState): sessionState is SessionStateAuthenticated;
6
+ export declare function assertAuthenticated(sessionState: SessionState): asserts sessionState is SessionStateAuthenticated;
7
+ export declare function assertAdminMode(sessionState: SessionState): asserts sessionState is SessionStateAuthenticated;
8
+ export declare function getAccountRole(sessionState: SessionState, account: AccountKeys, onlyActiveAccount?: boolean): string | null;
9
+ export declare function assertAccountRole(sessionState: SessionState, account: AccountKeys, role: string, onlyActiveAccount?: boolean): void;
10
+ export declare function isValidAccountType(type: string): type is 'user' | 'organization';
11
+ export declare function assertValidAccountType(type: string): asserts type is 'user' | 'organization';
package/session/index.js CHANGED
@@ -1,78 +1,55 @@
1
- /**
2
- * @typedef {import('./types.js').SessionStateAuthenticated} SessionStateAuthenticated
3
- * @typedef {import('./.type/index.js').SessionState} SessionState
4
- */
5
-
6
- import { httpError } from '@data-fair/lib/http-errors.js'
7
-
8
- export * from './.type/index.js'
9
-
10
- /** @type {(sessionState: SessionState) => sessionState is SessionStateAuthenticated} */
11
- export const isAuthenticated = (sessionState) => {
12
- return !!sessionState.user
13
- }
14
-
15
- /** @type {(sessionState: SessionState) => asserts sessionState is SessionStateAuthenticated} */
16
- export const assertAuthenticated = (sessionState) => {
17
- if (!isAuthenticated(sessionState)) throw httpError(401)
18
- }
19
-
20
- /** @type {(sessionState: SessionState) => asserts sessionState is SessionStateAuthenticated} */
21
- export const assertAdminMode = (sessionState) => {
22
- assertAuthenticated(sessionState)
23
- // TODO: use sessionState.locale to internationalize error message
24
- if (!sessionState.user.adminMode) throw httpError(403, 'super admin only')
25
- }
26
-
27
- /**
28
- * @param {import('../account/index.js').AccountKeys} userAccount
29
- * @param {import('../account/index.js').AccountKeys} resourceAccount
30
- * @returns {boolean}
31
- */
32
- const matchAccount = (userAccount, resourceAccount) => {
33
- if (userAccount.type !== resourceAccount.type) return false
34
- if (userAccount.id !== resourceAccount.id) return false
35
- if (userAccount.department && userAccount.department !== resourceAccount.department) return false
36
- return true
37
- }
38
-
39
- /**
40
- * @param {SessionState} sessionState
41
- * @param {import('../account/index.js').AccountKeys} account
42
- * @param {boolean} [onlyActiveAccount]
43
- * @returns {string | null}
44
- */
45
- export const getAccountRole = (sessionState, account, onlyActiveAccount = true) => {
46
- if (!isAuthenticated(sessionState)) return null
47
- if (sessionState.user.adminMode) return 'admin'
48
- if (onlyActiveAccount) {
49
- if (matchAccount(sessionState.account, account)) return sessionState.accountRole
50
- } else {
51
- if (account.type === 'user' && sessionState.user.id === account.id) return 'admin'
52
- for (const org of sessionState.user.organizations) {
53
- if (matchAccount({ type: 'organization', id: org.id, department: org.department }, account)) return org.role
1
+ import { httpError } from '@data-fair/lib-utils/http-errors.js';
2
+ export * from './.type/index.js';
3
+ export function isAuthenticated(sessionState) {
4
+ return !!sessionState.user;
5
+ }
6
+ export function assertAuthenticated(sessionState) {
7
+ if (!isAuthenticated(sessionState))
8
+ throw httpError(401);
9
+ }
10
+ export function assertAdminMode(sessionState) {
11
+ assertAuthenticated(sessionState);
12
+ // TODO: use sessionState.locale to internationalize error message
13
+ if (!sessionState.user.adminMode)
14
+ throw httpError(403, 'super admin only');
15
+ }
16
+ function matchAccount(userAccount, resourceAccount) {
17
+ if (userAccount.type !== resourceAccount.type)
18
+ return false;
19
+ if (userAccount.id !== resourceAccount.id)
20
+ return false;
21
+ if (userAccount.department && userAccount.department !== resourceAccount.department)
22
+ return false;
23
+ return true;
24
+ }
25
+ export function getAccountRole(sessionState, account, onlyActiveAccount = true) {
26
+ if (!isAuthenticated(sessionState))
27
+ return null;
28
+ if (sessionState.user.adminMode)
29
+ return 'admin';
30
+ if (onlyActiveAccount) {
31
+ if (matchAccount(sessionState.account, account))
32
+ return sessionState.accountRole;
33
+ }
34
+ else {
35
+ if (account.type === 'user' && sessionState.user.id === account.id)
36
+ return 'admin';
37
+ for (const org of sessionState.user.organizations) {
38
+ if (matchAccount({ type: 'organization', id: org.id, department: org.department }, account))
39
+ return org.role;
40
+ }
54
41
  }
55
- }
56
- return null
42
+ return null;
57
43
  }
58
-
59
- /**
60
- * @param {SessionState} sessionState
61
- * @param {import('../account/index.js').AccountKeys} account
62
- * @param {string} role
63
- * @param {boolean} [onlyActiveAccount]
64
- */
65
- export const assertAccountRole = (sessionState, account, role, onlyActiveAccount = true) => {
66
- const accountRole = getAccountRole(sessionState, account, onlyActiveAccount)
67
- if (accountRole !== role) throw httpError(403, `requires ${role} role`)
44
+ export function assertAccountRole(sessionState, account, role, onlyActiveAccount = true) {
45
+ const accountRole = getAccountRole(sessionState, account, onlyActiveAccount);
46
+ if (accountRole !== role)
47
+ throw httpError(403, `requires ${role} role`);
68
48
  }
69
-
70
- /** @type {(type: string) => type is "user" | "organization"} */
71
- export const isValidAccountType = (type) => {
72
- return ['user', 'organization'].includes(type)
49
+ export function isValidAccountType(type) {
50
+ return ['user', 'organization'].includes(type);
73
51
  }
74
-
75
- /** @type {(type: string) => asserts type is "user" | "organization"} */
76
- export const assertValidAccountType = (type) => {
77
- if (!isValidAccountType(type)) throw httpError(400, 'invalid account type')
52
+ export function assertValidAccountType(type) {
53
+ if (!isValidAccountType(type))
54
+ throw httpError(400, 'invalid account type');
78
55
  }
@@ -0,0 +1,2 @@
1
+ import { type SessionState } from './.type/index.js';
2
+ export type SessionStateAuthenticated = SessionState & Required<Pick<SessionState, 'user' | 'account' | 'accountRole'>>;
@@ -0,0 +1 @@
1
+ export {};
package/ws.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { type Account } from '@data-fair/lib-common-types/session/index.js';
2
+ type logFn = (msg: string, ...args: any[]) => void;
3
+ export interface WsClientOpts {
4
+ log?: {
5
+ info: logFn;
6
+ error: logFn;
7
+ debug: logFn;
8
+ };
9
+ url: string;
10
+ headers?: Record<string, string>;
11
+ apiKey?: string;
12
+ adminMode?: boolean;
13
+ account?: Account;
14
+ }
15
+ export type FullWsClientOpts = WsClientOpts & Required<Pick<WsClientOpts, 'log'>>;
16
+ export interface Message {
17
+ type: string;
18
+ channel: string;
19
+ data?: any;
20
+ status?: number;
21
+ }
22
+ export {};
package/ws.js ADDED
@@ -0,0 +1 @@
1
+ export {};