@occultist/occultist 0.0.5 → 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 +32 -1
  17. package/dist/cache/file.js +89 -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 +109 -13
  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
@@ -1,15 +1,15 @@
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 {getActionContext} from "../utils/getActionContext.js";
6
- import {getPropertyValueSpecifications} from "../utils/getPropertyValueSpecifications.js";
7
- import {isPopulatedObject} from '../utils/isPopulatedObject.js';
8
- import {joinPaths} from "../utils/joinPaths.js";
9
- import {AfterDefinition, BeforeDefinition, type ActionMeta} from "./meta.js";
10
- import type {ActionSpec, ContextState} from "./spec.js";
11
- import type {HandlerArgs, HandleRequestArgs, HandlerFn, HandlerMeta, HandlerObj, HandlerValue, HintArgs, ImplementedAction} from './types.js';
12
- 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 {getActionContext} from "../utils/getActionContext.ts";
6
+ import {getPropertyValueSpecifications} from "../utils/getPropertyValueSpecifications.ts";
7
+ import {isPopulatedObject} from '../utils/isPopulatedObject.ts';
8
+ import {joinPaths} from "../utils/joinPaths.ts";
9
+ import {AfterDefinition, BeforeDefinition, type ActionMeta} from "./meta.ts";
10
+ import type {ActionSpec, ContextState} from "./spec.ts";
11
+ import type {AuthMiddleware, AuthState, HandleRequestArgs, HandlerFn, HandlerMeta, HandlerObj, HandlerValue, HintArgs, ImplementedAction} from './types.ts';
12
+ import {type ResponseTypes} from './writer.ts';
13
13
 
14
14
 
15
15
  export type DefineArgs<
@@ -23,8 +23,9 @@ export type DefineArgs<
23
23
 
24
24
  function isHandlerObj<
25
25
  State extends ContextState = ContextState,
26
+ Auth extends AuthState = AuthState,
26
27
  Spec extends ActionSpec = ActionSpec
