@data-fair/lib-common-types 0.2.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.
package/account/index.js CHANGED
@@ -1 +1 @@
1
- export * from './.type/index.js';
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
+ }
@@ -1 +1 @@
1
- export * from './.type/index.js';
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.2.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
  ],
package/processings.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export {}
package/session/index.js CHANGED
@@ -1,55 +1,42 @@
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;
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)) { throw httpError(401) }
8
+ }
9
+ export function assertAdminMode (sessionState) {
10
+ assertAuthenticated(sessionState)
11
+ // TODO: use sessionState.locale to internationalize error message
12
+ if (!sessionState.user.adminMode) { throw httpError(403, 'super admin only') }
13
+ }
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 }
18
+ return true
19
+ }
20
+ export function getAccountRole (sessionState, account, onlyActiveAccount = true) {
21
+ if (!isAuthenticated(sessionState)) { return null }
22
+ if (sessionState.user.adminMode) { return 'admin' }
23
+ if (onlyActiveAccount) {
24
+ if (matchAccount(sessionState.account, account)) { return sessionState.accountRole }
25
+ } else {
26
+ if (account.type === 'user' && sessionState.user.id === account.id) { return 'admin' }
27
+ for (const org of sessionState.user.organizations) {
28
+ if (matchAccount({ type: 'organization', id: org.id, department: org.department }, account)) { return org.role }
33
29
  }
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
- }
41
- }
42
- return null;
30
+ }
31
+ return null
43
32
  }
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`);
33
+ export function assertAccountRole (sessionState, account, role, onlyActiveAccount = true) {
34
+ const accountRole = getAccountRole(sessionState, account, onlyActiveAccount)
35
+ if (accountRole !== role) { throw httpError(403, `requires ${role} role`) }
48
36
  }
49
- export function isValidAccountType(type) {
50
- return ['user', 'organization'].includes(type);
37
+ export function isValidAccountType (type) {
38
+ return ['user', 'organization'].includes(type)
51
39
  }
52
- export function assertValidAccountType(type) {
53
- if (!isValidAccountType(type))
54
- throw httpError(400, 'invalid account type');
40
+ export function assertValidAccountType (type) {
41
+ if (!isValidAccountType(type)) { throw httpError(400, 'invalid account type') }
55
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
+ }
@@ -1,2 +1,2 @@
1
- import { type SessionState } from './.type/index.js';
2
- export type SessionStateAuthenticated = SessionState & Required<Pick<SessionState, 'user' | 'account' | 'accountRole'>>;
1
+ import { type SessionState } from './.type/index.js'
2
+ export type SessionStateAuthenticated = SessionState & Required<Pick<SessionState, 'user' | 'account' | 'accountRole'>>
package/session/types.js CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ export {}
2
+ // # sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJ0eXBlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
package/ws.d.ts CHANGED
@@ -1,22 +1,6 @@
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
1
  export interface Message {
17
2
  type: string;
18
3
  channel: string;
19
4
  data?: any;
20
5
  status?: number;
21
6
  }
22
- export {};
package/ws.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export {}