@nestjs/common#flatten TypeScript Examples

The following examples show how to use @nestjs/common#flatten. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: bull.providers.ts    From nestjs-bullmq with MIT License 6 votes vote down vote up
export function createQueueOptionProviders(
  options: BullModuleOptions[],
): Provider[] {
  const providers = options.map((option) => {
    const optionalSharedConfigHolder = createConditionalDepHolder(
      getSharedConfigToken(option.configKey),
    );
    return [
      optionalSharedConfigHolder,
      {
        provide: getQueueOptionsToken(option.name),
        useFactory: (optionalDepHolder: IConditionalDepHolder<Bull.Queue>) => {
          return {
            ...optionalDepHolder.getDependencyRef(option.name),
            ...option,
          };
        },
        inject: [optionalSharedConfigHolder],
      },
    ];
  });
  return flatten(providers);
}
Example #2
Source File: dynamoose.module.ts    From nestjs-dynamoose with MIT License 6 votes vote down vote up
static forFeatureAsync(factories: AsyncModelFactory[] = []): DynamicModule {
    const providers = createDynamooseAsyncProviders(factories);
    const imports = factories.map((factory) => factory.imports || []);
    const uniqImports = new Set(flatten(imports));
    return {
      module: DynamooseModule,
      imports: [...uniqImports],
      providers: providers,
      exports: providers,
    };
  }
Example #3
Source File: dynamoose.providers.ts    From nestjs-dynamoose with MIT License 6 votes vote down vote up
export function createDynamooseAsyncProviders(
  modelFactories: AsyncModelFactory[] = [],
) {
  const providers = (modelFactories || []).map((model) => [
    {
      provide: getModelToken(model.name),
      useFactory: async (...args: unknown[]) => {
        const schema = await model.useFactory(...args);
        const modelInstance = dynamoose.model(
          model.name,
          schema,
          model.options,
        );
        if (model.serializers) {
          Object.entries(model.serializers).forEach(([key, value]) => {
            modelInstance.serializer.add(key, value);
          });
        }
        return modelInstance;
      },
      inject: [DYNAMOOSE_INITIALIZATION, ...(model.inject || [])],
    },
  ]);
  return flatten(providers);
}
Example #4
Source File: prepare-options.service.ts    From typegraphql-nestjs with MIT License 5 votes vote down vote up
prepareOptions<TOptions extends TypeGraphQLFeatureModuleOptions>(
    featureModuleToken: string,
    globalMiddlewares: Middleware<any>[] = [],
  ) {
    const globalResolvers = getMetadataStorage().resolverClasses.map(
      metadata => metadata.target,
    );
    const globalMiddlewareClasses = globalMiddlewares.filter(
      it => it.prototype,
    ) as Function[];

    const featureModuleOptionsArray: TOptions[] = [];
    const resolversClasses: ClassType[] = [];
    const providersMetadataMap = new Map<Function, InstanceWrapper<any>>();

    for (const module of this.modulesContainer.values()) {
      for (const provider of module.providers.values()) {
        if (
          typeof provider.name === "string" &&
          provider.name.includes(featureModuleToken)
        ) {
          featureModuleOptionsArray.push(provider.instance as TOptions);
        }
        if (globalResolvers.includes(provider.metatype)) {
          providersMetadataMap.set(provider.metatype, provider);
          resolversClasses.push(provider.metatype as ClassType);
        }
        if (globalMiddlewareClasses.includes(provider.metatype)) {
          providersMetadataMap.set(provider.metatype, provider);
        }
      }
    }

    const orphanedTypes = flatten(
      featureModuleOptionsArray.map(it => it.orphanedTypes),
    );
    const container: ContainerType = {
      get: (cls, { context }) => {
        let contextId = context[REQUEST_CONTEXT_ID];
        if (!contextId) {
          contextId = ContextIdFactory.create();
          context[REQUEST_CONTEXT_ID] = contextId;
        }
        const providerMetadata = providersMetadataMap.get(cls)!;
        if (
          providerMetadata.isDependencyTreeStatic() &&
          !providerMetadata.isTransient
        ) {
          return this.moduleRef.get(cls, { strict: false });
        }
        return this.moduleRef.resolve(cls, contextId, { strict: false });
      },
    };

    return {
      resolversClasses,
      orphanedTypes,
      container,
      featureModuleOptionsArray,
    };
  }