27
- >(handler: unknown): handler is HandlerObj<State, Spec> {
28
+ >(handler: unknown): handler is HandlerObj<State, Auth, Spec> {
28
29
  return isPopulatedObject(handler);
29
30
  }
30
31
 
@@ -34,13 +35,14 @@ function isHandlerObj<
34
35
  */
35
36
  export class HandlerDefinition<
36
37
  State extends ContextState = ContextState,
38
+ Auth extends AuthState = AuthState,
37
39
  Spec extends ActionSpec = ActionSpec,
38
40
  > {
39
41
  name: string;
40
42
  contentType: string;
41
43
  handler: HandlerFn | HandlerValue;
42
44
  meta: HandlerMeta;
43
- action: ImplementedAction<State, Spec>;
45
+ action: ImplementedAction<State, Auth, Spec>;
44
46
  cache: ReadonlyArray<CacheInstanceArgs>;
45
47
 
46
48
  constructor(
@@ -48,7 +50,7 @@ export class HandlerDefinition<
48
50
  contentType: string,
49
51
  handler: HandlerFn | HandlerValue,
50
52
  meta: HandlerMeta,
51
- action: ImplementedAction<State, Spec>,
53
+ action: ImplementedAction<State, Auth, Spec>,
52
54
  actionMeta: ActionMeta,
53
55
  ) {
54
56
  this.name = name;
@@ -75,6 +77,7 @@ export class HandlerDefinition<
75
77
 
76
78
  export interface Handleable<
77
79
  State extends ContextState = ContextState,
80
+ Auth extends AuthState = AuthState,
78
81
  Spec extends ActionSpec = ActionSpec,
79
82
  > {
80
83
 
@@ -86,39 +89,40 @@ export interface Handleable<
86
89
  */
87
90
  handle(
88
91
  contentType: string | string[],
89
- handler: HandlerValue | HandlerFn<State, Spec>,
90
- ): FinalizedAction<State, Spec>;
92
+ handler: HandlerValue | HandlerFn<State, Auth, Spec>,
93
+ ): FinalizedAction<State, Auth, Spec>;
91
94
 
92
95
  handle(
93
- args: HandlerObj<State, Spec>,
94
- ): FinalizedAction<State, Spec>;
96
+ args: HandlerObj<State, Auth, Spec>,
97
+ ): FinalizedAction<State, Auth, Spec>;
95
98
  }
96
99
 
97
100
  export class FinalizedAction<
98
101
  State extends ContextState = ContextState,
102
+ Auth extends AuthState = AuthState,
99
103
  Spec extends ActionSpec = ActionSpec,
100
104
  > implements
101
- Handleable<State, Spec>,
102
- ImplementedAction<State, Spec>
105
+ Handleable<State, Auth, Spec>,
106
+ ImplementedAction<State, Auth, Spec>
103
107
  {
104
108
  #spec: Spec;
105
- #meta: ActionMeta<State, Spec>;
109
+ #meta: ActionMeta<State, Auth, Spec>;
106
110
  #typeDef?: TypeDef;
107
- #handlers: Map<string, HandlerDefinition<State, Spec>>;
111
+ #handlers: Map<string, HandlerDefinition<State, Auth, Spec>>;
108
112
 
109
113
  constructor(
110
114
  typeDef: TypeDef | undefined,
111
115
  spec: Spec,
112
- meta: ActionMeta<State, Spec>,
113
- handlerArgs: HandlerObj<State, Spec>,
116
+ meta: ActionMeta<State, Auth, Spec>,
117
+ handlerArgs: HandlerObj<State, Auth, Spec>,
114
118
  ) {
115
119
  this.#typeDef = typeDef;
116
120
  this.#spec = spec ?? {} as Spec;
117
121
  this.#meta = meta;
118
122
 
119
- this.#meta.action = this as unknown as ImplementedAction<State, Spec>;
123
+ this.#meta.action = this as unknown as ImplementedAction<State, Auth, Spec>;
120
124
 
121
- const handlers: Map<string, HandlerDefinition<State, Spec>> = new Map();
125
+ const handlers: Map<string, HandlerDefinition<State, Auth, Spec>> = new Map();
122
126
 
123
127
  if (typeof handlerArgs.contentType === 'string') {
124
128
  handlers.set(handlerArgs.contentType, new HandlerDefinition(
@@ -126,7 +130,7 @@ export class FinalizedAction<
126
130
  handlerArgs.contentType,
127
131
  handlerArgs.handler,
128
132
  handlerArgs.meta,
129
- this as unknown as ImplementedAction<State, Spec>,
133
+ this as unknown as ImplementedAction<State, Auth, Spec>,
130
134
  this.#meta,
131
135
  ));
132
136
  } else if (isPopulatedObject(handlerArgs)) {
@@ -136,7 +140,7 @@ export class FinalizedAction<
136
140
  handlerArgs.contentType[i],
137
141
  handlerArgs.handler,
138
142
  handlerArgs.meta,
139
- this as unknown as ImplementedAction<State, Spec>,
143
+ this as unknown as ImplementedAction<State, Auth, Spec>,
140
144
  this.#meta,
141
145
  ));
142
146
  }
@@ -147,37 +151,40 @@ export class FinalizedAction<
147
151
 
148
152
  static fromHandlers<
149
153
  State extends ContextState = ContextState,
154
+ Auth extends AuthState = AuthState,
150
155
  Spec extends ActionSpec = ActionSpec,
151
156
  >(
152
157
  typeDef: TypeDef | undefined,
153
158
  spec: Spec,
154
- meta: ActionMeta<State, Spec>,
159
+ meta: ActionMeta<State, Auth, Spec>,
155
160
  contextType: string | string[],
156
- handler: HandlerValue | HandlerFn<State, Spec>,
157
- ): FinalizedAction<State, Spec>;
161
+ handler: HandlerValue | HandlerFn<State, Auth, Spec>,
162
+ ): FinalizedAction<State, Auth, Spec>;
158
163
 
159
164
  static fromHandlers<
160
165
  State extends ContextState = ContextState,
166
+ Auth extends AuthState = AuthState,
161
167
  Spec extends ActionSpec = ActionSpec,
162
168
  >(
163
169
  typeDef: TypeDef | undefined,
164
170
  spec: Spec,
165
- meta: ActionMeta<State, Spec>,
166
- handlerArgs: HandlerObj<State, Spec>,
167
- ): FinalizedAction<State, Spec>;
171
+ meta: ActionMeta<State, Auth, Spec>,
172
+ handlerArgs: HandlerObj<State, Auth, Spec>,
173
+ ): FinalizedAction<State, Auth, Spec>;
168
174
 
169
175
  static fromHandlers<
170
176
  State extends ContextState = ContextState,
177
+ Auth extends AuthState = AuthState,
171
178
  Spec extends ActionSpec = ActionSpec,
