@shakerquiz/utilities 0.4.10 → 0.4.13

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.4.10",
3
+ "version": "0.4.13",
4
4
  "author": "yurkimus <yurkimus@gmail.com>",
5
5
  "license": "ISC",
6
6
  "scripts": {
@@ -108,3 +108,4 @@ export const FeatureKindPathnames: Array<[feature: Feature, kind: Kind, pathname
108
108
  * @type {Array<[feature: Feature, kind: Kind, pattern: URLPattern]>}
109
109
  */
110
110
  export const FeatureKindPatterns: Array<[feature: Feature, kind: Kind, pattern: URLPattern]>;
111
+ export function resolvePattern(feature: Feature, kind: Kind): URLPattern;
@@ -1,3 +1,5 @@
1
+ import { Kinds } from './kinds.js'
2
+
1
3
  export var Domains = /** @type {const} */ ([
2
4
  'Asset',
3
5
  'User',
@@ -140,3 +142,25 @@ export var FeatureKindPatterns = FeatureKindPathnames
140
142
  kind,
141
143
  new URLPattern({ pathname }),
142
144
  ])
145
+
146
+ /**
147
+ * @param {Feature} feature
148
+ * @param {Kind} kind
149
+ *
150
+ * @returns {URLPattern}
151
+ */
152
+ export let resolvePattern = (feature, kind) => {
153
+ if (!Features.includes(feature))
154
+ throw TypeError(
155
+ `Feature '${feature}' must be listed in 'Features'.`,
156
+ )
157
+
158
+ if (!Kinds.includes(kind))
159
+ throw TypeError(
160
+ `Kind '${kind}' must be listed in 'Kinds'.`,
161
+ )
162
+
163
+ return FeatureKindPatterns
164
+ .find(([f, k]) => f == feature && k == kind)
165
+ ?.at(2)
166
+ }
@@ -2,3 +2,4 @@
2
2
  * @type {Scope[]}
3
3
  */
4
4
  export const Scopes: Scope[];
5
+ export function resolveScope(request: Request): Scope | "";
@@ -1,4 +1,4 @@
1
- import { Features } from './features.js'
1
+ import { FeatureKindPatterns, Features } from './features.js'
2
2
  import { Kinds } from './kinds.js'
3
3
  import { Methods } from './methods.js'
4
4
 
@@ -10,3 +10,18 @@ export var Scopes = Object
10
10
  .flatMap(method => Object.values(Features).map(feature => [method, feature]))
11
11
  .flatMap(array => Object.values(Kinds).map(kind => array.concat(kind)))
12
12
  .map(array => array.join('/'))
13
+
14
+ /**
15
+ * @param {Request} request
16
+ *
17
+ * @returns {Scope | ''}
18
+ */
19
+ export var resolveScope = request => {
20
+ var found = FeatureKindPatterns
21
+ .find(([, , pattern]) => pattern.test(request.url))
22
+ ?.slice(0, -1)
23
+
24
+ return found
25
+ ? request.method + '/' + found[0] + '/' + found[1]
26
+ : ''
27
+ }