@occultist/occultist 0.0.4 → 0.0.6

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.
Files changed (82) hide show
  1. package/dist/accept.js +0 -1
  2. package/dist/actions/actionSets.d.ts +3 -3
  3. package/dist/actions/actions.d.ts +69 -48
  4. package/dist/actions/actions.js +39 -4
  5. package/dist/actions/context.d.ts +15 -11
  6. package/dist/actions/context.js +5 -0
  7. package/dist/actions/meta.d.ts +18 -12
  8. package/dist/actions/meta.js +114 -38
  9. package/dist/actions/spec.d.ts +3 -3
  10. package/dist/actions/types.d.ts +45 -16
  11. package/dist/actions/writer.d.ts +1 -1
  12. package/dist/actions/writer.test.js +2 -2
  13. package/dist/cache/cache.d.ts +3 -3
  14. package/dist/cache/cache.js +111 -42
  15. package/dist/cache/etag.test.js +1 -1
  16. package/dist/cache/file.d.ts +33 -1
  17. package/dist/cache/file.js +92 -10
  18. package/dist/cache/memory.d.ts +12 -2
  19. package/dist/cache/memory.js +63 -1
  20. package/dist/cache/types.d.ts +51 -22
  21. package/dist/errors.d.ts +1 -1
  22. package/dist/jsonld.d.ts +1 -1
  23. package/dist/makeTypeDefs.d.ts +2 -2
  24. package/dist/mod.d.ts +17 -15
  25. package/dist/mod.js +17 -15
  26. package/dist/processAction.d.ts +2 -2
  27. package/dist/processAction.js +1 -1
  28. package/dist/registry.d.ts +74 -8
  29. package/dist/registry.js +70 -8
  30. package/dist/registry.test.js +1 -1
  31. package/dist/scopes.d.ts +8 -8
  32. package/dist/scopes.js +8 -5
  33. package/dist/utils/contextBuilder.d.ts +1 -1
  34. package/dist/utils/getActionContext.d.ts +2 -2
  35. package/dist/utils/getPropertyValueSpecifications.d.ts +1 -1
  36. package/dist/utils/getRequestBodyValues.d.ts +3 -3
  37. package/dist/utils/getRequestIRIValues.d.ts +2 -2
  38. package/dist/utils/isPopulatedObject.js +1 -1
  39. package/dist/utils/makeAppendProblemDetails.d.ts +1 -1
  40. package/dist/utils/makeURLPattern.js +1 -0
  41. package/dist/utils/parseSearchParams.d.ts +2 -2
  42. package/dist/validators.d.ts +2 -2
  43. package/dist/validators.js +2 -2
  44. package/lib/accept.test.ts +1 -1
  45. package/lib/accept.ts +0 -2
  46. package/lib/actions/actionSets.ts +4 -4
  47. package/lib/actions/actions.ts +159 -99
  48. package/lib/actions/context.ts +22 -10
  49. package/lib/actions/meta.ts +140 -55
  50. package/lib/actions/path.test.ts +1 -1
  51. package/lib/actions/path.ts +1 -1
  52. package/lib/actions/spec.ts +3 -3
  53. package/lib/actions/types.ts +60 -15
  54. package/lib/actions/writer.test.ts +2 -2
  55. package/lib/actions/writer.ts +1 -1
  56. package/lib/cache/cache.ts +138 -52
  57. package/lib/cache/etag.test.ts +1 -1
  58. package/lib/cache/file.ts +113 -12
  59. package/lib/cache/memory.ts +85 -3
  60. package/lib/cache/types.ts +70 -23
  61. package/lib/errors.ts +1 -1
  62. package/lib/jsonld.ts +1 -1
  63. package/lib/makeTypeDefs.ts +5 -5
  64. package/lib/mod.ts +17 -15
  65. package/lib/processAction.ts +14 -14
  66. package/lib/registry.test.ts +1 -1
  67. package/lib/registry.ts +96 -19
  68. package/lib/request.ts +1 -1
  69. package/lib/scopes.test.ts +2 -2
  70. package/lib/scopes.ts +14 -11
  71. package/lib/utils/contextBuilder.ts +3 -3
  72. package/lib/utils/getActionContext.ts +4 -4
  73. package/lib/utils/getInternalName.ts +1 -1
  74. package/lib/utils/getPropertyValueSpecifications.ts +4 -4
  75. package/lib/utils/getRequestBodyValues.ts +5 -5
  76. package/lib/utils/getRequestIRIValues.ts +4 -4
  77. package/lib/utils/isPopulatedObject.ts +1 -1
  78. package/lib/utils/makeAppendProblemDetails.ts +1 -1
  79. package/lib/utils/makeURLPattern.ts +1 -0
  80. package/lib/utils/parseSearchParams.ts +2 -2
  81. package/lib/validators.ts +5 -5
  82. package/package.json +4 -2