172
179
  >(
173
180
  typeDef: TypeDef | undefined,
174
181
  spec: Spec,
175
- meta: ActionMeta<State, Spec>,
176
- arg3: string | string[] | HandlerObj<State, Spec>,
177
- arg4?: HandlerValue | HandlerFn<State, Spec>,
178
- ): FinalizedAction<State, Spec> {
182
+ meta: ActionMeta<State, Auth, Spec>,
183
+ arg3: string | string[] | HandlerObj<State, Auth, Spec>,
184
+ arg4?: HandlerValue | HandlerFn<State, Auth, Spec>,
185
+ ): FinalizedAction<State, Auth, Spec> {
179
186
  if (Array.isArray(arg3) || typeof arg3 === 'string') {
180
- return new FinalizedAction<State, Spec>(typeDef, spec, meta, {
187
+ return new FinalizedAction<State, Auth, Spec>(typeDef, spec, meta, {
181
188
  contentType: arg3,
182
189
  handler: arg4,
183
190
  });
@@ -254,7 +261,7 @@ export class FinalizedAction<
254
261
  return this.#meta.registry;
255
262
  }
256
263
 
257
- get handlers(): HandlerDefinition<State, Spec>[] {
264
+ get handlers(): HandlerDefinition<State, Auth, Spec>[] {
258
265
  return Array.from(this.#handlers.values());
259
266
  }
260
267
 
@@ -274,6 +281,15 @@ export class FinalizedAction<
274
281
  return joinPaths(this.#meta.registry.rootIRI, this.#meta.path.normalized);
275
282
  }
276
283
 
284
+ /**
285
+ * Retrives the handler configured for the given content type.
286
+ *
287
+ * @param contentType The content type.
288
+ */
289
+ handlerFor(contentType: string): HandlerDefinition<State, Auth, Spec> | undefined {
290
+ return this.#handlers.get(contentType);
291
+ }
292
+
277
293
  jsonld(): Promise<JSONObject | null> {
278
294
  const scope = this.#meta.scope;
279
295
 
@@ -299,19 +315,19 @@ export class FinalizedAction<
299
315
 
300
316
  handle(
301
317
  contentType: string | string[],
302
- handler: HandlerFn<State, Spec> | HandlerValue,
303
- ): FinalizedAction<State, Spec>;
318
+ handler: HandlerFn<State, Auth, Spec> | HandlerValue,
319
+ ): FinalizedAction<State, Auth, Spec>;
304
320
 
305
321
  handle(
306
- args: HandlerObj<State, Spec>,
307
- ): FinalizedAction<State, Spec>;
322
+ args: HandlerObj<State, Auth, Spec>,
323
+ ): FinalizedAction<State, Auth, Spec>;
308
324
 
309
325
  handle(
310
- arg1: string | string[] | HandlerObj<State, Spec>,
311
- arg2?: HandlerFn<State, Spec>,
312
- ): FinalizedAction<State, Spec> {
326
+ arg1: string | string[] | HandlerObj<State, Auth, Spec>,
327
+ arg2?: HandlerFn<State, Auth, Spec>,
328
+ ): FinalizedAction<State, Auth, Spec> {
313
329
  let contentType: string | string[];
314
- let handler: HandlerFn<State, Spec> | HandlerValue;
330
+ let handler: HandlerFn<State, Auth, Spec> | HandlerValue;
315
331
  let meta: HandlerMeta;
316
332
 
317
333
  if (isHandlerObj(arg1)) {
@@ -336,7 +352,7 @@ export class FinalizedAction<
336
352
  contentType,
337
353
  handler,
338
354
  meta,
339
- this as unknown as ImplementedAction<State, Spec>,
355
+ this as unknown as ImplementedAction<State, Auth, Spec>,
340
356
  this.#meta,
341
357
  ));
342
358
  } else {
@@ -346,7 +362,7 @@ export class FinalizedAction<
346
362
  contentType[i],
347
363
  handler,
348
364
  meta,
349
- this as unknown as ImplementedAction<State, Spec>,
365
+ this as unknown as ImplementedAction<State, Auth, Spec>,
350
366
  this.#meta,
351
367
  ));
352
368
  }
@@ -355,7 +371,7 @@ export class FinalizedAction<
355
371
  return this;
356
372
  }
357
373
 
358
- async handleRequest(args: HandleRequestArgs): Promise<ResponseTypes> {
374
+ handleRequest(args: HandleRequestArgs): Promise<ResponseTypes> {
359
375
  const handler = this.#handlers.get(args.contentType as string);
360
376
 
361
377
  return this.#meta.handleRequest({
@@ -364,6 +380,10 @@ export class FinalizedAction<
364
380
  handler,
365
381
  });
