@shakerquiz/utilities 0.2.17 → 0.2.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shakerquiz/utilities",
3
- "version": "0.2.17",
3
+ "version": "0.2.19",
4
4
  "author": "yurkimus <yurkimus@gmail.com>",
5
5
  "license": "ISC",
6
6
  "exports": {
@@ -1,6 +1,6 @@
1
- export let Constants = {
1
+ export var Constants = /** @type {const} */ ({
2
2
  /**
3
3
  * @see https://www.rfc-editor.org/rfc/rfc9562.html#name-nil-uuid
4
4
  */
5
5
  NilUuid: '00000000-0000-0000-0000-000000000000',
6
- }
6
+ })
@@ -1,4 +1,4 @@
1
- export let Cookies = {
1
+ export var Cookies = {
2
2
  Token: 'shaker-quiz-token',
3
3
  Refresh: 'shaker-quiz-refresh-token',
4
4
  Role: 'shaker-quiz-user-role',
@@ -1,70 +1,147 @@
1
1
  import { Kinds } from './kinds.js'
2
- import { Services } from './services.js'
2
+ import { Networks } from './networks.js'
3
3
 
4
- export let Features = /** @type {const} */ ({
4
+ export var Features = /** @type {const} */ ({
5
5
  Checkin: 'Checkin',
6
-
7
- User: 'User',
8
- Users: 'Users',
9
-
10
- Theme: 'Theme',
11
- Themes: 'Themes',
12
-
6
+ Cities: 'Cities',
7
+ City: 'City',
13
8
  Game: 'Game',
14
9
  Games: 'Games',
15
-
16
- Registration: 'Registration',
17
- Registrations: 'Registrations',
18
-
19
- City: 'City',
20
- Cities: 'Cities',
21
-
22
10
  Location: 'Location',
23
11
  Locations: 'Locations',
12
+ Registration: 'Registration',
13
+ Registrations: 'Registrations',
14
+ Theme: 'Theme',
15
+ Themes: 'Themes',
16
+ User: 'User',
17
+ Users: 'Users',
24
18
  })
25
19
 
26
- export let FeatureKinds = {
20
+ export var FeatureKinds = {
27
21
  [Features.Checkin]: Kinds.Item,
28
-
29
- [Features.User]: Kinds.Item,
30
- [Features.Users]: Kinds.List,
31
-
32
- [Features.Theme]: Kinds.Item,
33
- [Features.Themes]: Kinds.List,
34
-
22
+ [Features.Cities]: Kinds.List,
23
+ [Features.City]: Kinds.Item,
35
24
  [Features.Game]: Kinds.Item,
36
25
  [Features.Games]: Kinds.List,
37
-
38
- [Features.Registration]: Kinds.Item,
39
- [Features.Registrations]: Kinds.List,
40
-
41
- [Features.City]: Kinds.Item,
42
- [Features.Cities]: Kinds.List,
43
-
44
26
  [Features.Location]: Kinds.Item,
45
27
  [Features.Locations]: Kinds.List,
28
+ [Features.Registration]: Kinds.Item,
29
+ [Features.Registrations]: Kinds.List,
30
+ [Features.Theme]: Kinds.Item,
31
+ [Features.Themes]: Kinds.List,
32
+ [Features.User]: Kinds.Item,
33
+ [Features.Users]: Kinds.List,
46
34
  }
47
35
 
48
- export let FeatureServices = {
49
- [Features.Checkin]: Services.Users,
50
-
51
- [Features.User]: Services.Users,
52
- [Features.Users]: Services.Users,
53
-
54
- [Features.Theme]: Services.Games,
55
- [Features.Themes]: Services.Games,
56
-
57
- [Features.Game]: Services.Games,
58
- [Features.Games]: Services.Games,
59
-
60
- [Features.Registration]: Services.Games,
61
- [Features.Registrations]: Services.Games,
62
-
63
- [Features.City]: Services.Locations,
64
- [Features.Cities]: Services.Locations,
36
+ export var FeaturePathnames = /** @type {const} */ ({
37
+ [Features.Checkin]: '/checkin',
38
+ [Features.Cities]: '/cities',
39
+ [Features.City]: '/city/:city?',
40
+ [Features.Game]: '/game/:game?',
41
+ [Features.Games]: '/games',
42
+ [Features.Location]: '/location/:location?',
43
+ [Features.Locations]: '/locations',
44
+ [Features.Registration]: '/registration/:registration?',
45
+ [Features.Registrations]: '/registrations',
46
+ [Features.Theme]: '/theme/:theme?',
47
+ [Features.Themes]: '/themes',
48
+ [Features.User]: '/user/:user?',
49
+ [Features.Users]: '/users',
50
+ })
65
51
 
66
- [Features.Location]: Services.Locations,
67
- [Features.Locations]: Services.Locations,
52
+ export var FeaturePatterns = {
53
+ [Features.Checkin]: new URLPattern({
54
+ pathname: FeaturePathnames[Features.Checkin],
55
+ }),
56
+ [Features.Cities]: new URLPattern({
57
+ pathname: FeaturePathnames[Features.Cities],
58
+ }),
59
+ [Features.City]: new URLPattern({
60
+ pathname: FeaturePathnames[Features.City],
61
+ }),
62
+ [Features.Game]: new URLPattern({
63
+ pathname: FeaturePathnames[Features.Game],
64
+ }),
65
+ [Features.Games]: new URLPattern({
66
+ pathname: FeaturePathnames[Features.Games],
67
+ }),
68
+ [Features.Location]: new URLPattern({
69
+ pathname: FeaturePathnames[Features.Location],
70
+ }),
71
+ [Features.Locations]: new URLPattern({
72
+ pathname: FeaturePathnames[Features.Locations],
73
+ }),
74
+ [Features.Registration]: new URLPattern({
75
+ pathname: FeaturePathnames[Features.Registration],
76
+ }),
77
+ [Features.Registrations]: new URLPattern({
78
+ pathname: FeaturePathnames[Features.Registrations],
79
+ }),
80
+ [Features.Theme]: new URLPattern({
81
+ pathname: FeaturePathnames[Features.Theme],
82
+ }),
83
+ [Features.Themes]: new URLPattern({
84
+ pathname: FeaturePathnames[Features.Themes],
85
+ }),
86
+ [Features.User]: new URLPattern({
87
+ pathname: FeaturePathnames[Features.User],
88
+ }),
89
+ [Features.Users]: new URLPattern({
90
+ pathname: FeaturePathnames[Features.Users],
91
+ }),
68
92
  }
69
93
 
70
- export let FeatureOrigins = new Map()
94
+ export var FeatureNetworkOrigins = /** @type {const} */ ({
95
+ [Features.Checkin]: {
96
+ [Networks.Private]: 'http://users/',
97
+ [Networks.Public]: 'https://users.backend.shakerquiz.ru/',
98
+ },
99
+ [Features.Cities]: {
100
+ [Networks.Private]: 'http://locations/',
101
+ [Networks.Public]: 'https://locations.backend.shakerquiz.ru/',
102
+ },
103
+ [Features.City]: {
104
+ [Networks.Private]: 'http://locations/',
105
+ [Networks.Public]: 'https://locations.backend.shakerquiz.ru/',
106
+ },
107
+ [Features.Game]: {
108
+ [Networks.Private]: 'http://games/',
109
+ [Networks.Public]: 'https://games.backend.shakerquiz.ru/',
110
+ },
111
+ [Features.Games]: {
112
+ [Networks.Private]: 'http://games/',
113
+ [Networks.Public]: 'https://games.backend.shakerquiz.ru/',
114
+ },
115
+ [Features.Location]: {
116
+ [Networks.Private]: 'http://locations/',
117
+ [Networks.Public]: 'https://locations.backend.shakerquiz.ru/',
118
+ },
119
+ [Features.Locations]: {
120
+ [Networks.Private]: 'http://locations/',
121
+ [Networks.Public]: 'https://locations.backend.shakerquiz.ru/',
122
+ },
123
+ [Features.Registration]: {
124
+ [Networks.Private]: 'http://games/',
125
+ [Networks.Public]: 'https://games.backend.shakerquiz.ru/',
126
+ },
127
+ [Features.Registrations]: {
128
+ [Networks.Private]: 'http://games/',
129
+ [Networks.Public]: 'https://games.backend.shakerquiz.ru/',
130
+ },
131
+ [Features.Theme]: {
132
+ [Networks.Private]: 'http://games/',
133
+ [Networks.Public]: 'https://games.backend.shakerquiz.ru/',
134
+ },
135
+ [Features.Themes]: {
136
+ [Networks.Private]: 'http://games/',
137
+ [Networks.Public]: 'https://games.backend.shakerquiz.ru/',
138
+ },
139
+ [Features.User]: {
140
+ [Networks.Private]: 'http://users/',
141
+ [Networks.Public]: 'https://users.backend.shakerquiz.ru/',
142
+ },
143
+ [Features.Users]: {
144
+ [Networks.Private]: 'http://users/',
145
+ [Networks.Public]: 'https://users.backend.shakerquiz.ru/',
146
+ },
147
+ })
@@ -1,39 +1,39 @@
1
1
  import { Roles } from './roles.js'
2
2
 
3
- export let GameStatuses = {
4
- Approved: 'APPROVED',
5
- Archive: 'ARCHIVE',
6
- Closed: 'CLOSED',
7
- Finished: 'FINISHED',
8
- Invitation: 'FORINVITES',
9
- Moderation: 'MODERATION',
10
- Published: 'PUBLISHED',
11
- Rejected: 'REJECTED',
12
- Reserved: 'IS_RESERVE',
13
- }
3
+ export var GameStatuses = /** @type {const} */ ({
4
+ APPROVED: 'APPROVED',
5
+ ARCHIVE: 'ARCHIVE',
6
+ CLOSED: 'CLOSED',
7
+ FINISHED: 'FINISHED',
8
+ FORINVITES: 'FORINVITES',
9
+ MODERATION: 'MODERATION',
10
+ PUBLISHED: 'PUBLISHED',
11
+ REJECTED: 'REJECTED',
12
+ IS_RESERVE: 'IS_RESERVE',
13
+ })
14
14
 
15
- export let RoleGameStatuses = {
16
- [Roles.Admin]: [
17
- GameStatuses.Approved,
18
- GameStatuses.Moderation,
19
- GameStatuses.Published,
20
- GameStatuses.Rejected,
21
- GameStatuses.Finished,
22
- GameStatuses.Archive,
23
- GameStatuses.Invitation,
24
- GameStatuses.Reserved,
25
- GameStatuses.Closed,
15
+ export var RoleGameStatuses = {
16
+ [Roles.admin]: [
17
+ GameStatuses.APPROVED,
18
+ GameStatuses.MODERATION,
19
+ GameStatuses.PUBLISHED,
20
+ GameStatuses.REJECTED,
21
+ GameStatuses.FINISHED,
22
+ GameStatuses.ARCHIVE,
23
+ GameStatuses.FORINVITES,
24
+ GameStatuses.IS_RESERVE,
25
+ GameStatuses.CLOSED,
26
26
  ],
27
27
 
28
- [Roles.Organizer]: [
29
- GameStatuses.Approved,
30
- GameStatuses.Published,
31
- GameStatuses.Finished,
32
- GameStatuses.Archive,
33
- GameStatuses.Invitation,
34
- GameStatuses.Reserved,
35
- GameStatuses.Closed,
28
+ [Roles.organizer]: [
29
+ GameStatuses.APPROVED,
30
+ GameStatuses.PUBLISHED,
31
+ GameStatuses.FINISHED,
32
+ GameStatuses.ARCHIVE,
33
+ GameStatuses.FORINVITES,
34
+ GameStatuses.IS_RESERVE,
35
+ GameStatuses.CLOSED,
36
36
  ],
37
37
 
38
- [Roles.Default]: [],
38
+ [Roles.default]: [],
39
39
  }
@@ -1,4 +1,4 @@
1
- export let Kinds = /** @type {const} */ ({
1
+ export var Kinds = /** @type {const} */ ({
2
2
  Item: 'Item',
3
3
  List: 'List',
4
4
  })
@@ -1,7 +1,8 @@
1
- export let Methods = /** @type {const} */ ({
1
+ export var Methods = /** @type {const} */ ({
2
2
  DELETE: 'DELETE',
3
3
  GET: 'GET',
4
4
  OPTIONS: 'OPTIONS',
5
5
  PATCH: 'PATCH',
6
6
  POST: 'POST',
7
+ PUT: 'PUT',
7
8
  })
@@ -1,4 +1,4 @@
1
- export let Networks = {
1
+ export var Networks = /** @type {const} */ ({
2
2
  Private: 'Private',
3
3
  Public: 'Public',
4
- }
4
+ })
@@ -1,8 +1,6 @@
1
1
  export let RegExps = {
2
2
  Uuid: /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/,
3
-
4
3
  ElementId:
5
4
  /\d+:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}:\d+/,
6
-
7
5
  Jwt: /^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/,
8
6
  }
@@ -1,5 +1,5 @@
1
- export let Roles = {
2
- Admin: 'admin',
3
- Organizer: 'organizer',
4
- Default: 'default',
5
- }
1
+ export var Roles = /** @type {const} */ ({
2
+ admin: 'admin',
3
+ organizer: 'organizer',
4
+ default: 'default',
5
+ })
@@ -2,15 +2,19 @@ import * as cookies from '@yurkimus/cookies'
2
2
  import { MessageError } from '@yurkimus/errors'
3
3
  import * as message from '@yurkimus/message'
4
4
  import { ResponseStatus } from '@yurkimus/response-status'
5
+ import { url } from '@yurkimus/url'
5
6
 
6
7
  import { Cookies } from '../enumerations/cookies.js'
7
8
  import {
8
9
  FeatureKinds,
9
- FeatureOrigins,
10
- FeatureServices,
10
+ FeatureNetworkOrigins,
11
+ FeaturePathnames,
12
+ Features,
11
13
  } from '../enumerations/features.js'
12
14
  import { Kinds } from '../enumerations/kinds.js'
13
- import { FeaturePatterns } from '../enumerations/patterns.js'
15
+ import { Methods } from '../enumerations/methods.js'
16
+ import { Networks } from '../enumerations/networks.js'
17
+ import { Roles } from '../enumerations/roles.js'
14
18
 
15
19
  let handleMessage = (feature, [response, body]) => {
16
20
  switch (response.status) {
@@ -27,28 +31,18 @@ let handleMessage = (feature, [response, body]) => {
27
31
  return []
28
32
  }
29
33
 
30
- case 400:
31
- case 401:
32
- case 403:
33
- case 404:
34
- case 409:
35
- case 415:
36
- case 500:
37
- case 502:
34
+ default:
38
35
  throw MessageError(
39
- body.message,
36
+ body?.message ?? '',
40
37
  ResponseStatus(response.status),
41
38
  )
42
-
43
- default:
44
- throw body
45
39
  }
46
40
  }
47
41
 
48
42
  /**
49
- * @template {FeaturesUnion} Feature
50
- * @template {MethodsUnion} Method
51
- * @template {NetworksUnion} Network
43
+ * @template {keyof typeof Features} Feature
44
+ * @template {keyof typeof Methods} Method
45
+ * @template {keyof typeof Networks} Network
52
46
  *
53
47
  * @param {Feature} feature
54
48
  * @param {Method} method
@@ -56,36 +50,37 @@ let handleMessage = (feature, [response, body]) => {
56
50
  * @param {import('@yurkimus/url').URLOptions | undefined} options
57
51
  * @param {RequestInit} init
58
52
  */
59
- let makeRequest = (feature, method, network, options, init) => {
53
+ let request = (feature, method, network, options, init) => {
60
54
  if (!('method' in init))
61
55
  init.method = method
62
56
 
63
- let url = FeatureOrigins
64
- .get(FeatureServices[feature])
65
- .get(network)
66
- .call(
67
- undefined,
68
- FeaturePatterns[feature].pathname,
57
+ let instance = new Request(
58
+ url(
59
+ FeatureNetworkOrigins[feature][network],
60
+ FeaturePathnames[feature],
69
61
  options,
70
- )
71
-
72
- let request = new Request(url, init)
62
+ ),
63
+ init,
64
+ )
73
65
 
74
- if (!request.headers.has('Content-Type'))
75
- request.headers.set(
66
+ if (!instance.headers.has('Content-Type'))
67
+ instance.headers.set(
76
68
  'Content-Type',
77
69
  'application/json',
78
70
  )
79
71
 
80
72
  if ('cookie' in init)
81
- request.headers.set(
73
+ instance.headers.set(
82
74
  'Authorization',
83
75
  cookies.read(Cookies.Token, init.cookie),
84
76
  )
85
77
 
86
- return fetch(request)
78
+ return fetch(instance)
87
79
  .then(message.read)
88
- .then(handleMessage.bind(undefined, feature))
80
+ .then(handleMessage.bind(
81
+ undefined,
82
+ feature,
83
+ ))
89
84
  }
90
85
 
91
86
  /**
@@ -94,45 +89,54 @@ let makeRequest = (feature, method, network, options, init) => {
94
89
  export let Extensions = new WeakMap()
95
90
 
96
91
  /**
97
- * @template {FeaturesUnion} Feature
98
- * @template {MethodsUnion} Method
99
- * @template {NetworksUnion} Network
92
+ * @template {keyof typeof Features} Feature
93
+ * @template {keyof typeof Methods} Method
94
+ * @template {keyof typeof Networks} Network
100
95
  *
101
96
  * @param {Feature} feature
102
97
  * @param {Method} method
103
98
  * @param {Network} network
104
99
  */
105
- export let useRequest = (feature, method, network) => {
106
- if (!(feature in FeatureServices))
100
+ export let useFetch = (feature, method, network) => {
101
+ if (!(feature in Features))
102
+ throw TypeError(
103
+ `Feature '${feature}' must be listed in 'Features'.`,
104
+ )
105
+
106
+ if (!(method in Methods))
107
107
  throw TypeError(
108
- `Feature '${feature}' must be listed in 'FeatureServices'.`,
108
+ `Method '${method}' must be listed in 'Methods'.`,
109
109
  )
110
110
 
111
- if (!(feature in FeaturePatterns))
111
+ if (!(feature in FeaturePathnames))
112
112
  throw TypeError(
113
- `Feature '${feature}' must be listed in 'FeaturePatterns'.`,
113
+ `Feature '${feature}' must be listed in 'FeaturePathnames'.`,
114
114
  )
115
115
 
116
- if (!FeatureOrigins.has(FeatureServices[feature]))
116
+ if (!(feature in FeatureNetworkOrigins))
117
117
  throw TypeError(
118
- `Service '${
119
- FeatureServices[feature]
120
- }' must be listed in 'FeatureOrigins'.`,
118
+ `Feature '${feature}' must be listed in 'FeatureNetworkOrigins'.`,
121
119
  )
122
120
 
123
- if (!FeatureOrigins.get(FeatureServices[feature]).has(network))
121
+ if (!(network in FeatureNetworkOrigins[feature]))
124
122
  throw TypeError(
125
- `Network '${network}' must be listed in '${FeatureServices[feature]}'.`,
123
+ `Network '${
124
+ FeatureNetworkOrigins[feature]
125
+ }' must be listed in 'FeatureNetworkOrigins[feature]'.`,
126
126
  )
127
127
 
128
128
  /**
129
+ * @template {keyof typeof Roles} Role
130
+ *
129
131
  * @param {import('@yurkimus/url').URLOptions} options
130
132
  * @param {RequestInit} init
133
+ *
134
+ * @returns {Promise<FetchResults[Feature][Method][Role]>}
131
135
  */
132
- let request = (options, init) => {
136
+ let fetch = (options, init) => {
133
137
  let onbefore = parameters => {
134
138
  let predicates = Extensions
135
- .get(request)
139
+ .get(fetch)
136
140
  .get('onbefore')
137
141
 
138
142
  return (predicates.size > 0)
@@ -147,7 +151,7 @@ export let useRequest = (feature, method, network) => {
147
151
 
148
152
  let onfulfilled = contract => {
149
153
  let predicates = Extensions
150
- .get(request)
154
+ .get(fetch)
151
155
  .get('onfulfilled')
152
156
 
153
157
  return (predicates.size > 0)
@@ -162,7 +166,7 @@ export let useRequest = (feature, method, network) => {
162
166
 
163
167
  let onrejected = reason => {
164
168
  let predicates = Extensions
165
- .get(request)
169
+ .get(fetch)
166
170
  .get('onrejected')
167
171
 
168
172
  Array
@@ -180,7 +184,7 @@ export let useRequest = (feature, method, network) => {
180
184
  }
181
185
  })
182
186
  .then(([options, init]) =>
183
- makeRequest(
187
+ request(
184
188
  feature,
185
189
  method,
186
190
  network,
@@ -193,7 +197,7 @@ export let useRequest = (feature, method, network) => {
193
197
  }
194
198
 
195
199
  Extensions.set(
196
- request,
200
+ fetch,
197
201
  new Map([
198
202
  ['onbefore', new Set([])],
199
203
  ['onfulfilled', new Set([])],
@@ -201,5 +205,5 @@ export let useRequest = (feature, method, network) => {
201
205
  ]),
202
206
  )
203
207
 
204
- return request
208
+ return fetch
205
209
  }