@azure-net/kit 0.7.0 → 0.8.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.
@@ -0,0 +1,3 @@
1
+ export declare class ClassMirror<Target extends object> {
2
+ constructor(target: Target);
3
+ }
@@ -1,8 +1,6 @@
1
- export class ProxyResourceService {
2
- repository;
3
- constructor(repo) {
4
- this.repository = repo;
5
- const proto = Object.getPrototypeOf(repo);
1
+ export class ClassMirror {
2
+ constructor(target) {
3
+ const proto = Object.getPrototypeOf(target);
6
4
  for (const name of Object.getOwnPropertyNames(proto)) {
7
5
  if (name === 'constructor')
8
6
  continue;
@@ -12,7 +10,7 @@ export class ProxyResourceService {
12
10
  if (name in this)
13
11
  continue;
14
12
  Object.defineProperty(this, name, {
15
- value: descriptor.value.bind(repo),
13
+ value: descriptor.value.bind(target),
16
14
  configurable: true,
17
15
  writable: true
18
16
  });
@@ -0,0 +1 @@
1
+ export * from './ClassMirror.js';
@@ -0,0 +1 @@
1
+ export * from './ClassMirror.js';
@@ -3,6 +3,6 @@ export * from './infra/resource/index.js';
3
3
  export * from './infra/httpService/index.js';
4
4
  export * from './infra/datasource/index.js';
5
5
  export * from './infra/query/index.js';
6
- export * from './application/index.js';
6
+ export * from './classMirror/index.js';
7
7
  export * from './request/index.js';
8
8
  export * from './boundaryProvider/index.js';
@@ -3,6 +3,6 @@ export * from './infra/resource/index.js';
3
3
  export * from './infra/httpService/index.js';
4
4
  export * from './infra/datasource/index.js';
5
5
  export * from './infra/query/index.js';
6
- export * from './application/index.js';
6
+ export * from './classMirror/index.js';
7
7
  export * from './request/index.js';
8
8
  export * from './boundaryProvider/index.js';
@@ -1,10 +1,10 @@
1
- type IResource<T> = {
1
+ export type PlainObject<T> = {
2
2
  [K in keyof T as K extends string ? (T[K] extends (...args: unknown[]) => unknown ? never : K) : never]: T[K];
3
3
  };
4
- export interface IBaseResource {
5
- toPlainObject(): IResource<this>;
4
+ export interface IDTOMapper<TOutput = never> {
5
+ toPlainObject(): [TOutput] extends [never] ? PlainObject<this> : TOutput;
6
6
  }
7
- export declare class Resource implements IBaseResource {
8
- toPlainObject(): IResource<this>;
7
+ export type Collection<T> = T[];
8
+ export declare class DTOMapper<TOutput = never> implements IDTOMapper<TOutput> {
9
+ toPlainObject(): [TOutput] extends [never] ? PlainObject<this> : TOutput;
9
10
  }
10
- export {};
@@ -1,4 +1,4 @@
1
- export class Resource {
1
+ export class DTOMapper {
2
2
  toPlainObject() {
3
3
  return { ...this };
4
4
  }
@@ -7,14 +7,18 @@ type ResponseBuilderState<TData, TMeta = unknown> = {
7
7
  data: TData;
8
8
  meta: TMeta;
9
9
  };
10
+ type ArrayElement<T> = T extends readonly (infer E)[] ? E : never;
10
11
  export declare class ResponseBuilder<TData = unknown, TMeta = object, TWrapper = TData> {
11
12
  protected readonly response: HttpServiceResponse<TWrapper>;
12
13
  protected state: ResponseBuilderState<TData, TMeta>;
13
14
  constructor(response: HttpServiceResponse<TWrapper>);
14
15
  protected unwrapData(data: TWrapper): TData;
15
- toResource<TResource>(ResourceClass: new (data: TData) => {
16
+ mapUsing<TResource>(ResourceClass: new (data: TData) => {
16
17
  toPlainObject(): TResource;
17
18
  }): ResponseBuilder<TResource, TMeta, TWrapper>;
19
+ mapCollectionUsing<TResource>(ResourceClass: new (data: ArrayElement<TData>) => {
20
+ toPlainObject(): TResource;
21
+ }): ResponseBuilder<TResource[], TMeta, TWrapper>;
18
22
  extract<TPath extends DeepKeys<TData>>(path: TPath): ResponseBuilder<DeepValue<TData, TPath>, TMeta, TWrapper>;
19
23
  addMeta<TNewMeta extends Record<string, unknown>>(metaData: TNewMeta | ((current: TMeta) => TNewMeta)): ResponseBuilder<TData, TMeta & TNewMeta, TWrapper>;
20
24
  getData(): TData;
@@ -12,7 +12,7 @@ export class ResponseBuilder {
12
12
  unwrapData(data) {
13
13
  return data;
14
14
  }
15
- toResource(ResourceClass) {
15
+ mapUsing(ResourceClass) {
16
16
  const resource = new ResourceClass(this.state.data);
17
17
  const newResponse = new ResponseBuilder(this.response);
18
18
  newResponse.state = {
@@ -21,6 +21,18 @@ export class ResponseBuilder {
21
21
  };
22
22
  return newResponse;
23
23
  }
24
+ mapCollectionUsing(ResourceClass) {
25
+ if (!Array.isArray(this.state.data)) {
26
+ throw new Error('toCollection can only be used when data is an array');
27
+ }
28
+ const collection = this.state.data.map((dataElement) => new ResourceClass(dataElement).toPlainObject());
29
+ const newResponse = new ResponseBuilder(this.response);
30
+ newResponse.state = {
31
+ ...this.state,
32
+ data: collection
33
+ };
34
+ return newResponse;
35
+ }
24
36
  extract(path) {
25
37
  const keys = path.split('.');
26
38
  let result = this.state.data;
@@ -1,6 +1,17 @@
1
1
  export type EventHandler<E = Event> = (event: E) => void;
2
- export declare const preventDefault: <E extends Event = Event>(fn: EventHandler<E>) => EventHandler<E>;
3
- export declare const stopPropagation: <E extends Event = Event>(fn: EventHandler<E>) => EventHandler<E>;
4
- export declare const stopImmediatePropagation: <E extends Event = Event>(fn: EventHandler<E>) => EventHandler<E>;
5
- export declare const once: <E extends Event = Event>(fn: EventHandler<E>) => EventHandler<E>;
6
- export declare const compose: <E extends Event = Event>(...wrappers: Array<(fn: EventHandler<E>) => EventHandler<E>>) => (fn: EventHandler<E>) => EventHandler<E>;
2
+ interface EventModifier<E extends Event = Event> {
3
+ (fn: () => void): EventHandler<E>;
4
+ prevent: EventModifier<E>;
5
+ stop: EventModifier<E>;
6
+ immediate: EventModifier<E>;
7
+ once: EventModifier<E>;
8
+ preventDefault: EventModifier<E>;
9
+ stopPropagation: EventModifier<E>;
10
+ stopImmediatePropagation: EventModifier<E>;
11
+ }
12
+ export declare const prevent: EventModifier<Event>;
13
+ export declare const stop: EventModifier<Event>;
14
+ export declare const immediate: EventModifier<Event>;
15
+ export declare const once: EventModifier<Event>;
16
+ export declare const event: EventModifier<Event>;
17
+ export {};
@@ -1,22 +1,31 @@
1
- export const preventDefault = (fn) => (event) => {
2
- event.preventDefault();
3
- fn(event);
4
- };
5
- export const stopPropagation = (fn) => (event) => {
6
- event.stopPropagation();
7
- fn(event);
8
- };
9
- export const stopImmediatePropagation = (fn) => (event) => {
10
- event.stopImmediatePropagation();
11
- fn(event);
12
- };
13
- export const once = (fn) => {
14
- let called = false;
15
- return (event) => {
16
- if (!called) {
17
- called = true;
18
- fn(event);
1
+ const createEventModifier = (modifiers = new Set()) => {
2
+ return new Proxy(() => { }, {
3
+ get(_, prop) {
4
+ return createEventModifier(new Set([...modifiers, prop]));
5
+ },
6
+ apply(_, __, [fn]) {
7
+ let executed = false;
8
+ return (event) => {
9
+ if (modifiers.has('once') && executed)
10
+ return;
11
+ if (modifiers.has('prevent') || modifiers.has('preventDefault')) {
12
+ event.preventDefault();
13
+ }
14
+ if (modifiers.has('stop') || modifiers.has('stopPropagation')) {
15
+ event.stopPropagation();
16
+ }
17
+ if (modifiers.has('immediate') || modifiers.has('stopImmediatePropagation')) {
18
+ event.stopImmediatePropagation();
19
+ }
20
+ if (modifiers.has('once'))
21
+ executed = true;
22
+ fn();
23
+ };
19
24
  }
20
- };
25
+ });
21
26
  };
22
- export const compose = (...wrappers) => (fn) => wrappers.reduce((acc, wrap) => wrap(acc), fn);
27
+ export const prevent = createEventModifier(new Set(['prevent']));
28
+ export const stop = createEventModifier(new Set(['stop']));
29
+ export const immediate = createEventModifier(new Set(['immediate']));
30
+ export const once = createEventModifier(new Set(['once']));
31
+ export const event = createEventModifier();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-net/kit",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "files": [
5
5
  "dist",
6
6
  "!dist/**/*.test.*",
@@ -1,4 +0,0 @@
1
- export declare class ProxyResourceService<TRepo extends object> {
2
- protected repository: TRepo;
3
- constructor(repo: TRepo);
4
- }
@@ -1 +0,0 @@
1
- export * from './ProxyResourceService.js';
@@ -1 +0,0 @@
1
- export * from './ProxyResourceService.js';