package/dist/accept.js CHANGED
@@ -57,7 +57,6 @@ export class Accept {
57
57
  const accept = req.headers.get('Accept');
58
58
  const acceptLanguage = req.headers.get('Accept-Language');
59
59
  const acceptEncoding = req.headers.get('Accept-Encoding');
60
- console.log('ACCEPT', accept);
61
60
  return new Accept(accept, acceptLanguage, acceptEncoding);
62
61
  }
63
62
  debug() {
@@ -1,6 +1,6 @@
1
- import { type Accept } from "../accept.js";
2
- import type { ActionMeta } from "./meta.js";
3
- import type { ImplementedAction } from "./types.js";
1
+ import { type Accept } from "../accept.ts";
2
+ import type { ActionMeta } from "./meta.ts";
3
+ import type { ImplementedAction } from "./types.ts";
4
4
  export type UnsupportedContentTypeMatch = {
5
5
  type: 'unsupported-content-type';
6
6
  contentTypes: string[];
@@ -1,11 +1,11 @@
1
- import { CacheInstanceArgs } from '../cache/types.js';
2
- import type { JSONLDContext, JSONObject, TypeDef } from "../jsonld.js";
3
- import type { Registry } from '../registry.js';
4
- import type { Scope } from "../scopes.js";
5
- import { type ActionMeta } from "./meta.js";
6
- import type { ActionSpec, ContextState } from "./spec.js";
7
- import type { HandleRequestArgs, HandlerFn, HandlerMeta, HandlerObj, HandlerValue, HintArgs, ImplementedAction } from './types.js';
8
- import { ResponseTypes } from './writer.js';
1
+ import type { CacheInstanceArgs } from '../cache/types.ts';
2
+ import type { JSONLDContext, JSONObject, TypeDef } from "../jsonld.ts";
3
+ import type { Registry } from '../registry.ts';
4
+ import type { Scope } from "../scopes.ts";
5
+ import { type ActionMeta } from "./meta.ts";
6
+ import type { ActionSpec, ContextState } from "./spec.ts";
7
+ import type { AuthMiddleware, AuthState, HandleRequestArgs, HandlerFn, HandlerMeta, HandlerObj, HandlerValue, HintArgs, ImplementedAction } from './types.ts';
8
+ import { type ResponseTypes } from './writer.ts';
9
9
  export type DefineArgs<Term extends string = string, Spec extends ActionSpec = ActionSpec> = {
10
10
  typeDef?: TypeDef<Term>;
11
11
  spec?: Spec;
@@ -14,31 +14,31 @@ export type DefineArgs<Term extends string = string, Spec extends ActionSpec = A
14
14
  * A handler definition which can be pulled from a registry, scope or action
15
15
  * after an action is defined.
16
16
  */
17
- export declare class HandlerDefinition<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec> {
17
+ export declare class HandlerDefinition<State extends ContextState = ContextState, Auth extends AuthState = AuthState, Spec extends ActionSpec = ActionSpec> {
18
18
  name: string;
19
19
  contentType: string;
20
20
  handler: HandlerFn | HandlerValue;
21
21
  meta: HandlerMeta;
22
- action: ImplementedAction<State, Spec>;
22
+ action: ImplementedAction<State, Auth, Spec>;
23
23
  cache: ReadonlyArray<CacheInstanceArgs>;
24
- constructor(name: string, contentType: string, handler: HandlerFn | HandlerValue, meta: HandlerMeta, action: ImplementedAction<State, Spec>, actionMeta: ActionMeta);
24
+ constructor(name: string, contentType: string, handler: HandlerFn | HandlerValue, meta: HandlerMeta, action: ImplementedAction<State, Auth, Spec>, actionMeta: ActionMeta);
25
25
  get [Symbol.toStringTag](): string;
26
26
  }
27
- export interface Handleable<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec> {
27
+ export interface Handleable<State extends ContextState = ContextState, Auth extends AuthState = AuthState, Spec extends ActionSpec = ActionSpec> {
28
28
  /**
29
29
  * Defines the final handler for this content type.
30
30
  *
31
31
  * An action can have multiple handlers defined
32
32
  * each for a different set of content types.
33
33
  */
34
- handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State, Spec>): FinalizedAction<State, Spec>;
35
- handle(args: HandlerObj<State, Spec>): FinalizedAction<State, Spec>;
34
+ handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State, Auth, Spec>): FinalizedAction<State, Auth, Spec>;
35
+ handle(args: HandlerObj<State, Auth, Spec>): FinalizedAction<State, Auth, Spec>;
36
36
  }
37
- export declare class FinalizedAction<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec> implements Handleable<State, Spec>, ImplementedAction<State, Spec> {
37
+ export declare class FinalizedAction<State extends ContextState = ContextState, Auth extends AuthState = AuthState, Spec extends ActionSpec = ActionSpec> implements Handleable<State, Auth, Spec>, ImplementedAction<State, Auth, Spec> {
38
38
  #private;
39
- constructor(typeDef: TypeDef | undefined, spec: Spec, meta: ActionMeta<State, Spec>, handlerArgs: HandlerObj<State, Spec>);
40
- static fromHandlers<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec>(typeDef: TypeDef | undefined, spec: Spec, meta: ActionMeta<State, Spec>, contextType: string | string[], handler: HandlerValue | HandlerFn<State, Spec>): FinalizedAction<State, Spec>;
41
- static fromHandlers<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec>(typeDef: TypeDef | undefined, spec: Spec, meta: ActionMeta<State, Spec>, handlerArgs: HandlerObj<State, Spec>): FinalizedAction<State, Spec>;
39
+ constructor(typeDef: TypeDef | undefined, spec: Spec, meta: ActionMeta<State, Auth, Spec>, handlerArgs: HandlerObj<State, Auth, Spec>);
40
+ static fromHandlers<State extends ContextState = ContextState, Auth extends AuthState = AuthState, Spec extends ActionSpec = ActionSpec>(typeDef: TypeDef | undefined, spec: Spec, meta: ActionMeta<State, Auth, Spec>, contextType: string | string[], handler: HandlerValue | HandlerFn<State, Auth, Spec>): FinalizedAction<State, Auth, Spec>;
41
+ static fromHandlers<State extends ContextState = ContextState, Auth extends AuthState = AuthState, Spec extends ActionSpec = ActionSpec>(typeDef: TypeDef | undefined, spec: Spec, meta: ActionMeta<State, Auth, Spec>, handlerArgs: HandlerObj<State, Auth, Spec>): FinalizedAction<State, Auth, Spec>;
42
42
  static toJSONLD(action: ImplementedAction, scope: Scope): Promise<JSONObject | null>;
43
43
  get public(): boolean;
44
44
  get method(): string;
@@ -51,25 +51,32 @@ export declare class FinalizedAction<State extends ContextState = ContextState,
51
51
  get spec(): Spec;
52
52
  get scope(): Scope | undefined;
53
53
  get registry(): Registry;
54
- get handlers(): HandlerDefinition<State, Spec>[];
54
+ get handlers(): HandlerDefinition<State, Auth, Spec>[];
55
55
  get contentTypes(): string[];
56
56
  get context(): JSONObject;
57
57
  url(): string;
58
+ /**
59
+ * Retrives the handler configured for the given content type.
60
+ *
61
+ * @param contentType The content type.
62
+ */
63
+ handlerFor(contentType: string): HandlerDefinition<State, Auth, Spec> | undefined;
58
64
  jsonld(): Promise<JSONObject | null>;
59
65
  jsonldPartial(): {
60
66
  '@type': string;
61
67
  '@id': string;
62
68
  } | null;
63
- handle(contentType: string | string[], handler: HandlerFn<State, Spec> | HandlerValue): FinalizedAction<State, Spec>;
64
- handle(args: HandlerObj<State, Spec>): FinalizedAction<State, Spec>;
69
+ handle(contentType: string | string[], handler: HandlerFn<State, Auth, Spec> | HandlerValue): FinalizedAction<State, Auth, Spec>;
70
+ handle(args: HandlerObj<State, Auth, Spec>): FinalizedAction<State, Auth, Spec>;
65
71
  handleRequest(args: HandleRequestArgs): Promise<ResponseTypes>;
72
+ perform(req: Request): Promise<Response>;
66
73
  }
67
74
  export interface Applicable<ActionType> {
68
75
  use(): ActionType;
69
76
  }
70
- export declare class DefinedAction<State extends ContextState = ContextState, Term extends string = string, Spec extends ActionSpec = ActionSpec> implements Applicable<DefinedAction<State, Term, Spec>>, Handleable<State, Spec>, ImplementedAction<State, Spec> {
77
+ export declare class DefinedAction<State extends ContextState = ContextState, Auth extends AuthState = AuthState, Term extends string = string, Spec extends ActionSpec = ActionSpec> implements Applicable<DefinedAction<State, Auth, Term, Spec>>, Handleable<State, Auth, Spec>, ImplementedAction<State, Auth, Spec> {
71
78
  #private;
72
- constructor(typeDef: TypeDef | undefined, spec: Spec, meta: ActionMeta<State, Spec>);
79
+ constructor(typeDef: TypeDef | undefined, spec: Spec, meta: ActionMeta<State, Auth, Spec>);
73
80
  get public(): boolean;
74
81
  get method(): string;
75
82
  get term(): string | undefined;
@@ -82,9 +89,15 @@ export declare class DefinedAction<State extends ContextState = ContextState, Te
82
89
  get spec(): Spec;
83
90
  get scope(): Scope | undefined;
84
91
  get registry(): Registry;
85
- get handlers(): HandlerDefinition<State, Spec>[];
92
+ get handlers(): HandlerDefinition<State, Auth, Spec>[];
86
93
  get contentTypes(): string[];
87
94
  url(): string;
95
+ /**
96
+ * Retrives the handler configured for the given content type.
97
+ *
98
+ * @param contentType The content type.
99
+ */
100
+ handlerFor(_contentType: string): undefined;
88
101
  get context(): JSONLDContext;
89
102
  jsonld(): Promise<JSONObject | null>;
90
103
  jsonldPartial(): {
@@ -98,14 +111,15 @@ export declare class DefinedAction<State extends ContextState = ContextState, Te
98
111
  * if validating and transforming the action payload might cause
99
112
  * auth sensitive checks to be run which might reject the request.
100
113
  */
101
- cache(args: CacheInstanceArgs): DefinedAction<State, string, Spec>;
102
- meta(): DefinedAction<State, string, Spec>;
103
- use(): DefinedAction<State, string, Spec>;
104
- handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State, Spec>): FinalizedAction<State, Spec>;
105
- handle(args: HandlerObj<State, Spec>): FinalizedAction<State, Spec>;
114
+ cache(args: CacheInstanceArgs): DefinedAction<State, Auth, Term, Spec>;
115
+ meta(): DefinedAction<State, Auth, Term, Spec>;
116
+ use(): DefinedAction<State, Auth, Term, Spec>;
117
+ handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State, Auth, Spec>): FinalizedAction<State, Auth, Spec>;
118
+ handle(args: HandlerObj<State, Auth, Spec>): FinalizedAction<State, Auth, Spec>;
106
119
  handleRequest(args: HandleRequestArgs): Promise<ResponseTypes>;
120
+ perform(req: Request): Promise<Response>;
107
121
  }
108
- export declare class Action<State extends ContextState = ContextState> implements Applicable<Action>, Handleable<State>, ImplementedAction<State> {
122
+ export declare class Action<State extends ContextState = ContextState, Auth extends AuthState = AuthState> implements Applicable<Action>, Handleable<State>, ImplementedAction<State> {
109
123
  #private;
110
124
  constructor(meta: ActionMeta<State>);
111
125
  get public(): boolean;
@@ -124,40 +138,47 @@ export declare class Action<State extends ContextState = ContextState> implement
124
138
  get contentTypes(): string[];
125
139
  get context(): JSONObject;
126
140
  url(): string;
141
+ /**
142
+ * Retrives the handler configured for the given content type.
143
+ *
144
+ * @param contentType The content type.
145
+ */
146
+ handlerFor(_contentType: string): undefined;
127
147
  jsonld(): Promise<null>;
128
148
  jsonldPartial(): {
129
149
  '@type': string;
130
150
  '@id': string;
131
151
  } | null;
132
152
  use(): Action<State>;
133
- define<Term extends string = string, Spec extends ActionSpec = ActionSpec>(args: DefineArgs<Term, Spec>): DefinedAction<State, Term, Spec>;
153
+ define<Auth extends AuthState = AuthState, Term extends string = string, Spec extends ActionSpec = ActionSpec>(args: DefineArgs<Term, Spec>): DefinedAction<State, Auth, Term, Spec>;
134
154
  handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State>): FinalizedAction<State>;
135
155
  handle(args: HandlerObj<State>): FinalizedAction<State>;
136
156
  handleRequest(args: HandleRequestArgs): Promise<ResponseTypes>;
157
+ perform(req: Request): Promise<Response>;
137
158
  }
138
- export declare class PreAction<State extends ContextState = ContextState> implements Applicable<Action>, Handleable<State> {
159
+ export declare class PreAction<State extends ContextState = ContextState, Auth extends AuthState = AuthState> implements Applicable<Action>, Handleable<State, Auth> {
139
160
  #private;
140
- constructor(meta: ActionMeta<State>);
141
- use(): Action<State>;
142
- define<Term extends string = string, Spec extends ActionSpec = ActionSpec>(args: DefineArgs<Term, Spec>): DefinedAction<State, Term, Spec>;
143
- handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State>): FinalizedAction<State>;
144
- handle(args: HandlerObj<State>): FinalizedAction<State>;
161
+ constructor(meta: ActionMeta<State, Auth>);
162
+ use(): Action<State, AuthState>;
163
+ define<Term extends string = string, Spec extends ActionSpec = ActionSpec>(args: DefineArgs<Term, Spec>): DefinedAction<State, Auth, Term, Spec>;
164
+ handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State, Auth>): FinalizedAction<State, Auth>;
165
+ handle(args: HandlerObj<State>): FinalizedAction<State, Auth>;
145
166
  }
146
- export declare class Endpoint<State extends ContextState = ContextState> implements Applicable<Action>, Handleable<State> {
167
+ export declare class Endpoint<State extends ContextState = ContextState, Auth extends AuthState = AuthState> implements Applicable<Action>, Handleable<State, Auth> {
147
168
  #private;
148
- constructor(meta: ActionMeta<State>);
149
- hint(hints: HintArgs): Endpoint<State>;
150
- compress(): Endpoint<State>;
151
- cache(args: CacheInstanceArgs): this;
169
+ constructor(meta: ActionMeta<State, Auth>);
170
+ hint(hints: HintArgs): Endpoint<State, Auth>;
171
+ compress(): Endpoint<State, Auth>;
172
+ cache(args: CacheInstanceArgs): Endpoint<State, Auth>;
152
173
  etag(): this;
153
- use(): Action<State>;
154
- define<Term extends string = string, Spec extends ActionSpec = ActionSpec>(args: DefineArgs<Term, Spec>): DefinedAction<State, Term, Spec>;
155
- handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State>): FinalizedAction<State>;
156
- handle(args: HandlerObj<State>): FinalizedAction<State>;
174
+ use(): Action<State, Auth>;
175
+ define<Term extends string = string, Spec extends ActionSpec = ActionSpec>(args: DefineArgs<Term, Spec>): DefinedAction<State, Auth, Term, Spec>;
176
+ handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State>): FinalizedAction<State, Auth>;
177
+ handle(args: HandlerObj<State, Auth>): FinalizedAction<State, Auth>;
157
178
  }
158
179
  export declare class ActionAuth<State extends ContextState = ContextState> {
159
180
  #private;
160
181
  constructor(meta: ActionMeta<State>);
161
- public(): Endpoint<State>;
162
- private(): Endpoint<State>;
182
+ public<Auth extends AuthState = AuthState>(authMiddleware?: AuthMiddleware<Auth>): Endpoint<State, Auth>;
183
+ private<Auth extends AuthState = AuthState>(authMiddleware: AuthMiddleware<Auth>): Endpoint<State, Auth>;
163
184
  }
@@ -1,6 +1,6 @@
1
1
  import { getActionContext } from "../utils/getActionContext.js";
2
2
  import { getPropertyValueSpecifications } from "../utils/getPropertyValueSpecifications.js";
3
- import { isPopulatedObject } from '../utils/isPopulatedObject.js';
3
+ import { isPopulatedObject } from "../utils/isPopulatedObject.js";
4
4
  import { joinPaths } from "../utils/joinPaths.js";
5
5
  import { AfterDefinition, BeforeDefinition } from "./meta.js";
6
6
  function isHandlerObj(handler) {
@@ -131,6 +131,14 @@ export class FinalizedAction {
131
131
  url() {
132
132
  return joinPaths(this.#meta.registry.rootIRI, this.#meta.path.normalized);
133
133
  }
134
+ /**
135
+ * Retrives the handler configured for the given content type.
136
+ *
137
+ * @param contentType The content type.
138
+ */
139
+ handlerFor(contentType) {
140
+ return this.#handlers.get(contentType);
141
+ }
134
142
  jsonld() {
135
143
  const scope = this.#meta.scope;
136
144
  return FinalizedAction.toJSONLD(this, scope);
@@ -175,7 +183,7 @@ export class FinalizedAction {
175
183
  }
176
184
  return this;
177
185
  }
178
- async handleRequest(args) {
186
+ handleRequest(args) {
179
187
  const handler = this.#handlers.get(args.contentType);
180
188
  return this.#meta.handleRequest({
181
189
  ...args,
@@ -183,6 +191,9 @@ export class FinalizedAction {
183
191
  handler,
184
192
  });
185
193
  }
194
+ perform(req) {
195
+ return this.#meta.perform(req);
196
+ }
186
197
  }
187
198
  export class DefinedAction {
188
199
  #spec;
@@ -239,6 +250,12 @@ export class DefinedAction {
239
250
  url() {
240
251
  return '';
241
252
  }
253
+ /**
254
+ * Retrives the handler configured for the given content type.
255
+ *
256
+ * @param contentType The content type.
257
+ */
258
+ handlerFor(_contentType) { }
242
259
  get context() {
243
260
  return getActionContext({
244
261
  spec: this.#spec,
@@ -295,6 +312,9 @@ export class DefinedAction {
295
312
  spec: this.#spec,
296
313
  });
297
314
  }
315
+ perform(req) {
316
+ return this.#meta.perform(req);
317
+ }
298
318
  }
299
319
  export class Action {
300
320
  #spec = {};
@@ -355,6 +375,12 @@ export class Action {
355
375
  url() {
356
376
  return '';
357
377
  }
378
+ /**
379
+ * Retrives the handler configured for the given content type.
380
+ *
381
+ * @param contentType The content type.
382
+ */
383
+ handlerFor(_contentType) { }
358
384
  jsonld() {
359
385
  return Promise.resolve(null);
360
386
  }
@@ -376,6 +402,9 @@ export class Action {
376
402
  spec: this.#spec,
377
403
  });
378
404
  }
405
+ perform(req) {
406
+ return this.#meta.perform(req);
407
+ }
379
408
  }
380
409
  export class PreAction {
381
410
  #meta;
@@ -426,11 +455,17 @@ export class ActionAuth {
426
455
  constructor(meta) {
427
456
  this.#meta = meta;
428
457
  }
429
- public() {
458
+ public(authMiddleware) {
459
+ if (authMiddleware != null && typeof authMiddleware !== 'function')
460
+ throw new Error('Public action given invalid auth middleware');
430
461
  this.#meta.public = true;
462
+ this.#meta.auth = authMiddleware;
431
463
  return new Endpoint(this.#meta);
432
464
  }
433
- private() {
465
+ private(authMiddleware) {
466
+ if (typeof authMiddleware !== 'function')
467
+ throw new Error('Private action given invalid auth middleware');
468
+ this.#meta.auth = authMiddleware;
434
469
  return new Endpoint(this.#meta);
435
470
  }
436
471
  }
@@ -1,14 +1,15 @@
1
- import type { HandlerDefinition } from "../mod.js";
2
- import type { Registry } from "../registry.js";
3
- import type { ActionPayload, ActionSpec, ContextState, ParsedIRIValues } from "./spec.js";
4
- import type { ImplementedAction } from "./types.js";
5
- import type { ResponseBody } from "./writer.js";
6
- export type CacheContextArgs = {
1
+ import type { HandlerDefinition } from "../mod.ts";
2
+ import type { Registry } from "../registry.ts";
3
+ import type { ActionPayload, ActionSpec, ContextState, ParsedIRIValues } from "./spec.ts";
4
+ import type { AuthState, ImplementedAction } from "./types.ts";
5
+ import type { ResponseBody } from "./writer.ts";
6
+ export type CacheContextArgs<Auth extends AuthState = AuthState> = {
7
7
  req: Request;
8
8
  url: string;
9
9
  contentType: string;
10
10
  public: boolean;
11
11
  authKey?: string;
12
+ auth: Auth;
12
13
  handler: HandlerDefinition;
13
14
  params: ParsedIRIValues;
14
15
  query: ParsedIRIValues;
@@ -16,7 +17,7 @@ export type CacheContextArgs = {
16
17
  /**
17
18
  * Request context object.
18
19
  */
19
- export declare class CacheContext {
20
+ export declare class CacheContext<Auth extends AuthState = AuthState> {
20
21
  #private;
21
22
  req: Request;
22
23
  method: string;
@@ -24,12 +25,13 @@ export declare class CacheContext {
24
25
  contentType: string;
25
26
  public: boolean;
26
27
  authKey?: string;
28
+ auth: Auth;
27
29
  action: ImplementedAction;
28
30
  registry: Registry;
29
31
  params: ParsedIRIValues;
30
32
  query: ParsedIRIValues;
31
33
  headers: Headers;
32
- constructor(args: CacheContextArgs);
34
+ constructor(args: CacheContextArgs<Auth>);
33
35
  get hit(): boolean;
34
36
  set hit(hit: boolean);
35
37
  get status(): undefined | number;
@@ -40,12 +42,13 @@ export declare class CacheContext {
40
42
  set etag(etag: string);
41
43
  get [Symbol.toStringTag](): string;
42
44
  }
43
- export type ContextArgs<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec> = {
45
+ export type ContextArgs<State extends ContextState = ContextState, Auth extends AuthState = AuthState, Spec extends ActionSpec = ActionSpec> = {
44
46
  req: Request;
45
47
  url: string;
46
48
  contentType: string;
47
49
  public: boolean;
48
50
  authKey?: string;
51
+ auth: Auth;
49
52
  handler: HandlerDefinition<State, Spec>;
50
53
  params: ParsedIRIValues;
51
54
  query: ParsedIRIValues;
@@ -54,7 +57,7 @@ export type ContextArgs<State extends ContextState = ContextState, Spec extends
54
57
  /**
55
58
  * Request context object.
56
59
  */
57
- export declare class Context<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec> {
60
+ export declare class Context<State extends ContextState = ContextState, Auth extends AuthState = AuthState, Spec extends ActionSpec = ActionSpec> {
58
61
  #private;
59
62
  req: Request;
60
63
  method: string;
@@ -62,6 +65,7 @@ export declare class Context<State extends ContextState = ContextState, Spec ext
62
65
  contentType: string;
63
66
  public: boolean;
64
67
  authKey?: string;
68
+ auth: Auth;
65
69
  state: State;
66
70
  action: ImplementedAction<State, Spec>;
67
71
  registry: Registry;
@@ -69,7 +73,7 @@ export declare class Context<State extends ContextState = ContextState, Spec ext
69
73
  query: ParsedIRIValues;
70
74
  payload: ActionPayload<Spec>;
71
75
  headers: Headers;
72
- constructor(args: ContextArgs<State, Spec>);
76
+ constructor(args: ContextArgs<State, Auth, Spec>);
73
77
  get status(): undefined | number;
74
78
  set status(status: number);
75
79
  get body(): undefined | ResponseBody;
@@ -16,6 +16,7 @@ export class CacheContext {
16
16
  contentType;
17
17
  public = false;
18
18
  authKey;
19
+ auth;
19
20
  action;
20
21
  registry;
21
22
  params;
@@ -27,6 +28,7 @@ export class CacheContext {
27
28
  this.contentType = args.contentType;
28
29
  this.public = args.public;
29
30
  this.authKey = args.authKey;
31
+ this.auth = args.auth;
30
32
  this.action = args.handler.action;
31
33
  this.method = args.handler.action.method;
32
34
  this.registry = args.handler.action.registry;
@@ -73,6 +75,7 @@ export class Context {
73
75
  contentType;
74
76
  public = false;
75
77
  authKey;
78
+ auth;
76
79
  state = {};
77
80
  action;
78
81
  registry;
@@ -86,6 +89,7 @@ export class Context {
86
89
  this.contentType = args.contentType;
87
90
  this.public = args.public;
88
91
  this.authKey = args.authKey;
92
+ this.auth = args.auth;
89
93
  this.action = args.handler.action;
90
94
  this.method = args.handler.action.method;
91
95
  this.registry = args.handler.action.registry;
@@ -93,6 +97,7 @@ export class Context {
93
97
  this.query = args.query;
94
98
  this.payload = args.payload;
95
99
  Object.freeze(this);
100
+ Object.freeze(this.auth);
96
101
  }
97
102
  get status() {
98
103
  return this.#editable.status;
@@ -1,15 +1,15 @@
1
- import { CacheInstanceArgs } from '../cache/types.js';
2
- import { JSONValue } from '../jsonld.js';
3
- import type { Registry } from '../registry.js';
4
- import type { Scope } from "../scopes.js";
5
- import { HandlerDefinition } from './actions.js';
6
- import { Path } from "./path.js";
7
- import type { ActionSpec, ContextState, FileValue, TransformerFn } from './spec.js';
8
- import type { HintArgs, ImplementedAction } from './types.js';
9
- import type { HTTPWriter, ResponseTypes } from "./writer.js";
1
+ import type { CacheInstanceArgs } from '../cache/types.ts';
2
+ import type { JSONValue } from '../jsonld.ts';
3
+ import type { Registry } from '../registry.ts';
4
+ import type { Scope } from "../scopes.ts";
5
+ import { HandlerDefinition } from './actions.ts';
6
+ import { Path } from "./path.ts";
7
+ import type { ActionSpec, ContextState, FileValue, TransformerFn } from './spec.ts';
8
+ import type { AuthMiddleware, AuthState, CacheHitHeader, HintArgs, ImplementedAction } from './types.ts';
9
+ import { type HTTPWriter, type ResponseTypes } from "./writer.ts";
10
10
  export declare const BeforeDefinition = 0;
11
11
  export declare const AfterDefinition = 1;
12
- export declare class ActionMeta<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec> {
12
+ export declare class ActionMeta<State extends ContextState = ContextState, Auth extends AuthState = AuthState, Spec extends ActionSpec = ActionSpec> {
13
13
  #private;
14
14
  rootIRI: string;
15
15
  method: string;
@@ -27,6 +27,7 @@ export declare class ActionMeta<State extends ContextState = ContextState, Spec
27
27
  acceptCache: Set<string>;
28
28
  compressBeforeCache: boolean;
29
29
  cacheOccurance: 0 | 1;
30
+ auth?: AuthMiddleware<Auth>;
30
31
  cache: CacheInstanceArgs[];
31
32
  serverTiming: boolean;
32
33
  constructor(rootIRI: string, method: string, name: string, uriTemplate: string, registry: Registry, writer: HTTPWriter, scope?: Scope);
@@ -34,8 +35,11 @@ export declare class ActionMeta<State extends ContextState = ContextState, Spec
34
35
  * Called when the API is defined to compute all uncomputed values.
35
36
  */
36
37
  finalize(): void;
37
- handleRequest({ startTime, contentType, language: _language, encoding: _encoding, url, req, writer, spec, handler, }: {
38
- startTime: number;
38
+ perform(req: Request): Promise<Response>;
39
+ /**
40
+ *
41
+ */
42
+ handleRequest({ contentType, url, req, writer, spec, handler, cacheHitHeader, startTime, }: {
39
43
  contentType?: string;
40
44
  language?: string;
41
45
  encoding?: string;
@@ -44,6 +48,8 @@ export declare class ActionMeta<State extends ContextState = ContextState, Spec
44
48
  writer: HTTPWriter;
45
49
  spec?: Spec;
46
50
  handler?: HandlerDefinition<State, Spec>;
51
+ cacheHitHeader?: CacheHitHeader;
52
+ startTime?: number;
47
53
  }): Promise<ResponseTypes>;
48
54
  get [Symbol.toStringTag](): string;
49
55
  }