@atls/nestjs-dataloader 0.0.10 → 0.0.12

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/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+
2
+
3
+ ## 0.0.11 (2025-12-31)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+
9
+ * **packages:** linter errors ([204ce22](https://github.com/atls/nestjs/commit/204ce229e375b09ffd69d93e47c08bd1d1fbea1b))
10
+ * **packages:** linter errors ([d41854b](https://github.com/atls/nestjs/commit/d41854b94d5502ebf2b9e18d22cac33f98c8fc41))
11
+ * **packages:** tests ([ae0f308](https://github.com/atls/nestjs/commit/ae0f308e695cfe39b4e2d38b6a33be4f7e5a8821))
12
+
13
+ ### Features
14
+
15
+
16
+ * **common:** bump yarn, trigger release ([#338](https://github.com/atls/nestjs/issues/338)) ([9837d48](https://github.com/atls/nestjs/commit/9837d482f75928a3ac132d0306ab6de04d8a04b9))
17
+
18
+
@@ -1 +1 @@
1
- export declare const GET_LOADER_CONTEXT_KEY: string;
1
+ export declare const GET_LOADER_CONTEXT_KEY = "GET_LOADER_CONTEXT_KEY";
@@ -12,5 +12,6 @@ export const Loader = createParamDecorator((type, context) => {
12
12
  You should provide interceptor ${DataLoaderInterceptor.name} globaly with ${APP_INTERCEPTOR}
13
13
  `);
14
14
  }
15
- return ctx[GET_LOADER_CONTEXT_KEY](type);
15
+ const getLoader = ctx[GET_LOADER_CONTEXT_KEY];
16
+ return getLoader(type);
16
17
  });
@@ -1 +1 @@
1
- export declare const OrderResultByKey: (key?: string, defaultValue?: undefined) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
1
+ export declare const OrderResultByKey: (key?: string, defaultValue?: unknown) => (target: object, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
@@ -1,13 +1,32 @@
1
+ const normalizeKey = (value) => {
2
+ if (typeof value === 'string' || typeof value === 'number') {
3
+ return String(value);
4
+ }
5
+ if (typeof value === 'symbol') {
6
+ return value.toString();
7
+ }
8
+ return undefined;
9
+ };
1
10
  export const OrderResultByKey = (key = 'id', defaultValue = undefined) => (target, propertyKey, descriptor) => {
2
11
  const original = descriptor.value;
3
12
  descriptor.value = async function (keys, ...args) {
4
13
  const method = original.bind(this);
5
14
  const result = await method(keys, ...args);
6
- const resultByKey = result.reduce((res, item) => ({
7
- ...res,
8
- [item[key]]: item,
9
- }), {});
10
- return keys.map((itemKey) => resultByKey[itemKey] || defaultValue);
15
+ const resultByKey = result.reduce((res, item) => {
16
+ const itemKey = item[key];
17
+ const normalizedKey = normalizeKey(itemKey);
18
+ if (normalizedKey !== undefined) {
19
+ res[normalizedKey] = item;
20
+ }
21
+ return res;
22
+ }, {});
23
+ return keys.map((itemKey) => {
24
+ const normalizedKey = normalizeKey(itemKey);
25
+ if (normalizedKey === undefined) {
26
+ return defaultValue;
27
+ }
28
+ return resultByKey[normalizedKey] ?? defaultValue;
29
+ });
11
30
  };
12
31
  return descriptor;
13
32
  };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export * from './decorators/index.js';
2
2
  export * from './interceptors/index.js';
3
- export * from './interfaces/index.js';
3
+ export type * from './interfaces/index.js';
package/dist/index.js CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from "./decorators/index.js";
2
2
  export * from "./interceptors/index.js";
3
- export * from "./interfaces/index.js";
@@ -1,10 +1,10 @@
1
- import { CallHandler } from '@nestjs/common';
2
- import { ExecutionContext } from '@nestjs/common';
3
- import { NestInterceptor } from '@nestjs/common';
1
+ import type { CallHandler } from '@nestjs/common';
2
+ import type { ExecutionContext } from '@nestjs/common';
3
+ import type { NestInterceptor } from '@nestjs/common';
4
+ import type { Observable } from 'rxjs';
4
5
  import { ModuleRef } from '@nestjs/core';
5
- import { Observable } from 'rxjs';
6
6
  export declare class DataLoaderInterceptor implements NestInterceptor {
7
7
  private readonly moduleRef;
8
8
  constructor(moduleRef: ModuleRef);
9
- intercept(context: ExecutionContext, next: CallHandler): Observable<any>;
9
+ intercept(context: ExecutionContext, next: CallHandler): Observable<unknown>;
10
10
  }
@@ -20,19 +20,23 @@ let DataLoaderInterceptor = class DataLoaderInterceptor {
20
20
  intercept(context, next) {
21
21
  const graphqlExecutionContext = GqlExecutionContext.create(context);
22
22
  const ctx = graphqlExecutionContext.getContext();
23
+ const loaders = ctx;
23
24
  if (ctx[GET_LOADER_CONTEXT_KEY] === undefined) {
24
25
  ctx[GET_LOADER_CONTEXT_KEY] = (type) => {
25
- if (ctx[type] === undefined) {
26
- try {
27
- ctx[type] = this.moduleRef
28
- .get(type, { strict: false })
29
- .generateDataLoader();
30
- }
31
- catch (e) {
32
- throw new InternalServerErrorException(`The loader ${type} is not provided`);
33
- }
26
+ const existing = loaders[type];
27
+ if (existing) {
28
+ return existing;
29
+ }
30
+ try {
31
+ const loader = this.moduleRef
32
+ .get(type, { strict: false })
33
+ .generateDataLoader();
34
+ loaders[type] = loader;
35
+ return loader;
36
+ }
37
+ catch {
38
+ throw new InternalServerErrorException(`The loader ${type} is not provided`);
34
39
  }
35
- return ctx[type];
36
40
  };
37
41
  }
38
42
  return next.handle();
@@ -1 +1 @@
1
- export * from './nest-dataloader.interfaces.js';
1
+ export type * from './nest-dataloader.interfaces.js';
@@ -1 +1 @@
1
- export * from "./nest-dataloader.interfaces.js";
1
+ export {};
@@ -1,4 +1,4 @@
1
- import DataLoader from 'dataloader';
1
+ import type DataLoader from 'dataloader';
2
2
  export interface NestDataLoader {
3
- generateDataLoader(): DataLoader<any, any>;
3
+ generateDataLoader: () => DataLoader<unknown, unknown>;
4
4
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atls/nestjs-dataloader",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "license": "BSD-3-Clause",
5
5
  "type": "module",
6
6
  "exports": {