@azure-net/kit 1.3.5 → 1.3.7

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.
@@ -1,4 +1,7 @@
1
1
  import { error, fail, redirect, type RequestEvent } from '@sveltejs/kit';
2
+ type NoConflict<I, D> = {
3
+ [K in keyof I]: K extends keyof D ? never : I[K];
4
+ };
2
5
  type Deps = {
3
6
  context: RequestEvent;
4
7
  utils: {
@@ -7,5 +10,6 @@ type Deps = {
7
10
  error: typeof error;
8
11
  };
9
12
  };
10
- export declare const createServerAction: <T>(factory: (args: Deps) => T) => (() => T);
13
+ export declare const createServerAction: <T, I extends Record<string, unknown> = Record<string, unknown>>(factory: (args: Deps & NoConflict<I, Deps>) => T, inject?: I) => (() => T);
14
+ export declare const createServerActionFactory: <I extends Record<string, unknown>>(inject: I) => <T>(factory: (args: Deps & NoConflict<I, Deps>) => T) => () => T;
11
15
  export {};
@@ -1,12 +1,22 @@
1
1
  import { RequestContext } from '../../../edges/context/index.js';
2
2
  import { error, fail, redirect } from '@sveltejs/kit';
3
3
  import { EnvironmentUtil } from 'azure-net-tools';
4
- export const createServerAction = (factory) => {
4
+ export const createServerAction = (factory, inject) => {
5
5
  return () => {
6
6
  if (EnvironmentUtil.isBrowser) {
7
7
  throw Error('Do not use actions on client side');
8
8
  }
9
9
  const context = RequestContext.current().event;
10
- return factory({ context, utils: { fail, redirect, error } });
10
+ const deps = {
11
+ context,
12
+ utils: { fail, redirect, error },
13
+ ...inject
14
+ };
15
+ return factory(deps);
16
+ };
17
+ };
18
+ export const createServerActionFactory = (inject) => {
19
+ return function createInjectedServerAction(factory) {
20
+ return createServerAction(factory, inject);
11
21
  };
12
22
  };
@@ -2,7 +2,6 @@ import type { HttpServiceResponse } from '../httpService/index.js';
2
2
  type DeepKeys<T> = T extends object ? {
3
3
  [K in keyof T & string]: T[K] extends object ? K | `${K}.${DeepKeys<T[K]>}` : K;
4
4
  }[keyof T & string] : never;
5
- type DeepValue<T, Path extends string> = Path extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? DeepValue<T[Key], Rest> : never : Path extends keyof T ? T[Path] : never;
6
5
  type ResponseBuilderState<TData, TMeta = unknown> = {
7
6
  data: TData;
8
7
  meta: TMeta;
@@ -15,12 +14,12 @@ export declare class ResponseBuilder<TData = unknown, TMeta = object, TWrapper =
15
14
  protected unwrapData(data: TWrapper): TData;
16
15
  mapUsing<TResource>(ResourceClass: new (data: TData) => {
17
16
  toPlainObject(): TResource;
18
- }): ResponseBuilder<TResource, TMeta, TWrapper>;
17
+ }): this;
19
18
  mapCollectionUsing<TResource>(ResourceClass: new (data: ArrayElement<TData>) => {
20
19
  toPlainObject(): TResource;
21
- }): ResponseBuilder<TResource[], TMeta, TWrapper>;
22
- extract<TPath extends DeepKeys<TData>>(path: TPath): ResponseBuilder<DeepValue<TData, TPath>, TMeta, TWrapper>;
23
- addMeta<TNewMeta extends Record<string, unknown>>(metaData: TNewMeta | ((current: TMeta) => TNewMeta)): ResponseBuilder<TData, TMeta & TNewMeta, TWrapper>;
20
+ }): this;
21
+ extract<TPath extends DeepKeys<TData>>(path: TPath): this;
22
+ addMeta<TNewMeta extends Record<string, unknown>>(metaData: TNewMeta | ((current: TMeta) => TNewMeta)): Omit<this, keyof ResponseBuilder<unknown, unknown, unknown>> & ResponseBuilder<TData, TMeta & TNewMeta, TWrapper>;
24
23
  getData(): TData;
25
24
  get(): {
26
25
  data: TData;
@@ -13,7 +13,7 @@ export class ResponseBuilder {
13
13
  }
14
14
  mapUsing(ResourceClass) {
15
15
  const resource = new ResourceClass(this.state.data);
16
- const newResponse = new ResponseBuilder(this.response);
16
+ const newResponse = new this.constructor(this.response);
17
17
  newResponse.state = {
18
18
  ...this.state,
19
19
  data: resource.toPlainObject()
@@ -25,7 +25,7 @@ export class ResponseBuilder {
25
25
  throw new Error('toCollection can only be used when data is an array');
26
26
  }
27
27
  const collection = this.state.data.map((dataElement) => new ResourceClass(dataElement).toPlainObject());
28
- const newResponse = new ResponseBuilder(this.response);
28
+ const newResponse = new this.constructor(this.response);
29
29
  newResponse.state = {
30
30
  ...this.state,
31
31
  data: collection
@@ -43,7 +43,7 @@ export class ResponseBuilder {
43
43
  throw new Error(`Path "${path}" not found in response data`);
44
44
  }
45
45
  }
46
- const newResponse = new ResponseBuilder(this.response);
46
+ const newResponse = new this.constructor(this.response);
47
47
  newResponse.state = {
48
48
  ...this.state,
49
49
  data: result
@@ -52,7 +52,7 @@ export class ResponseBuilder {
52
52
  }
53
53
  addMeta(metaData) {
54
54
  const newMeta = typeof metaData === 'function' ? metaData(this.state.meta) : metaData;
55
- const newResponse = new ResponseBuilder(this.response);
55
+ const newResponse = new this.constructor(this.response);
56
56
  newResponse.state = {
57
57
  ...this.state,
58
58
  meta: { ...this.state.meta, ...newMeta }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-net/kit",
3
- "version": "1.3.5",
3
+ "version": "1.3.7",
4
4
  "files": [
5
5
  "dist",
6
6
  "!dist/**/*.test.*",