@occultist/occultist 0.0.1
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/LICENSE +21 -0
- package/README.md +144 -0
- package/dist/accept.d.ts +41 -0
- package/dist/accept.js +110 -0
- package/dist/accept.test.d.ts +1 -0
- package/dist/accept.test.js +44 -0
- package/dist/action.test.d.ts +1 -0
- package/dist/action.test.js +1 -0
- package/dist/actions/actionSets.d.ts +23 -0
- package/dist/actions/actionSets.js +49 -0
- package/dist/actions/actions.d.ts +163 -0
- package/dist/actions/actions.js +436 -0
- package/dist/actions/context.d.ts +78 -0
- package/dist/actions/context.js +112 -0
- package/dist/actions/meta.d.ts +49 -0
- package/dist/actions/meta.js +177 -0
- package/dist/actions/path.d.ts +21 -0
- package/dist/actions/path.js +83 -0
- package/dist/actions/path.test.d.ts +1 -0
- package/dist/actions/path.test.js +9 -0
- package/dist/actions/spec.d.ts +214 -0
- package/dist/actions/spec.js +1 -0
- package/dist/actions/types.d.ts +112 -0
- package/dist/actions/types.js +2 -0
- package/dist/actions/writer.d.ts +27 -0
- package/dist/actions/writer.js +140 -0
- package/dist/actions/writer.test.d.ts +1 -0
- package/dist/actions/writer.test.js +42 -0
- package/dist/auth/types.d.ts +14 -0
- package/dist/auth/types.js +1 -0
- package/dist/cache/cache.d.ts +30 -0
- package/dist/cache/cache.js +220 -0
- package/dist/cache/etag.d.ts +17 -0
- package/dist/cache/etag.js +83 -0
- package/dist/cache/etag.test.d.ts +1 -0
- package/dist/cache/etag.test.js +91 -0
- package/dist/cache/memory.d.ts +12 -0
- package/dist/cache/memory.js +36 -0
- package/dist/cache/types.d.ts +175 -0
- package/dist/cache/types.js +4 -0
- package/dist/errors.d.ts +11 -0
- package/dist/errors.js +54 -0
- package/dist/jsonld.d.ts +43 -0
- package/dist/jsonld.js +1 -0
- package/dist/makeTypeDefs.d.ts +27 -0
- package/dist/makeTypeDefs.js +70 -0
- package/dist/merge.d.ts +61 -0
- package/dist/merge.js +1 -0
- package/dist/mod.d.ts +14 -0
- package/dist/mod.js +14 -0
- package/dist/processAction.d.ts +15 -0
- package/dist/processAction.js +512 -0
- package/dist/registry.d.ts +88 -0
- package/dist/registry.js +314 -0
- package/dist/registry.test.d.ts +1 -0
- package/dist/registry.test.js +133 -0
- package/dist/request.d.ts +29 -0
- package/dist/request.js +118 -0
- package/dist/scopes.d.ts +35 -0
- package/dist/scopes.js +121 -0
- package/dist/scopes.test.d.ts +1 -0
- package/dist/scopes.test.js +55 -0
- package/dist/transformers/fileTransformer.d.ts +1 -0
- package/dist/transformers/fileTransformer.js +8 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.js +1 -0
- package/dist/utils/alwaysArray.d.ts +1 -0
- package/dist/utils/alwaysArray.js +9 -0
- package/dist/utils/contextBuilder.d.ts +9 -0
- package/dist/utils/contextBuilder.js +82 -0
- package/dist/utils/getActionContext.d.ts +7 -0
- package/dist/utils/getActionContext.js +48 -0
- package/dist/utils/getInternalName.d.ts +6 -0
- package/dist/utils/getInternalName.js +7 -0
- package/dist/utils/getParamLocation.d.ts +2 -0
- package/dist/utils/getParamLocation.js +6 -0
- package/dist/utils/getPropertyValueSpecifications.d.ts +2 -0
- package/dist/utils/getPropertyValueSpecifications.js +49 -0
- package/dist/utils/getRequestBodyValues.d.ts +11 -0
- package/dist/utils/getRequestBodyValues.js +122 -0
- package/dist/utils/getRequestIRIValues.d.ts +14 -0
- package/dist/utils/getRequestIRIValues.js +133 -0
- package/dist/utils/isBodyInit.d.ts +1 -0
- package/dist/utils/isBodyInit.js +21 -0
- package/dist/utils/isNil.d.ts +1 -0
- package/dist/utils/isNil.js +4 -0
- package/dist/utils/isObject.d.ts +6 -0
- package/dist/utils/isObject.js +6 -0
- package/dist/utils/isPopulatedObject.d.ts +5 -0
- package/dist/utils/isPopulatedObject.js +8 -0
- package/dist/utils/isPopulatedString.d.ts +1 -0
- package/dist/utils/isPopulatedString.js +4 -0
- package/dist/utils/joinPaths.d.ts +1 -0
- package/dist/utils/joinPaths.js +31 -0
- package/dist/utils/makeAppendProblemDetails.d.ts +14 -0
- package/dist/utils/makeAppendProblemDetails.js +26 -0
- package/dist/utils/makeURLPattern.d.ts +5 -0
- package/dist/utils/makeURLPattern.js +12 -0
- package/dist/utils/normalizeURL.d.ts +4 -0
- package/dist/utils/normalizeURL.js +11 -0
- package/dist/utils/parseSearchParams.d.ts +3 -0
- package/dist/utils/parseSearchParams.js +24 -0
- package/dist/utils/preferredMediaTypes.d.ts +42 -0
- package/dist/utils/preferredMediaTypes.js +149 -0
- package/dist/utils/urlToIRI.d.ts +1 -0
- package/dist/utils/urlToIRI.js +8 -0
- package/dist/utils/validateSpecValue.d.ts +1 -0
- package/dist/utils/validateSpecValue.js +1 -0
- package/dist/validators.d.ts +16 -0
- package/dist/validators.js +134 -0
- package/lib/accept.test.ts +55 -0
- package/lib/accept.ts +147 -0
- package/lib/action.test.ts +2 -0
- package/lib/actions/actionSets.ts +88 -0
- package/lib/actions/actions.ts +795 -0
- package/lib/actions/context.ts +170 -0
- package/lib/actions/meta.ts +251 -0
- package/lib/actions/path.test.ts +15 -0
- package/lib/actions/path.ts +99 -0
- package/lib/actions/spec.ts +545 -0
- package/lib/actions/types.ts +146 -0
- package/lib/actions/writer.test.ts +57 -0
- package/lib/actions/writer.ts +176 -0
- package/lib/auth/types.ts +22 -0
- package/lib/cache/cache.ts +291 -0
- package/lib/cache/etag.test.ts +122 -0
- package/lib/cache/etag.ts +106 -0
- package/lib/cache/memory.ts +52 -0
- package/lib/cache/types.ts +240 -0
- package/lib/errors.ts +66 -0
- package/lib/jsonld.ts +67 -0
- package/lib/makeTypeDefs.ts +138 -0
- package/lib/merge.ts +86 -0
- package/lib/mod.ts +14 -0
- package/lib/processAction.ts +690 -0
- package/lib/registry.test.ts +174 -0
- package/lib/registry.ts +455 -0
- package/lib/request.ts +153 -0
- package/lib/scopes.test.ts +70 -0
- package/lib/scopes.ts +178 -0
- package/lib/transformers/fileTransformer.ts +10 -0
- package/lib/types.ts +13 -0
- package/lib/utils/alwaysArray.ts +10 -0
- package/lib/utils/contextBuilder.ts +111 -0
- package/lib/utils/getActionContext.ts +76 -0
- package/lib/utils/getInternalName.ts +15 -0
- package/lib/utils/getParamLocation.ts +14 -0
- package/lib/utils/getPropertyValueSpecifications.ts +76 -0
- package/lib/utils/getRequestBodyValues.ts +155 -0
- package/lib/utils/getRequestIRIValues.ts +201 -0
- package/lib/utils/isBodyInit.ts +22 -0
- package/lib/utils/isNil.ts +4 -0
- package/lib/utils/isObject.ts +8 -0
- package/lib/utils/isPopulatedObject.ts +9 -0
- package/lib/utils/isPopulatedString.ts +4 -0
- package/lib/utils/joinPaths.ts +36 -0
- package/lib/utils/makeAppendProblemDetails.ts +57 -0
- package/lib/utils/makeURLPattern.ts +18 -0
- package/lib/utils/normalizeURL.ts +15 -0
- package/lib/utils/parseSearchParams.ts +36 -0
- package/lib/utils/preferredMediaTypes.ts +220 -0
- package/lib/utils/urlToIRI.ts +11 -0
- package/lib/utils/validateSpecValue.ts +0 -0
- package/lib/validators.ts +186 -0
- package/package.json +41 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export class InMemoryCacheMeta {
|
|
2
|
+
#details = new Map();
|
|
3
|
+
async get(key) {
|
|
4
|
+
const details = this.#details.get(key);
|
|
5
|
+
async function set(details) {
|
|
6
|
+
this.#details.set(key, details);
|
|
7
|
+
}
|
|
8
|
+
if (details == null) {
|
|
9
|
+
return {
|
|
10
|
+
type: 'cache-miss',
|
|
11
|
+
set,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
...details,
|
|
16
|
+
type: 'cache-hit',
|
|
17
|
+
set,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
async set(key, details) {
|
|
21
|
+
this.#details.set(key, details);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export class InMemoryCacheStorage {
|
|
25
|
+
#cache = new Map();
|
|
26
|
+
get(key) {
|
|
27
|
+
const value = this.#cache.get(key);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
set(key, value) {
|
|
31
|
+
this.#cache.set(key, value);
|
|
32
|
+
}
|
|
33
|
+
invalidate(key) {
|
|
34
|
+
this.#cache.delete(key);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { ImplementedAction } from "../actions/types.js";
|
|
2
|
+
import { CacheContext } from "../mod.js";
|
|
3
|
+
export type CacheStrategyType = 'http' | 'etag' | 'store';
|
|
4
|
+
export interface CacheEntryDescriptor {
|
|
5
|
+
contentType: string;
|
|
6
|
+
action: ImplementedAction;
|
|
7
|
+
request: Request;
|
|
8
|
+
args: CacheInstanceArgs;
|
|
9
|
+
}
|
|
10
|
+
export type CacheWhenFn = (ctx: CacheContext) => boolean;
|
|
11
|
+
export type CacheRuleArgs = {
|
|
12
|
+
/**
|
|
13
|
+
* A version which should increment every when a new release
|
|
14
|
+
* causes the existing cache to become stale.
|
|
15
|
+
*/
|
|
16
|
+
version?: number;
|
|
17
|
+
lock?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Defaults to varying on the authorization header
|
|
20
|
+
* when authenticated.
|
|
21
|
+
*/
|
|
22
|
+
vary?: string;
|
|
23
|
+
varyOnAuth?: boolean;
|
|
24
|
+
varyOnCapabilities?: string | string[];
|
|
25
|
+
/**
|
|
26
|
+
* Defaults to false when a querystring is present
|
|
27
|
+
* or the request is authenticated.
|
|
28
|
+
*
|
|
29
|
+
* @default 'public-no-query'
|
|
30
|
+
*/
|
|
31
|
+
when?: 'always' | 'public' | 'private' | 'no-query' | 'public-no-query' | 'private-no-query' | CacheWhenFn;
|
|
32
|
+
};
|
|
33
|
+
export type CacheControlArgs = {
|
|
34
|
+
private?: boolean;
|
|
35
|
+
public?: true;
|
|
36
|
+
noCache?: true;
|
|
37
|
+
noStore?: true;
|
|
38
|
+
mustRevalidate?: true;
|
|
39
|
+
mustUndestand?: true;
|
|
40
|
+
noTransform?: true;
|
|
41
|
+
immutable?: true;
|
|
42
|
+
proxyRevalidate?: true;
|
|
43
|
+
expires?: () => number | Date;
|
|
44
|
+
maxAge?: number | Date | (() => number | Date);
|
|
45
|
+
etag?: string;
|
|
46
|
+
};
|
|
47
|
+
export type CacheHTTPArgs = {
|
|
48
|
+
strategy: 'http';
|
|
49
|
+
strong?: undefined;
|
|
50
|
+
fromRequest?: undefined;
|
|
51
|
+
} & CacheRuleArgs & CacheControlArgs;
|
|
52
|
+
export type CacheHTTPInstanceArgs = CacheHTTPArgs & {
|
|
53
|
+
strategy: 'http';
|
|
54
|
+
cache: CacheBuilder;
|
|
55
|
+
};
|
|
56
|
+
export type CacheETagArgs = {
|
|
57
|
+
strategy: 'etag';
|
|
58
|
+
strong?: boolean;
|
|
59
|
+
fromRequest?: boolean;
|
|
60
|
+
etag?: undefined;
|
|
61
|
+
} & CacheRuleArgs & Omit<CacheControlArgs, 'etag'>;
|
|
62
|
+
export type CacheETagInstanceArgs = CacheETagArgs & {
|
|
63
|
+
stratey: 'etag';
|
|
64
|
+
cache: CacheBuilder;
|
|
65
|
+
};
|
|
66
|
+
export type CacheStoreArgs = {
|
|
67
|
+
strategy: 'store';
|
|
68
|
+
strong?: boolean;
|
|
69
|
+
fromRequest?: boolean;
|
|
70
|
+
etag?: undefined;
|
|
71
|
+
} & CacheRuleArgs & Omit<CacheControlArgs, 'etag'>;
|
|
72
|
+
export type CacheStoreInstanceArgs = CacheStoreArgs & {
|
|
73
|
+
strategy: 'store';
|
|
74
|
+
cache: CacheBuilder;
|
|
75
|
+
};
|
|
76
|
+
export type CacheInstanceArgs = CacheHTTPInstanceArgs | CacheETagInstanceArgs | CacheStoreInstanceArgs;
|
|
77
|
+
export type CacheDetails = {
|
|
78
|
+
key: string;
|
|
79
|
+
iri: string;
|
|
80
|
+
status?: number;
|
|
81
|
+
hasContent: boolean;
|
|
82
|
+
authKey: string;
|
|
83
|
+
etag: string;
|
|
84
|
+
headers: Headers;
|
|
85
|
+
contentType: string;
|
|
86
|
+
contentLength?: number;
|
|
87
|
+
contentEncoding?: string;
|
|
88
|
+
contentLanguage?: string;
|
|
89
|
+
contentRange?: string;
|
|
90
|
+
};
|
|
91
|
+
export type CacheHitHandle = CacheDetails & {
|
|
92
|
+
type: 'cache-hit';
|
|
93
|
+
set(details: CacheDetails): Promise<void>;
|
|
94
|
+
};
|
|
95
|
+
export type CacheMissHandle = {
|
|
96
|
+
type: 'cache-miss';
|
|
97
|
+
set(details: CacheDetails): Promise<void>;
|
|
98
|
+
};
|
|
99
|
+
export type LockedCacheMissHandle = {
|
|
100
|
+
type: 'locked-cache-miss';
|
|
101
|
+
set(details: CacheDetails): Promise<void>;
|
|
102
|
+
release(): Promise<void>;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* The cache meta knows how to query a storage for current freshness
|
|
106
|
+
* information on a cache entry without querying the data itself.
|
|
107
|
+
*
|
|
108
|
+
* Meta information might be stored in a separate data store to the
|
|
109
|
+
* cached response bodies. If supported by the storage, the representation
|
|
110
|
+
* can be locked for update preventing other requests for the same
|
|
111
|
+
* resource from proceeding until the cached representation is created.
|
|
112
|
+
*/
|
|
113
|
+
export interface CacheMeta {
|
|
114
|
+
/**
|
|
115
|
+
* Sets the cache details for a representation.
|
|
116
|
+
*
|
|
117
|
+
* @param key A unique key for this representation.
|
|
118
|
+
* @param details The cache details.
|
|
119
|
+
*/
|
|
120
|
+
set(key: string, details: CacheDetails): void | Promise<void>;
|
|
121
|
+
/**
|
|
122
|
+
* Retrieves the cache details of a representation.
|
|
123
|
+
*
|
|
124
|
+
* @param key A unique key for this representation.
|
|
125
|
+
*/
|
|
126
|
+
get(key: string): CacheHitHandle | CacheMissHandle | Promise<CacheHitHandle | CacheMissHandle>;
|
|
127
|
+
/**
|
|
128
|
+
* Retrieves the cache details of a representation and takes a lock
|
|
129
|
+
* for update if the representation is not current.
|
|
130
|
+
*
|
|
131
|
+
* Any other requests for this representation will wait for the request
|
|
132
|
+
* holding the lock to populate the cache before proceeding.
|
|
133
|
+
*
|
|
134
|
+
* @param key A unique key for this representation.
|
|
135
|
+
*/
|
|
136
|
+
getOrLock?(key: string): Promise<CacheHitHandle | LockedCacheMissHandle>;
|
|
137
|
+
}
|
|
138
|
+
export interface UpstreamCache {
|
|
139
|
+
/**
|
|
140
|
+
* Pushes a representation to the upstream cache.
|
|
141
|
+
*/
|
|
142
|
+
push(args: {
|
|
143
|
+
url: string;
|
|
144
|
+
headers: Headers;
|
|
145
|
+
data: Blob;
|
|
146
|
+
}): Promise<void>;
|
|
147
|
+
/**
|
|
148
|
+
* Invalidates a representation in the upstream cache.
|
|
149
|
+
*/
|
|
150
|
+
invalidate(url: string): Promise<void>;
|
|
151
|
+
}
|
|
152
|
+
export interface CacheStorage {
|
|
153
|
+
/**
|
|
154
|
+
* Retrieves a cache entry.
|
|
155
|
+
*/
|
|
156
|
+
get(key: string): Blob | Promise<Blob>;
|
|
157
|
+
/**
|
|
158
|
+
* Sets a cache entry.
|
|
159
|
+
*/
|
|
160
|
+
set(key: string, data: Blob): void | Promise<void>;
|
|
161
|
+
/**
|
|
162
|
+
* Invalidates a cache entry.
|
|
163
|
+
*/
|
|
164
|
+
invalidate(key: string): void | Promise<void>;
|
|
165
|
+
}
|
|
166
|
+
export interface CacheBuilder {
|
|
167
|
+
meta: CacheMeta;
|
|
168
|
+
storage: CacheStorage;
|
|
169
|
+
upstream: UpstreamCache | undefined;
|
|
170
|
+
http(args?: CacheHTTPArgs): CacheInstanceArgs;
|
|
171
|
+
etag(args?: CacheETagArgs): CacheInstanceArgs;
|
|
172
|
+
store(args?: CacheStoreArgs): CacheInstanceArgs;
|
|
173
|
+
invalidate?(request: Request): Promise<void>;
|
|
174
|
+
push?(request: Request): Promise<void>;
|
|
175
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ProblemDetails } from "./types.js";
|
|
2
|
+
export declare class ProblemDetailsError extends Error {
|
|
3
|
+
status: number;
|
|
4
|
+
problemDetails: ProblemDetails;
|
|
5
|
+
parentErr?: unknown;
|
|
6
|
+
constructor(status: number, problemDetails: string | ProblemDetails, parentErr?: unknown);
|
|
7
|
+
toContent(contentType?: string): string;
|
|
8
|
+
}
|
|
9
|
+
export declare class InvalidActionParamsError extends ProblemDetailsError {
|
|
10
|
+
constructor(title: string);
|
|
11
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// https://datatracker.ietf.org/doc/html/rfc9457
|
|
2
|
+
export class ProblemDetailsError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
problemDetails;
|
|
5
|
+
parentErr;
|
|
6
|
+
constructor(status, problemDetails, parentErr) {
|
|
7
|
+
const message = typeof problemDetails === 'string'
|
|
8
|
+
? problemDetails
|
|
9
|
+
: problemDetails.title;
|
|
10
|
+
super(message);
|
|
11
|
+
this.status = status;
|
|
12
|
+
this.parentErr = parentErr;
|
|
13
|
+
if (typeof problemDetails === 'string') {
|
|
14
|
+
this.problemDetails = { title: problemDetails };
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
this.problemDetails = problemDetails;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
toContent(contentType = 'application/ld+json') {
|
|
21
|
+
if (contentType === 'text/html') {
|
|
22
|
+
return `
|
|
23
|
+
<!doctype html>
|
|
24
|
+
<html lang="en">
|
|
25
|
+
<head>
|
|
26
|
+
<title>${this.problemDetails.title}</title>
|
|
27
|
+
</head>
|
|
28
|
+
<body>
|
|
29
|
+
<h1>${this.problemDetails.title}</h1>
|
|
30
|
+
|
|
31
|
+
<dl>
|
|
32
|
+
${this.problemDetails?.errors?.map((error) => {
|
|
33
|
+
return `
|
|
34
|
+
<dt>
|
|
35
|
+
<code>${error.name}</code>
|
|
36
|
+
</dt>
|
|
37
|
+
<dd>
|
|
38
|
+
${error.reason}
|
|
39
|
+
</dd>
|
|
40
|
+
`;
|
|
41
|
+
})}
|
|
42
|
+
</dl>
|
|
43
|
+
</body>
|
|
44
|
+
</html>
|
|
45
|
+
`;
|
|
46
|
+
}
|
|
47
|
+
return JSON.stringify(this.problemDetails);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export class InvalidActionParamsError extends ProblemDetailsError {
|
|
51
|
+
constructor(title) {
|
|
52
|
+
super(400, { title, type: 'invalid-param' });
|
|
53
|
+
}
|
|
54
|
+
}
|
package/dist/jsonld.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ShallowMerge } from "./merge.js";
|
|
2
|
+
export type RecursiveDigit = 1 | 2 | 3 | 4 | 5 | 6 | 7;
|
|
3
|
+
export type RecursiveNextDigit = [1, 2, 3, 4, 5, 6, 7, 'STOP'];
|
|
4
|
+
export type RecursiveIncrement<T> = T extends RecursiveDigit ? RecursiveNextDigit[T] : 'STOP';
|
|
5
|
+
export type EmptyObject = Pick<{
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
}, ''>;
|
|
8
|
+
export type GuardType<T> = T extends (value: any) => value is infer U ? U : never;
|
|
9
|
+
export type JSONPrimitive = string | number | boolean | null | undefined;
|
|
10
|
+
export type JSONValue = JSONPrimitive | JSONObject | JSONArray;
|
|
11
|
+
export type JSONObject = {
|
|
12
|
+
[member: string]: JSONValue;
|
|
13
|
+
};
|
|
14
|
+
export interface JSONArray extends Array<JSONValue> {
|
|
15
|
+
}
|
|
16
|
+
export type OrArray<T> = T | Array<T>;
|
|
17
|
+
export type Merge<T1 extends object, T2 extends object> = ShallowMerge<T1, T2>;
|
|
18
|
+
export type IRI = string;
|
|
19
|
+
export type IRIObject = {
|
|
20
|
+
'@id': IRI;
|
|
21
|
+
};
|
|
22
|
+
export type Aliases = Record<string, string>;
|
|
23
|
+
export type ContextVersion = 1.1;
|
|
24
|
+
export type ContextDefinitionType = '@id';
|
|
25
|
+
export type ContextDefinitionContainer = '@list' | '@set';
|
|
26
|
+
export type ContextDefinition = {
|
|
27
|
+
'@id'?: string;
|
|
28
|
+
'@type'?: ContextDefinitionType;
|
|
29
|
+
'@container'?: ContextDefinitionContainer;
|
|
30
|
+
'@context'?: JSONLDContext;
|
|
31
|
+
'@protected'?: boolean;
|
|
32
|
+
};
|
|
33
|
+
export type JSONLDContext = {
|
|
34
|
+
'@version'?: ContextVersion;
|
|
35
|
+
'@base'?: string;
|
|
36
|
+
'@protected'?: boolean;
|
|
37
|
+
'@vocab'?: string;
|
|
38
|
+
} & Record<string, string | ContextDefinition>;
|
|
39
|
+
export type TypeDef<Term extends string = any, Type extends string = any> = {
|
|
40
|
+
type: Type;
|
|
41
|
+
term: Term;
|
|
42
|
+
contextDefinition?: ContextDefinition;
|
|
43
|
+
};
|
package/dist/jsonld.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ContextDefinitionContainer, TypeDef } from "./jsonld.js";
|
|
2
|
+
import type { Context } from "./actions/context.js";
|
|
3
|
+
export type MakeTypeDefArgsFromType<Term extends string, Type extends string> = {
|
|
4
|
+
term: Term;
|
|
5
|
+
type: Type;
|
|
6
|
+
schema?: undefined;
|
|
7
|
+
isIRI?: boolean;
|
|
8
|
+
protect?: boolean;
|
|
9
|
+
container?: ContextDefinitionContainer;
|
|
10
|
+
context?: Context;
|
|
11
|
+
};
|
|
12
|
+
export type MakeTypeDefArgsFromSchema<Term extends string, Schema extends string> = {
|
|
13
|
+
term: Term;
|
|
14
|
+
schema: Schema;
|
|
15
|
+
type?: undefined;
|
|
16
|
+
isIRI?: boolean;
|
|
17
|
+
protect?: boolean;
|
|
18
|
+
container?: ContextDefinitionContainer;
|
|
19
|
+
context?: Context;
|
|
20
|
+
};
|
|
21
|
+
export type MakeTypeDefArgs<A extends string, B extends string> = MakeTypeDefArgsFromType<A, B> | MakeTypeDefArgsFromSchema<A, B>;
|
|
22
|
+
export declare function makeTypeDef<Term extends string, Schema extends string>(term: Term, type: Schema): TypeDef<Term, `${Schema}${Term}`>;
|
|
23
|
+
export declare function makeTypeDef<Term extends string, Type extends string>(args: MakeTypeDefArgsFromType<Term, Type>): TypeDef<Term, Type>;
|
|
24
|
+
export declare function makeTypeDef<Term extends string, Schema extends string>(args: MakeTypeDefArgsFromSchema<Term, Schema>): TypeDef<Term, `${Schema}${Term}`>;
|
|
25
|
+
export declare function makeTypeDefs<Term extends string, TD extends TypeDef<Term>>(typeDefs: Readonly<Array<TD>>): {
|
|
26
|
+
[TypeDef in TD as TypeDef['term']]: TypeDef;
|
|
27
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { joinPaths } from "./utils/joinPaths.js";
|
|
2
|
+
import { isPopulatedObject } from "./utils/isPopulatedObject.js";
|
|
3
|
+
import { isPopulatedString } from "./utils/isPopulatedString.js";
|
|
4
|
+
export function makeTypeDef(arg1, arg2) {
|
|
5
|
+
let contextDefinition = {};
|
|
6
|
+
let protect = false;
|
|
7
|
+
let args;
|
|
8
|
+
if (typeof arg1 === 'string') {
|
|
9
|
+
args = {
|
|
10
|
+
term: arg1,
|
|
11
|
+
schema: arg2,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
args = arg1;
|
|
16
|
+
if (typeof arg1.protect === 'boolean') {
|
|
17
|
+
protect = arg1.protect;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (args.isIRI || isPopulatedString(args.container) ||
|
|
21
|
+
isPopulatedObject(args.context)) {
|
|
22
|
+
contextDefinition = {};
|
|
23
|
+
if (args.isIRI) {
|
|
24
|
+
contextDefinition['@type'] = '@id';
|
|
25
|
+
}
|
|
26
|
+
if (isPopulatedString(args.container)) {
|
|
27
|
+
contextDefinition['@container'] = args.container;
|
|
28
|
+
}
|
|
29
|
+
if (isPopulatedObject(args.context)) {
|
|
30
|
+
contextDefinition['@context'] = args.context;
|
|
31
|
+
}
|
|
32
|
+
if (protect) {
|
|
33
|
+
contextDefinition['@protected'] = true;
|
|
34
|
+
}
|
|
35
|
+
if (isPopulatedString(args.schema)) {
|
|
36
|
+
return {
|
|
37
|
+
term: args.term,
|
|
38
|
+
type: joinPaths(args.schema, args.term),
|
|
39
|
+
contextDefinition,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
term: args.term,
|
|
44
|
+
type: args.type,
|
|
45
|
+
contextDefinition,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (protect) {
|
|
49
|
+
contextDefinition = { '@protected': true };
|
|
50
|
+
}
|
|
51
|
+
if (isPopulatedString(args.schema)) {
|
|
52
|
+
return {
|
|
53
|
+
term: args.term,
|
|
54
|
+
type: joinPaths(args.schema, args.term),
|
|
55
|
+
contextDefinition,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
term: args.term,
|
|
60
|
+
type: args.type,
|
|
61
|
+
contextDefinition,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function makeTypeDefs(typeDefs) {
|
|
65
|
+
const result = typeDefs.reduce((acc, typeDef) => ({
|
|
66
|
+
...acc,
|
|
67
|
+
[typeDef.term]: typeDef,
|
|
68
|
+
}), {});
|
|
69
|
+
return result;
|
|
70
|
+
}
|
package/dist/merge.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type ShallowMerge<T1 extends object, T2 extends object> = {
|
|
2
|
+
[K in keyof T1 & keyof T2]: T1[K] | T2[K];
|
|
3
|
+
} & ({
|
|
4
|
+
[K in Exclude<keyof T1, keyof T2>]: T1[K];
|
|
5
|
+
}) & ({
|
|
6
|
+
[K in Exclude<keyof T2, keyof T1>]: T2[K];
|
|
7
|
+
});
|
|
8
|
+
export type DeepMergeTwoTypes<T, U> = [
|
|
9
|
+
T,
|
|
10
|
+
U
|
|
11
|
+
] extends [any[], any[]] ? Zip_DeepMergeTwoTypes<T, U> : [T, U] extends [{
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}, {
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}] ? MergeTwoObjects<T, U> : T | U;
|
|
16
|
+
export type DeepMerge<T1, T2> = (T1 extends object ? (T2 extends object ? (MergeToOne<({
|
|
17
|
+
[K in (keyof T2 & keyof T1 & RequiredKeys<T1 | T2>)]: DeepMerge<T1[K], T2[K]>;
|
|
18
|
+
} & {
|
|
19
|
+
[K in (keyof T2 & keyof T1 & OptionalKeys<T1 | T2>)]?: DeepMerge<T1[K], T2[K]>;
|
|
20
|
+
} & {
|
|
21
|
+
[K in Exclude<RequiredKeys<T1>, keyof T2>]: T1[K];
|
|
22
|
+
} & {
|
|
23
|
+
[K in Exclude<OptionalKeys<T1>, keyof T2>]?: T1[K];
|
|
24
|
+
} & {
|
|
25
|
+
[K in Exclude<RequiredKeys<T2>, keyof T1>]: T2[K];
|
|
26
|
+
} & {
|
|
27
|
+
[K in Exclude<OptionalKeys<T2>, keyof T1>]?: T2[K];
|
|
28
|
+
})>) : (T1 extends object ? T2 : T1 | T2)) : (T2 extends object ? T1 : T1 | T2));
|
|
29
|
+
type Head<T> = T extends [infer I, ...infer _Rest] ? I : never;
|
|
30
|
+
type Tail<T> = T extends [infer _I, ...infer Rest] ? Rest : never;
|
|
31
|
+
type Zip_DeepMergeTwoTypes<T, U> = T extends [] ? U : U extends [] ? T : [
|
|
32
|
+
DeepMergeTwoTypes<Head<T>, Head<U>>,
|
|
33
|
+
...Zip_DeepMergeTwoTypes<Tail<T>, Tail<U>>
|
|
34
|
+
];
|
|
35
|
+
/**
|
|
36
|
+
* Take two objects T and U and create the new one with uniq keys for T a U objectI
|
|
37
|
+
* helper generic for `DeepMergeTwoTypes`
|
|
38
|
+
*/
|
|
39
|
+
type GetObjDifferentKeys<T, U, T0 = Omit<T, keyof U> & Omit<U, keyof T>, T1 = {
|
|
40
|
+
[K in keyof T0]: T0[K];
|
|
41
|
+
}> = T1;
|
|
42
|
+
/**
|
|
43
|
+
* Take two objects T and U and create the new one with the same objects keys
|
|
44
|
+
* helper generic for `DeepMergeTwoTypes`
|
|
45
|
+
*/
|
|
46
|
+
type GetObjSameKeys<T, U> = Omit<T | U, keyof GetObjDifferentKeys<T, U>>;
|
|
47
|
+
type MergeTwoObjects<T, U, T0 = Partial<GetObjDifferentKeys<T, U>> & {
|
|
48
|
+
[K in keyof GetObjSameKeys<T, U>]: DeepMergeTwoTypes<T[K], U[K]>;
|
|
49
|
+
}, T1 = {
|
|
50
|
+
[K in keyof T0]: T0[K];
|
|
51
|
+
}> = T1;
|
|
52
|
+
type MergeToOne<T> = (T extends object ? {
|
|
53
|
+
[K in keyof T]: (K extends RequiredKeys<T> ? Exclude<T[K], undefined> : T[K]);
|
|
54
|
+
} : never);
|
|
55
|
+
type RequiredKeys<T> = {
|
|
56
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? never : K;
|
|
57
|
+
}[keyof T];
|
|
58
|
+
type OptionalKeys<T> = {
|
|
59
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
60
|
+
}[keyof T];
|
|
61
|
+
export {};
|
package/dist/merge.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from './cache/types.js';
|
|
2
|
+
export * from './cache/cache.js';
|
|
3
|
+
export * from './cache/memory.js';
|
|
4
|
+
export * from './actions/types.js';
|
|
5
|
+
export * from './actions/meta.js';
|
|
6
|
+
export * from './actions/spec.js';
|
|
7
|
+
export * from './actions/context.js';
|
|
8
|
+
export * from './actions/path.js';
|
|
9
|
+
export * from './actions/actions.js';
|
|
10
|
+
export * from './actions/actionSets.js';
|
|
11
|
+
export * from './registry.js';
|
|
12
|
+
export * from './makeTypeDefs.js';
|
|
13
|
+
export * from './utils/joinPaths.js';
|
|
14
|
+
export * from './utils/contextBuilder.js';
|
package/dist/mod.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from './cache/types.js';
|
|
2
|
+
export * from './cache/cache.js';
|
|
3
|
+
export * from './cache/memory.js';
|
|
4
|
+
export * from './actions/types.js';
|
|
5
|
+
export * from './actions/meta.js';
|
|
6
|
+
export * from './actions/spec.js';
|
|
7
|
+
export * from './actions/context.js';
|
|
8
|
+
export * from './actions/path.js';
|
|
9
|
+
export * from './actions/actions.js';
|
|
10
|
+
export * from './actions/actionSets.js';
|
|
11
|
+
export * from './registry.js';
|
|
12
|
+
export * from './makeTypeDefs.js';
|
|
13
|
+
export * from './utils/joinPaths.js';
|
|
14
|
+
export * from './utils/contextBuilder.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ImplementedAction } from "./actions/types.js";
|
|
2
|
+
import type { ActionPayload, ActionSpec, ContextState, ParsedIRIValues } from "./actions/spec.js";
|
|
3
|
+
export type ProcessActionArgs<State extends ContextState = ContextState, Spec extends ActionSpec<ContextState> = ActionSpec<ContextState>> = {
|
|
4
|
+
iri: string;
|
|
5
|
+
req: Request;
|
|
6
|
+
spec: Spec;
|
|
7
|
+
state: State;
|
|
8
|
+
action: ImplementedAction<State, Spec>;
|
|
9
|
+
};
|
|
10
|
+
export type ProcessActionResult<Spec extends ActionSpec<ContextState> = ActionSpec<ContextState>> = {
|
|
11
|
+
params: ParsedIRIValues;
|
|
12
|
+
query: ParsedIRIValues;
|
|
13
|
+
payload: ActionPayload<Spec>;
|
|
14
|
+
};
|
|
15
|
+
export declare function processAction<State extends ContextState = ContextState, Spec extends ActionSpec<ContextState> = ActionSpec<ContextState>>({ iri, req, spec, state, action, }: ProcessActionArgs<State, Spec>): Promise<ProcessActionResult<Spec>>;
|