366
382
  }
383
+
384
+ perform(req: Request): Promise<Response> {
385
+ return this.#meta.perform(req);
386
+ }
367
387
  }
368
388
 
369
389
  export interface Applicable<ActionType> {
@@ -372,27 +392,28 @@ export interface Applicable<ActionType> {
372
392
 
373
393
  export class DefinedAction<
374
394
  State extends ContextState = ContextState,
395
+ Auth extends AuthState = AuthState,
375
396
  Term extends string = string,
376
397
  Spec extends ActionSpec = ActionSpec,
377
398
  > implements
378
- Applicable<DefinedAction<State, Term, Spec>>,
379
- Handleable<State, Spec>,
380
- ImplementedAction<State, Spec>
399
+ Applicable<DefinedAction<State, Auth, Term, Spec>>,
400
+ Handleable<State, Auth, Spec>,
401
+ ImplementedAction<State, Auth, Spec>
381
402
  {
382
403
  #spec: Spec;
383
- #meta: ActionMeta<State, Spec>;
404
+ #meta: ActionMeta<State, Auth, Spec>;
384
405
  #typeDef?: TypeDef;
385
406
 
386
407
  constructor(
387
408
  typeDef: TypeDef | undefined,
388
409
  spec: Spec,
389
- meta: ActionMeta<State, Spec>,
410
+ meta: ActionMeta<State, Auth, Spec>,
390
411
  ) {
391
412
  this.#spec = spec ?? {} as Spec;
392
413
  this.#meta = meta;
393
414
  this.#typeDef = typeDef;
394
415
 
395
- this.#meta.action = this as unknown as ImplementedAction<State, Spec>;
416
+ this.#meta.action = this as unknown as ImplementedAction<State, Auth, Spec>;
396
417
  }
397
418
 
398
419
  get public(): boolean {
@@ -443,7 +464,7 @@ export class DefinedAction<
443
464
  return this.#meta.registry;
444
465
  }
445
466
 
446
- get handlers(): HandlerDefinition<State, Spec>[] {
467
+ get handlers(): HandlerDefinition<State, Auth, Spec>[] {
447
468
  return [];
448
469
  }
449
470
 
@@ -455,6 +476,13 @@ export class DefinedAction<
455
476
  return '';
456
477
  }
457
478
 
479
+ /**
480
+ * Retrives the handler configured for the given content type.
481
+ *
482
+ * @param contentType The content type.
483
+ */
484
+ handlerFor(_contentType: string): undefined {}
485
+
458
486
  get context(): JSONLDContext {
459
487
  return getActionContext({
460
488
  spec: this.#spec,
@@ -493,7 +521,7 @@ export class DefinedAction<
493
521
  * if validating and transforming the action payload might cause
494
522
  * auth sensitive checks to be run which might reject the request.
495
523
  */
496
- cache(args: CacheInstanceArgs): DefinedAction<State, string, Spec> {
524
+ cache(args: CacheInstanceArgs): DefinedAction<State, Auth, Term, Spec> {
497
525
  if (this.#meta.cache.length !== 0 &&
498
526
  this.#meta.cacheOccurance === BeforeDefinition) {
499
527
  throw new Error(
@@ -508,23 +536,23 @@ export class DefinedAction<
508
536
  return this;
509
537
  }
510
538
 
511
- meta(): DefinedAction<State, string, Spec> {
539
+ meta(): DefinedAction<State, Auth, Term, Spec> {
512
540
  return this;
513
541
  }
514
542
 
515
- use(): DefinedAction<State, string, Spec> {
543
+ use(): DefinedAction<State, Auth, Term, Spec> {
516
544
  return this;
517
545
  }
518
546
 
519
- handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State, Spec>): FinalizedAction<State, Spec>;
520
- handle(args: HandlerObj<State, Spec>): FinalizedAction<State, Spec>;
521
- handle(arg1: unknown, arg2?: unknown): FinalizedAction<State, Spec> {
547
+ handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State, Auth, Spec>): FinalizedAction<State, Auth, Spec>;
548
+ handle(args: HandlerObj<State, Auth, Spec>): FinalizedAction<State, Auth, Spec>;
549
+ handle(arg1: unknown, arg2?: unknown): FinalizedAction<State, Auth, Spec> {
522
550
  return FinalizedAction.fromHandlers(
523
551
  this.#typeDef,
524
552
  this.#spec,
525
553
  this.#meta,
526
554
  arg1 as string | string[],
527
- arg2 as HandlerFn<State, Spec>,
555
+ arg2 as HandlerFn<State, Auth, Spec>,
528
556
  );
529
557
  }
530
558
 
@@ -535,10 +563,15 @@ export class DefinedAction<
535
563
  });
536
564
  }
537
565
 
566
+ perform(req: Request): Promise<Response> {
567
+ return this.#meta.perform(req);
568
+ }
569
+
538
570
  }
539
571
 
540
572
  export class Action<
541
573
  State extends ContextState = ContextState,
574
+ Auth extends AuthState = AuthState,
542
575
  > implements
543
576
  Applicable<Action>,
544
577
  Handleable<State>,
@@ -623,6 +656,13 @@ export class Action<
623
656
  return '';
624
657
  }
625
658
 
659
+ /**
660
+ * Retrives the handler configured for the given content type.
661
+ *
662
+ * @param contentType The content type.
663
+ */
664
+ handlerFor(_contentType: string): undefined {}
665
+
626
666
  jsonld(): Promise<null> {
627
667
  return Promise.resolve(null);
628
668
  }
@@ -636,13 +676,14 @@ export class Action<
636
676
  }
637
677
 
638
678
  define<
679
+ Auth extends AuthState = AuthState,
639
680
  Term extends string = string,
640
681
  Spec extends ActionSpec = ActionSpec,
641
- >(args: DefineArgs<Term, Spec>): DefinedAction<State, Term, Spec> {
642
- return new DefinedAction<State, Term, Spec>(
682
+ >(args: DefineArgs<Term, Spec>): DefinedAction<State, Auth, Term, Spec> {
683
+ return new DefinedAction<State, Auth, Term, Spec>(
643
684
  args.typeDef,
644
685
  args.spec ?? {} as Spec,
645
- this.#meta as unknown as ActionMeta<State, Spec>,
686
+ this.#meta as unknown as ActionMeta<State, Auth, Spec>,
646
687
  );
647
688
  }
648
689
 
@@ -665,18 +706,23 @@ export class Action<
665
706
  });
666
707
  }
667
708
 
709
+ perform(req: Request): Promise<Response> {
710
+ return this.#meta.perform(req);
711
+ }
712
+
668
713
  }
669
714
 
670
715
  export class PreAction<
671
716
  State extends ContextState = ContextState,
717
+ Auth extends AuthState = AuthState,
672
718
  > implements
673
719
  Applicable<Action>,
674
- Handleable<State>
720
+ Handleable<State, Auth>
675
721
  {
676
- #meta: ActionMeta<State>;
722
+ #meta: ActionMeta<State, Auth>;
677
723
 
678
724
  constructor(
679
- meta: ActionMeta<State>,
725
+ meta: ActionMeta<State, Auth>,
680
726
  ) {
681
727
  this.#meta = meta;
682
728
  }
@@ -690,18 +736,18 @@ export class PreAction<
690
736
  define<
691
737
  Term extends string = string,
692
738
  Spec extends ActionSpec = ActionSpec,
693
- >(args: DefineArgs<Term, Spec>): DefinedAction<State, Term, Spec> {
694
- return new DefinedAction<State, Term, Spec>(
739
+ >(args: DefineArgs<Term, Spec>): DefinedAction<State, Auth, Term, Spec> {
740
+ return new DefinedAction<State, Auth, Term, Spec>(
695
741
  args.typeDef,
696
742
  args.spec,
697
- this.#meta as unknown as ActionMeta<State, Spec>,
743
+ this.#meta as unknown as ActionMeta<State, Auth, Spec>,
698
744
  );
699
745
  }
700
746
 
701
- handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State>): FinalizedAction<State>;
702
- handle(args: HandlerObj<State>): FinalizedAction<State>;
703
- handle(arg1: unknown, arg2?: unknown): FinalizedAction<State> {
704
- return FinalizedAction.fromHandlers(
747
+ handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State, Auth>): FinalizedAction<State, Auth>;
748
+ handle(args: HandlerObj<State>): FinalizedAction<State, Auth>;
749
+ handle(arg1: unknown, arg2?: unknown): FinalizedAction<State, Auth> {
750
+ return FinalizedAction.fromHandlers<State, Auth>(
705
751
  null,
706
752
  {},
707
753
  this.#meta,
@@ -713,29 +759,30 @@ export class PreAction<
713
759
 
714
760
  export class Endpoint<
715
761
  State extends ContextState = ContextState,
762
+ Auth extends AuthState = AuthState,
716
763
  > implements
717
764
  Applicable<Action>,
718
- Handleable<State>
765
+ Handleable<State, Auth>
719
766
  {
720
- #meta: ActionMeta<State>;
767
+ #meta: ActionMeta<State, Auth>;
721
768
 
722
769
  constructor(
723
- meta: ActionMeta<State>,
770
+ meta: ActionMeta<State, Auth>,
724
771
  ) {
725
772
  this.#meta = meta;
726
773
  }
727
774
 
728
- hint(hints: HintArgs): Endpoint<State> {
775
+ hint(hints: HintArgs): Endpoint<State, Auth> {
729
776
  this.#meta.hints.push(hints);
730
777
 
731
778
  return this;
732
779
  }
733
780
 
734
- compress(): Endpoint<State> {
781
+ compress(): Endpoint<State, Auth> {
735
782
  return this;
736
783
  }
737
784
 
738
- cache(args: CacheInstanceArgs) {
785
+ cache(args: CacheInstanceArgs): Endpoint<State, Auth> {
739
786
  this.#meta.cache.push(args);
740
787
 
741
788
  return this;
@@ -745,30 +792,30 @@ export class Endpoint<
745
792
  return this;
746
793
  }
747
794
 
748
- use(): Action<State> {
749
- return new Action<State>(this.#meta);
795
+ use(): Action<State, Auth> {
796
+ return new Action<State, Auth>(this.#meta);
750
797
  }
751
798
 
752
799
  define<
753
800
  Term extends string = string,
754
801
  Spec extends ActionSpec = ActionSpec,
755
- >(args: DefineArgs<Term, Spec>): DefinedAction<State, Term, Spec> {
756
- return new DefinedAction<State, Term, Spec>(
802
+ >(args: DefineArgs<Term, Spec>): DefinedAction<State, Auth, Term, Spec> {
803
+ return new DefinedAction<State, Auth, Term, Spec>(
757
804
  args.typeDef,
758
805
  args.spec,
759
- this.#meta as ActionMeta<State, Spec>,
806
+ this.#meta as ActionMeta<State, Auth, Spec>,
760
807
  );
761
808
  }
762
809
 
763
- handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State>): FinalizedAction<State>;
764
- handle(args: HandlerObj<State>): FinalizedAction<State>;
765
- handle(arg1: unknown, arg2?: unknown): FinalizedAction<State> {
810
+ handle(contentType: string | string[], handler: HandlerValue | HandlerFn<State>): FinalizedAction<State, Auth>;
811
+ handle(args: HandlerObj<State, Auth>): FinalizedAction<State, Auth>;
812
+ handle(arg1: unknown, arg2?: unknown): FinalizedAction<State, Auth> {
766
813
  return FinalizedAction.fromHandlers(
767
814
  undefined,
768
815
  {},
769
816
  this.#meta,
770
817
  arg1 as string | string[],
771
- arg2 as HandlerFn<State>,
818
+ arg2 as HandlerFn<State, Auth>,
772
819
  );
773
820
  }
774
821
  }
@@ -782,14 +829,27 @@ export class ActionAuth<
782
829
  this.#meta = meta;
783
830
  }
784
831
 
785
- public(): Endpoint<State> {
832
+ public<
833
+ Auth extends AuthState = AuthState,
834
+ >(authMiddleware?: AuthMiddleware<Auth>): Endpoint<State, Auth> {
835
+ if (authMiddleware != null && typeof authMiddleware !== 'function')
836
+ throw new Error('Public action given invalid auth middleware');
837
+
786
838
  this.#meta.public = true;
787
-
788
- return new Endpoint(this.#meta);
839
+ this.#meta.auth = authMiddleware;
840
+
841
+ return new Endpoint(this.#meta as ActionMeta<State, Auth>);
789
842
  }
790
843
 
791
- private(): Endpoint<State> {
792
- return new Endpoint(this.#meta);
844
+ private<
845
+ Auth extends AuthState = AuthState,
846
+ >(authMiddleware: AuthMiddleware<Auth>): Endpoint<State, Auth> {
847
+ if (typeof authMiddleware !== 'function')
848
+ throw new Error('Private action given invalid auth middleware');
849
+
850
+ this.#meta.auth = authMiddleware;
851
+
852
+ return new Endpoint(this.#meta as ActionMeta<State, Auth>);
793
853
  }
794
854
  }
795
855