@data-fair/lib-common-types 0.1.0 → 1.0.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';
@@ -0,0 +1,45 @@
1
+ {
2
+ "$id": "https://github.com/data-fair/lib/account",
3
+ "x-exports": ["types", "validate", "schema"],
4
+ "type":"object",
5
+ "title": "account",
6
+ "required": ["type", "id", "name"],
7
+ "properties":{
8
+ "type":{
9
+ "type":"string",
10
+ "enum": ["user", "organization"]
11
+ },
12
+ "id":{
13
+ "type":"string"
14
+ },
15
+ "name":{
16
+ "type":"string"
17
+ },
18
+ "department":{
19
+ "type":"string"
20
+ },
21
+ "departmentName":{
22
+ "type":"string"
23
+ }
24
+ },
25
+ "$defs": {
26
+ "accountKeys": {
27
+ "type": "object",
28
+ "title": "account keys",
29
+ "required": ["type", "id"],
30
+ "additionalProperties": false,
31
+ "properties": {
32
+ "type":{
33
+ "type":"string",
34
+ "enum": ["user", "organization"]
35
+ },
36
+ "id": {
37
+ "type": "string"
38
+ },
39
+ "department": {
40
+ "type": "string"
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
@@ -0,0 +1 @@
1
+ export * from './.type/index.js';
@@ -0,0 +1,141 @@
1
+ {
2
+ "$id": "https://github.com/data-fair/lib/application",
3
+ "title": "Application",
4
+ "x-exports": [
5
+ "types",
6
+ "schema"
7
+ ],
8
+ "type": "object",
9
+ "required": [
10
+ "id",
11
+ "title",
12
+ "exposedUrl",
13
+ "href",
14
+ "wsUrl",
15
+ "owner",
16
+ "configuration"
17
+ ],
18
+ "properties": {
19
+ "id": {
20
+ "type": "string",
21
+ "description": "Globally unique identifier of the application"
22
+ },
23
+ "title": {
24
+ "type": "string",
25
+ "description": "Short title of the application"
26
+ },
27
+ "exposedUrl": {
28
+ "type": "string",
29
+ "description": "The URL where this application is exposed"
30
+ },
31
+ "href": {
32
+ "type": "string",
33
+ "description": "The URL where this resource can be fetched"
34
+ },
35
+ "wsUrl": {
36
+ "type": "string",
37
+ "description": "The URL where this application can be accessed through a websocket"
38
+ },
39
+ "owner": {
40
+ "$ref": "https://github.com/data-fair/lib/account#/$defs/accountKeys"
41
+ },
42
+ "configuration": {
43
+ "type": "object",
44
+ "description": "A free format configuration object used by applications. A minimal common structure is used to ensure proper linking between applications and datasets and remote services",
45
+ "additionalProperties": true,
46
+ "properties": {
47
+ "datasets": {
48
+ "type": "array",
49
+ "items": {
50
+ "$ref": "#/$defs/dataset"
51
+ }
52
+ }
53
+ }
54
+ }
55
+ },
56
+ "$defs": {
57
+ "dataset": {
58
+ "type": [
59
+ "object",
60
+ "null"
61
+ ],
62
+ "required": [
63
+ "href",
64
+ "id",
65
+ "title",
66
+ "finalizedAt"
67
+ ],
68
+ "properties": {
69
+ "href": {
70
+ "type": "string"
71
+ },
72
+ "id": {
73
+ "type": "string"
74
+ },
75
+ "title": {
76
+ "type": "string"
77
+ },
78
+ "key": {
79
+ "type": "string",
80
+ "description": "Not the id of the dataset, but a key inside this configuration object to define the role of the dataset in this context."
81
+ },
82
+ "schema": {
83
+ "type": "array",
84
+ "items": {
85
+ "$ref": "#/$defs/field"
86
+ }
87
+ },
88
+ "finalizedAt": {
89
+ "type": "string",
90
+ "format": "date-time"
91
+ }
92
+ }
93
+ },
94
+ "field": {
95
+ "type": "object",
96
+ "required": [
97
+ "key",
98
+ "type"
99
+ ],
100
+ "properties": {
101
+ "key": {
102
+ "type": "string"
103
+ },
104
+ "type": {
105
+ "type": "string"
106
+ },
107
+ "format": {
108
+ "type": "string"
109
+ },
110
+ "title": {
111
+ "type": "string"
112
+ },
113
+ "x-originalName": {
114
+ "type": "string"
115
+ },
116
+ "x-group": { "type": "string" },
117
+ "x-refersTo": {
118
+ "deprecated": true,
119
+ "type": ["string", "null"]
120
+ },
121
+ "x-concept": {
122
+ "type": "object",
123
+ "properties": {
124
+ "id": { "type": "string" },
125
+ "title": { "type": "string" },
126
+ "primary": { "type": "boolean" }
127
+ }
128
+ },
129
+ "x-labels": {
130
+ "type": "object",
131
+ "additionalProperties": {
132
+ "type": "string"
133
+ }
134
+ },
135
+ "x-labelsRestricted": {
136
+ "type": "boolean"
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@data-fair/lib-common-types",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "description": "Shared schemas and built type definitions in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "devDependencies": {},
7
7
  "files": [
8
+ "**/schema.json",
8
9
  "**/*.js",
9
10
  "**/*.d.ts"
10
11
  ],
@@ -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,42 @@
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
-
1
+ import { httpError } from '@data-fair/lib-utils/http-errors.js'
8
2
  export * from './.type/index.js'
9
-
10
- /** @type {(sessionState: SessionState) => sessionState is SessionStateAuthenticated} */
11
- export const isAuthenticated = (sessionState) => {
3
+ export function isAuthenticated (sessionState) {
12
4
  return !!sessionState.user
13
5
  }
14
-
15
- /** @type {(sessionState: SessionState) => asserts sessionState is SessionStateAuthenticated} */
16
- export const assertAuthenticated = (sessionState) => {
17
- if (!isAuthenticated(sessionState)) throw httpError(401)
6
+ export function assertAuthenticated (sessionState) {
7
+ if (!isAuthenticated(sessionState)) { throw httpError(401) }
18
8
  }
19
-
20
- /** @type {(sessionState: SessionState) => asserts sessionState is SessionStateAuthenticated} */
21
- export const assertAdminMode = (sessionState) => {
9
+ export function assertAdminMode (sessionState) {
22
10
  assertAuthenticated(sessionState)
23
11
  // TODO: use sessionState.locale to internationalize error message
24
- if (!sessionState.user.adminMode) throw httpError(403, 'super admin only')
12
+ if (!sessionState.user.adminMode) { throw httpError(403, 'super admin only') }
25
13
  }
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
14
+ function matchAccount (userAccount, resourceAccount) {
15
+ if (userAccount.type !== resourceAccount.type) { return false }
16
+ if (userAccount.id !== resourceAccount.id) { return false }
17
+ if (userAccount.department && userAccount.department !== resourceAccount.department) { return false }
36
18
  return true
37
19
  }
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'
20
+ export function getAccountRole (sessionState, account, onlyActiveAccount = true) {
21
+ if (!isAuthenticated(sessionState)) { return null }
22
+ if (sessionState.user.adminMode) { return 'admin' }
48
23
  if (onlyActiveAccount) {
49
- if (matchAccount(sessionState.account, account)) return sessionState.accountRole
24
+ if (matchAccount(sessionState.account, account)) { return sessionState.accountRole }
50
25
  } else {
51
- if (account.type === 'user' && sessionState.user.id === account.id) return 'admin'
26
+ if (account.type === 'user' && sessionState.user.id === account.id) { return 'admin' }
52
27
  for (const org of sessionState.user.organizations) {
53
- if (matchAccount({ type: 'organization', id: org.id, department: org.department }, account)) return org.role
28
+ if (matchAccount({ type: 'organization', id: org.id, department: org.department }, account)) { return org.role }
54
29
  }
55
30
  }
56
31
  return null
57
32
  }
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) => {
33
+ export function assertAccountRole (sessionState, account, role, onlyActiveAccount = true) {
66
34
  const accountRole = getAccountRole(sessionState, account, onlyActiveAccount)
67
- if (accountRole !== role) throw httpError(403, `requires ${role} role`)
35
+ if (accountRole !== role) { throw httpError(403, `requires ${role} role`) }
68
36
  }
69
-
70
- /** @type {(type: string) => type is "user" | "organization"} */
71
- export const isValidAccountType = (type) => {
37
+ export function isValidAccountType (type) {
72
38
  return ['user', 'organization'].includes(type)
73
39
  }
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')
40
+ export function assertValidAccountType (type) {
41
+ if (!isValidAccountType(type)) { throw httpError(400, 'invalid account type') }
78
42
  }
@@ -0,0 +1,157 @@
1
+ {
2
+ "$id": "https://github.com/data-fair/lib/session-state",
3
+ "x-exports": ["types", "validate", "schema"],
4
+ "type":"object",
5
+ "title": "session state",
6
+ "additionalProperties":false,
7
+ "properties":{
8
+ "user":{
9
+ "$ref":"#/$defs/user"
10
+ },
11
+ "organization":{
12
+ "$ref":"#/$defs/organizationMembership"
13
+ },
14
+ "account":{
15
+ "$ref":"#/$defs/account"
16
+ },
17
+ "accountRole":{
18
+ "type":"string"
19
+ },
20
+ "lang":{
21
+ "type":"string"
22
+ },
23
+ "dark":{
24
+ "type":"boolean"
25
+ }
26
+ },
27
+ "$defs":{
28
+ "organizationMembership":{
29
+ "type":"object",
30
+ "additionalProperties":false,
31
+ "required":[
32
+ "id",
33
+ "name",
34
+ "role"
35
+ ],
36
+ "properties":{
37
+ "id":{
38
+ "type":"string"
39
+ },
40
+ "name":{
41
+ "type":"string"
42
+ },
43
+ "role":{
44
+ "type":"string"
45
+ },
46
+ "department":{
47
+ "type":"string"
48
+ },
49
+ "departmentName":{
50
+ "type":"string"
51
+ },
52
+ "dflt":{
53
+ "type":"boolean"
54
+ }
55
+ }
56
+ },
57
+ "userRef":{
58
+ "type":"object",
59
+ "additionalProperties":false,
60
+ "required":[
61
+ "id",
62
+ "name"
63
+ ],
64
+ "properties":{
65
+ "id":{
66
+ "type":"string"
67
+ },
68
+ "name":{
69
+ "type":"string"
70
+ }
71
+ }
72
+ },
73
+ "user":{
74
+ "type":"object",
75
+ "additionalProperties":false,
76
+ "required":[
77
+ "email",
78
+ "id",
79
+ "name",
80
+ "organizations"
81
+ ],
82
+ "properties":{
83
+ "email":{
84
+ "type":"string",
85
+ "format":"email"
86
+ },
87
+ "id":{
88
+ "type":"string"
89
+ },
90
+ "name":{
91
+ "type":"string"
92
+ },
93
+ "organizations":{
94
+ "type":"array",
95
+ "items":{
96
+ "$ref":"#/$defs/organizationMembership"
97
+ }
98
+ },
99
+ "isAdmin":{
100
+ "type":"integer",
101
+ "enum":[
102
+ 0,
103
+ 1
104
+ ]
105
+ },
106
+ "adminMode":{
107
+ "type":"integer",
108
+ "enum":[
109
+ 0,
110
+ 1
111
+ ]
112
+ },
113
+ "asAdmin":{
114
+ "$ref":"#/$defs/userRef"
115
+ },
116
+ "pd":{
117
+ "type":"string",
118
+ "format":"date"
119
+ },
120
+ "ipa":{
121
+ "type":"integer",
122
+ "enum":[
123
+ 0,
124
+ 1
125
+ ]
126
+ }
127
+ }
128
+ },
129
+ "account":{
130
+ "type":"object",
131
+ "additionalProperties":false,
132
+ "required":[
133
+ "type",
134
+ "id",
135
+ "name"
136
+ ],
137
+ "properties":{
138
+ "type":{
139
+ "type":"string",
140
+ "enum": ["user", "organization"]
141
+ },
142
+ "id":{
143
+ "type":"string"
144
+ },
145
+ "name":{
146
+ "type":"string"
147
+ },
148
+ "department":{
149
+ "type":"string"
150
+ },
151
+ "departmentName":{
152
+ "type":"string"
153
+ }
154
+ }
155
+ }
156
+ }
157
+ }
@@ -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,2 @@
1
+ export {}
2
+ // # sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJ0eXBlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
package/ws.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export interface Message {
2
+ type: string;
3
+ channel: string;
4
+ data?: any;
5
+ status?: number;
6
+ }
package/ws.js ADDED
@@ -0,0 +1 @@
1
+ export {}