mm-share-lib 0.0.4 → 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.
- package/package.json +12 -2
- package/src/auth/apple/apple.module.ts +10 -0
- package/src/auth/apple/apple.service.ts +28 -0
- package/src/auth/apple/dto/apple-login.dto.ts +16 -0
- package/src/auth/apple/dto/index.ts +1 -0
- package/src/auth/apple/index.ts +3 -0
- package/src/auth/config/apple.config.ts +18 -0
- package/src/auth/config/auth.config.ts +29 -0
- package/src/auth/config/facebook.config.ts +23 -0
- package/src/auth/config/file.config.ts +50 -0
- package/src/auth/config/google.config.ts +23 -0
- package/src/auth/config/index.ts +6 -0
- package/src/auth/config/twitter.config.ts +22 -0
- package/src/auth/facebook/dto/facebook-login.dto.ts +8 -0
- package/src/auth/facebook/dto/index.ts +1 -0
- package/src/auth/facebook/facebook.module.ts +11 -0
- package/src/auth/facebook/facebook.service.ts +51 -0
- package/src/auth/facebook/index.ts +4 -0
- package/src/auth/facebook/interface/facebook.interface.ts +6 -0
- package/src/auth/facebook/interface/index.ts +1 -0
- package/src/auth/google/dto/google-login.dto.ts +8 -0
- package/src/auth/google/dto/index.ts +1 -0
- package/src/auth/google/google.module.ts +10 -0
- package/src/auth/google/google.service.ts +51 -0
- package/src/auth/google/index.ts +3 -0
- package/src/auth/index.ts +5 -0
- package/src/auth/twitter/dto/index.ts +1 -0
- package/src/auth/twitter/dto/twitter-login.dto.ts +12 -0
- package/src/auth/twitter/index.ts +3 -0
- package/src/auth/twitter/twitter.module.ts +11 -0
- package/src/auth/twitter/twitter.service.ts +43 -0
- package/src/config/index.ts +1 -0
- package/src/config/type.config.ts +88 -0
- package/src/dto/pagination.dto.ts +14 -2
- package/src/exception/bad-request-error.exception.spec.ts +24 -0
- package/src/exception/bad-request-error.exception.ts +8 -0
- package/src/exception/base.exception.ts +36 -0
- package/src/exception/conflict-error.exception.spec.ts +23 -0
- package/src/exception/conflict-error.exception.ts +8 -0
- package/src/exception/forbidden-error.exception.spec.ts +23 -0
- package/src/exception/forbidden-error.exception.ts +8 -0
- package/src/exception/index.ts +7 -0
- package/src/exception/internal-server-error.exception.spec.ts +23 -0
- package/src/exception/internal-server-error.exception.ts +12 -0
- package/src/exception/not-found-error.exception.spec.ts +23 -0
- package/src/exception/not-found-error.exception.ts +8 -0
- package/src/exception/unauthorized-error.exception.spec.ts +23 -0
- package/src/exception/unauthorized-error.exception.ts +12 -0
- package/src/filter/http-exception.filter.ts +23 -0
- package/src/filter/index.ts +1 -0
- package/src/filter/rpc-exception.filter.ts +15 -0
- package/src/index.ts +3 -0
- package/src/interface/index.ts +1 -0
- package/src/interface/social.interface.ts +6 -0
- package/src/lib/index.ts +2 -0
- package/src/lib/mailer/config/index.ts +1 -0
- package/src/lib/mailer/config/mailer.config.ts +62 -0
- package/src/lib/mailer/index.ts +2 -0
- package/src/lib/mailer/mailer.module.ts +8 -0
- package/src/lib/mailer/mailer.service.ts +53 -0
- package/src/lib/redis/index.ts +0 -0
- package/src/lib/search-engine/document/base.document.ts +2 -2
- package/src/lib/search-engine/interface/search-document.interface.ts +10 -2
- package/src/lib/search-engine/schema/generic.schema.ts +1 -1
- package/src/lib/search-engine/schema/index.ts +1 -1
- package/src/lib/search-engine/typesense/collection/index.ts +1 -0
- package/src/lib/search-engine/typesense/document/index.ts +1 -0
- package/src/lib/search-engine/typesense/index.ts +1 -0
- package/src/lib/search-engine/typesense/service/client.service.ts +5 -3
- package/src/lib/storage/index.ts +0 -0
- package/src/lib/typesense/client/index.ts +1 -0
- package/src/lib/typesense/collection/index.ts +1 -0
- package/src/lib/typesense/decorator/field.decorator.spec.ts +89 -0
- package/src/lib/typesense/decorator/field.decorator.ts +44 -0
- package/src/lib/typesense/decorator/index.ts +2 -0
- package/src/lib/typesense/decorator/schema.decorator.ts +23 -0
- package/src/lib/typesense/document/base.document.ts +3 -0
- package/src/lib/typesense/index.ts +6 -0
- package/src/lib/typesense/metadata/index.ts +4 -0
- package/src/lib/typesense/metadata/schema.metadata.ts +21 -0
- package/src/lib/typesense/metadata/typesense.metadata-accessor.ts +37 -0
- package/src/lib/typesense/metadata/typesense.metadata-explorer.spec.ts +56 -0
- package/src/lib/typesense/metadata/typesense.metadata-explorer.ts +41 -0
- package/src/lib/typesense/metadata/typesense.metadata-registry.ts +28 -0
- package/src/lib/typesense/module/index.ts +4 -0
- package/src/lib/typesense/module/interface.module.ts +34 -0
- package/src/lib/typesense/module/typesense.constant.ts +1 -0
- package/src/lib/typesense/module/typesense.module.spec.ts +94 -0
- package/src/lib/typesense/module/typesense.module.ts +87 -0
- package/src/lib/typesense/module/typesense.provider.ts +54 -0
- package/src/response/base-entity.response.ts +8 -0
- package/src/response/index.ts +1 -0
- package/src/response/login.response.ts +0 -0
- package/src/response/pagination.response.ts +23 -8
- package/src/setup/index.ts +1 -0
- package/src/setup/microservice.setup.ts +29 -0
- package/src/setup/swagger.setup.ts +0 -0
- package/src/util/app.utils.ts +18 -0
- package/src/util/index.ts +2 -0
- package/src/util/validate-config.ts +20 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -18
- package/dist/index.js.map +0 -1
- package/dist/src/dto/base-filter.dto.d.ts +0 -4
- package/dist/src/dto/base-filter.dto.js +0 -11
- package/dist/src/dto/base-filter.dto.js.map +0 -1
- package/dist/src/dto/index.d.ts +0 -2
- package/dist/src/dto/index.js +0 -19
- package/dist/src/dto/index.js.map +0 -1
- package/dist/src/dto/pagination.dto.d.ts +0 -4
- package/dist/src/dto/pagination.dto.js +0 -11
- package/dist/src/dto/pagination.dto.js.map +0 -1
- package/dist/src/index.d.ts +0 -6
- package/dist/src/index.js +0 -23
- package/dist/src/index.js.map +0 -1
- package/dist/src/lib/index.d.ts +0 -1
- package/dist/src/lib/index.js +0 -18
- package/dist/src/lib/index.js.map +0 -1
- package/dist/src/lib/search-engine/document/base.document.d.ts +0 -3
- package/dist/src/lib/search-engine/document/base.document.js +0 -7
- package/dist/src/lib/search-engine/document/base.document.js.map +0 -1
- package/dist/src/lib/search-engine/document/index.js +0 -18
- package/dist/src/lib/search-engine/document/index.js.map +0 -1
- package/dist/src/lib/search-engine/index.d.ts +0 -1
- package/dist/src/lib/search-engine/index.js +0 -18
- package/dist/src/lib/search-engine/index.js.map +0 -1
- package/dist/src/lib/search-engine/interface/index.d.ts +0 -2
- package/dist/src/lib/search-engine/interface/index.js +0 -19
- package/dist/src/lib/search-engine/interface/index.js.map +0 -1
- package/dist/src/lib/search-engine/interface/search-document.interface.d.ts +0 -4
- package/dist/src/lib/search-engine/interface/search-document.interface.js +0 -3
- package/dist/src/lib/search-engine/interface/search-document.interface.js.map +0 -1
- package/dist/src/lib/search-engine/interface/transform-service.interface.d.ts +0 -6
- package/dist/src/lib/search-engine/interface/transform-service.interface.js +0 -3
- package/dist/src/lib/search-engine/interface/transform-service.interface.js.map +0 -1
- package/dist/src/lib/search-engine/schema/generic.schema.d.ts +0 -2
- package/dist/src/lib/search-engine/schema/generic.schema.js +0 -7
- package/dist/src/lib/search-engine/schema/generic.schema.js.map +0 -1
- package/dist/src/lib/search-engine/schema/index.d.ts +0 -1
- package/dist/src/lib/search-engine/schema/index.js +0 -18
- package/dist/src/lib/search-engine/schema/index.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/index.d.ts +0 -3
- package/dist/src/lib/search-engine/typesense/index.js +0 -20
- package/dist/src/lib/search-engine/typesense/index.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/metadata/index.d.ts +0 -2
- package/dist/src/lib/search-engine/typesense/metadata/index.js +0 -19
- package/dist/src/lib/search-engine/typesense/metadata/index.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/metadata/schema.metadata.d.ts +0 -12
- package/dist/src/lib/search-engine/typesense/metadata/schema.metadata.js +0 -3
- package/dist/src/lib/search-engine/typesense/metadata/schema.metadata.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/metadata/typesense.metadata-registry.d.ts +0 -10
- package/dist/src/lib/search-engine/typesense/metadata/typesense.metadata-registry.js +0 -33
- package/dist/src/lib/search-engine/typesense/metadata/typesense.metadata-registry.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/service/client.service.d.ts +0 -33
- package/dist/src/lib/search-engine/typesense/service/client.service.js +0 -197
- package/dist/src/lib/search-engine/typesense/service/client.service.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/service/index.d.ts +0 -1
- package/dist/src/lib/search-engine/typesense/service/index.js +0 -18
- package/dist/src/lib/search-engine/typesense/service/index.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/typesense-module.interface.d.ts +0 -27
- package/dist/src/lib/search-engine/typesense/typesense-module.interface.js +0 -3
- package/dist/src/lib/search-engine/typesense/typesense-module.interface.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.constants.d.ts +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.constants.js +0 -5
- package/dist/src/lib/search-engine/typesense/typesense.constants.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.module.d.ts +0 -8
- package/dist/src/lib/search-engine/typesense/typesense.module.js +0 -68
- package/dist/src/lib/search-engine/typesense/typesense.module.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.module.test.d.ts +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.module.test.js +0 -89
- package/dist/src/lib/search-engine/typesense/typesense.module.test.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.providers.d.ts +0 -4
- package/dist/src/lib/search-engine/typesense/typesense.providers.js +0 -40
- package/dist/src/lib/search-engine/typesense/typesense.providers.js.map +0 -1
- package/dist/src/response/index.d.ts +0 -1
- package/dist/src/response/index.js +0 -18
- package/dist/src/response/index.js.map +0 -1
- package/dist/src/response/pagination.response.d.ts +0 -17
- package/dist/src/response/pagination.response.js +0 -29
- package/dist/src/response/pagination.response.js.map +0 -1
- package/dist/src/util/date.util.d.ts +0 -2
- package/dist/src/util/date.util.js +0 -14
- package/dist/src/util/date.util.js.map +0 -1
- package/dist/src/util/generator.util.d.ts +0 -4
- package/dist/src/util/generator.util.js +0 -29
- package/dist/src/util/generator.util.js.map +0 -1
- package/dist/src/util/index.d.ts +0 -2
- package/dist/src/util/index.js +0 -19
- package/dist/src/util/index.js.map +0 -1
- package/dist/tsconfig.build.tsbuildinfo +0 -1
- /package/{dist/src/lib/search-engine/document/index.d.ts → src/lib/typesense/document/index.ts} +0 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
import { SetMetadata } from '@nestjs/common';
|
2
|
+
import { applyDecorators } from '@nestjs/common';
|
3
|
+
|
4
|
+
export interface FieldMetadata {
|
5
|
+
name?: string;
|
6
|
+
facet?: boolean;
|
7
|
+
index?: boolean;
|
8
|
+
optional?: boolean;
|
9
|
+
}
|
10
|
+
|
11
|
+
export type FieldType =
|
12
|
+
| 'string'
|
13
|
+
| 'int32'
|
14
|
+
| 'int64'
|
15
|
+
| 'float'
|
16
|
+
| 'bool'
|
17
|
+
| 'geopoint'
|
18
|
+
| 'geopoint[]'
|
19
|
+
| 'string[]'
|
20
|
+
| 'int32[]'
|
21
|
+
| 'int64[]'
|
22
|
+
| 'float[]'
|
23
|
+
| 'bool[]'
|
24
|
+
| 'object'
|
25
|
+
| 'object[]'
|
26
|
+
| 'auto'
|
27
|
+
| 'string*';
|
28
|
+
|
29
|
+
export const FIELD_METADATA = '__fieldMetadata__';
|
30
|
+
|
31
|
+
export const Field = (type: FieldType, options: FieldMetadata = {}) =>
|
32
|
+
applyDecorators((target: object, key?: any, descriptor?: any) => {
|
33
|
+
const exists =
|
34
|
+
Reflect.getMetadata(FIELD_METADATA, target.constructor) || [];
|
35
|
+
|
36
|
+
return SetMetadata(FIELD_METADATA, [
|
37
|
+
...exists,
|
38
|
+
{
|
39
|
+
...options,
|
40
|
+
type,
|
41
|
+
name: options.name || key,
|
42
|
+
},
|
43
|
+
])(target.constructor, key, descriptor);
|
44
|
+
});
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import decamelize from 'decamelize';
|
2
|
+
import { SetMetadata, applyDecorators } from '@nestjs/common';
|
3
|
+
|
4
|
+
export interface Schema {
|
5
|
+
name?: string;
|
6
|
+
defaultSortingField?: string;
|
7
|
+
auto?: boolean;
|
8
|
+
}
|
9
|
+
|
10
|
+
export const SCHEMA_METADATA = '__schemaMetadata__';
|
11
|
+
|
12
|
+
export const Schema = (options: Schema = {}): ClassDecorator =>
|
13
|
+
applyDecorators((target: object, key?: any, descriptor?: any) =>
|
14
|
+
SetMetadata(SCHEMA_METADATA, {
|
15
|
+
name:
|
16
|
+
options.name ||
|
17
|
+
decamelize((target as any).name, {
|
18
|
+
separator: '-',
|
19
|
+
preserveConsecutiveUppercase: false,
|
20
|
+
}),
|
21
|
+
defaultSortingField: options.defaultSortingField,
|
22
|
+
})(target, key, descriptor),
|
23
|
+
);
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import type { FieldType } from '../decorator/field.decorator';
|
2
|
+
|
3
|
+
export interface SchemaField {
|
4
|
+
name: string;
|
5
|
+
type: FieldType;
|
6
|
+
optional?: boolean;
|
7
|
+
facet?: boolean;
|
8
|
+
index?: boolean;
|
9
|
+
sort?: boolean;
|
10
|
+
locale?: string;
|
11
|
+
infix?: boolean;
|
12
|
+
num_dim?: number;
|
13
|
+
[t: string]: unknown;
|
14
|
+
}
|
15
|
+
|
16
|
+
export interface Schema {
|
17
|
+
name: string;
|
18
|
+
fields: Array<SchemaField>;
|
19
|
+
enable_nested_fields?: boolean;
|
20
|
+
defaultSortingField?: string;
|
21
|
+
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
2
|
+
import { Reflector } from '@nestjs/core';
|
3
|
+
|
4
|
+
import { SCHEMA_METADATA } from '../decorator';
|
5
|
+
import { FIELD_METADATA } from '../decorator';
|
6
|
+
import { Schema } from './schema.metadata';
|
7
|
+
|
8
|
+
@Injectable()
|
9
|
+
export class TypesenseMetadataAccessor {
|
10
|
+
constructor(private readonly reflector: Reflector) {}
|
11
|
+
|
12
|
+
getTypesenseMetadata(target): Schema | undefined {
|
13
|
+
if (target.constructor) {
|
14
|
+
const schema = this.reflector.get(SCHEMA_METADATA, target.constructor);
|
15
|
+
const fields = this.reflector.get(FIELD_METADATA, target.constructor);
|
16
|
+
|
17
|
+
if (!schema) {
|
18
|
+
return undefined;
|
19
|
+
}
|
20
|
+
|
21
|
+
if (!(fields || schema.auto)) {
|
22
|
+
return undefined;
|
23
|
+
}
|
24
|
+
|
25
|
+
return {
|
26
|
+
name: schema.name,
|
27
|
+
defaultSortingField: schema.defaultSortingField,
|
28
|
+
fields: [
|
29
|
+
...(schema.auto ? [{ name: '.*', type: 'auto' }] : []),
|
30
|
+
...(fields || []),
|
31
|
+
],
|
32
|
+
};
|
33
|
+
}
|
34
|
+
|
35
|
+
return undefined;
|
36
|
+
}
|
37
|
+
}
|
@@ -0,0 +1,56 @@
|
|
1
|
+
/* eslint-disable max-classes-per-file */
|
2
|
+
|
3
|
+
import 'reflect-metadata';
|
4
|
+
|
5
|
+
import { Module } from '@nestjs/common';
|
6
|
+
import { DiscoveryModule } from '@nestjs/core';
|
7
|
+
import { Test } from '@nestjs/testing';
|
8
|
+
|
9
|
+
import { Schema } from '../decorators';
|
10
|
+
import { Field } from '../decorators';
|
11
|
+
import { TypesenseMetadataAccessor } from './typesense.metadata-accessor';
|
12
|
+
import { TypesenseMetadataExplorer } from './typesense.metadata-explorer';
|
13
|
+
import { TypesenseMetadataRegistry } from './typesense.metadata-registry';
|
14
|
+
|
15
|
+
describe('typesense', () => {
|
16
|
+
describe('metadata', () => {
|
17
|
+
describe('explorer', () => {
|
18
|
+
let module;
|
19
|
+
|
20
|
+
@Module({
|
21
|
+
imports: [DiscoveryModule],
|
22
|
+
providers: [
|
23
|
+
TypesenseMetadataAccessor,
|
24
|
+
TypesenseMetadataExplorer,
|
25
|
+
TypesenseMetadataRegistry,
|
26
|
+
],
|
27
|
+
})
|
28
|
+
class TestMetadataModule {}
|
29
|
+
|
30
|
+
@Schema()
|
31
|
+
class TestSchema {
|
32
|
+
@Field('string')
|
33
|
+
field!: string;
|
34
|
+
}
|
35
|
+
|
36
|
+
beforeEach(async () => {
|
37
|
+
module = await Test.createTestingModule({
|
38
|
+
imports: [TestMetadataModule],
|
39
|
+
providers: [TestSchema],
|
40
|
+
}).compile();
|
41
|
+
|
42
|
+
await module.init();
|
43
|
+
});
|
44
|
+
|
45
|
+
afterEach(async () => {
|
46
|
+
await module.close();
|
47
|
+
});
|
48
|
+
|
49
|
+
it('should store schema metadata', () => {
|
50
|
+
expect(
|
51
|
+
module.get(TypesenseMetadataRegistry).getSchemaByTarget(TestSchema),
|
52
|
+
).toBeDefined();
|
53
|
+
});
|
54
|
+
});
|
55
|
+
});
|
56
|
+
});
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
2
|
+
import { DiscoveryService } from '@nestjs/core';
|
3
|
+
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
4
|
+
|
5
|
+
import { TypesenseMetadataAccessor } from './typesense.metadata-accessor';
|
6
|
+
import { TypesenseMetadataRegistry } from './typesense.metadata-registry';
|
7
|
+
|
8
|
+
@Injectable()
|
9
|
+
export class TypesenseMetadataExplorer implements OnModuleInit {
|
10
|
+
private readonly logger = new Logger(TypesenseMetadataExplorer.name);
|
11
|
+
|
12
|
+
constructor(
|
13
|
+
private readonly discoveryService: DiscoveryService,
|
14
|
+
private readonly metadataAccessor: TypesenseMetadataAccessor,
|
15
|
+
private readonly metadataRegistry: TypesenseMetadataRegistry,
|
16
|
+
) {}
|
17
|
+
|
18
|
+
onModuleInit() {
|
19
|
+
this.explore();
|
20
|
+
}
|
21
|
+
|
22
|
+
explore() {
|
23
|
+
this.discoveryService.getProviders().forEach((wrapper: InstanceWrapper) => {
|
24
|
+
const { instance } = wrapper;
|
25
|
+
|
26
|
+
if (!instance || !Object.getPrototypeOf(instance)) {
|
27
|
+
return;
|
28
|
+
}
|
29
|
+
|
30
|
+
this.lookupSchema(instance);
|
31
|
+
});
|
32
|
+
}
|
33
|
+
|
34
|
+
lookupSchema(instance) {
|
35
|
+
const metadata = this.metadataAccessor.getTypesenseMetadata(instance);
|
36
|
+
|
37
|
+
if (metadata) {
|
38
|
+
this.metadataRegistry.addSchema(instance.constructor, metadata);
|
39
|
+
}
|
40
|
+
}
|
41
|
+
}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import { Injectable, Logger } from '@nestjs/common';
|
2
|
+
|
3
|
+
import { Schema } from './schema.metadata';
|
4
|
+
|
5
|
+
type Constructor = new (...args: any[]) => unknown;
|
6
|
+
|
7
|
+
@Injectable()
|
8
|
+
export class TypesenseMetadataRegistry {
|
9
|
+
private logger = new Logger(TypesenseMetadataRegistry.name);
|
10
|
+
|
11
|
+
private schemas: Map<Constructor, Schema> = new Map();
|
12
|
+
|
13
|
+
addSchema(target: Constructor, schema: Schema) {
|
14
|
+
if (this.schemas.has(target)) {
|
15
|
+
this.logger.warn(`Schema ${target} already exists`);
|
16
|
+
}
|
17
|
+
|
18
|
+
this.schemas.set(target, schema);
|
19
|
+
}
|
20
|
+
|
21
|
+
getSchemaByTarget(target: Constructor) {
|
22
|
+
return this.schemas.get(target);
|
23
|
+
}
|
24
|
+
|
25
|
+
getTargets() {
|
26
|
+
return this.schemas.keys();
|
27
|
+
}
|
28
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { ModuleMetadata } from '@nestjs/common/interfaces';
|
2
|
+
import { Type } from '@nestjs/common/interfaces';
|
3
|
+
|
4
|
+
export interface TypesenseNodeOptions {
|
5
|
+
host: string;
|
6
|
+
port: string;
|
7
|
+
protocol: string;
|
8
|
+
}
|
9
|
+
|
10
|
+
export interface TypesenseModuleOptions {
|
11
|
+
nodes?: Array<TypesenseNodeOptions>;
|
12
|
+
numRetries?: number;
|
13
|
+
apiKey?: string;
|
14
|
+
connectionTimeoutSeconds?: number;
|
15
|
+
retryIntervalSeconds?: number;
|
16
|
+
healthcheckIntervalSeconds?: number;
|
17
|
+
logLevel?: string;
|
18
|
+
}
|
19
|
+
|
20
|
+
export interface TypesenseOptionsFactory {
|
21
|
+
createTypesenseOptions():
|
22
|
+
| Promise<TypesenseModuleOptions>
|
23
|
+
| TypesenseModuleOptions;
|
24
|
+
}
|
25
|
+
|
26
|
+
export interface TypesenseModuleAsyncOptions
|
27
|
+
extends Pick<ModuleMetadata, 'imports'> {
|
28
|
+
useExisting?: Type<TypesenseOptionsFactory>;
|
29
|
+
useClass?: Type<TypesenseOptionsFactory>;
|
30
|
+
useFactory?: (
|
31
|
+
...args: any[]
|
32
|
+
) => Promise<TypesenseModuleOptions> | TypesenseModuleOptions;
|
33
|
+
inject?: any[];
|
34
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export const TYPESENSE_MODULE_OPTIONS = 'TYPESENSE_MODULE_OPTIONS';
|
@@ -0,0 +1,94 @@
|
|
1
|
+
/* eslint-disable max-classes-per-file */
|
2
|
+
|
3
|
+
import { Module } from '@nestjs/common';
|
4
|
+
import { Test } from '@nestjs/testing';
|
5
|
+
|
6
|
+
import { TypesenseModuleOptions } from './interface.module';
|
7
|
+
import { TYPESENSE_MODULE_OPTIONS } from './typesense.constant';
|
8
|
+
import { TypesenseModule } from './';
|
9
|
+
|
10
|
+
describe('typesense', () => {
|
11
|
+
describe('module', () => {
|
12
|
+
let module;
|
13
|
+
|
14
|
+
afterEach(async () => {
|
15
|
+
await module.close();
|
16
|
+
});
|
17
|
+
|
18
|
+
it(`register`, async () => {
|
19
|
+
module = await Test.createTestingModule({
|
20
|
+
imports: [
|
21
|
+
TypesenseModule.register({
|
22
|
+
apiKey: 'test',
|
23
|
+
}),
|
24
|
+
],
|
25
|
+
}).compile();
|
26
|
+
|
27
|
+
expect(module.get(TYPESENSE_MODULE_OPTIONS)).toBeDefined();
|
28
|
+
});
|
29
|
+
|
30
|
+
it(`register async use factory`, async () => {
|
31
|
+
module = await Test.createTestingModule({
|
32
|
+
imports: [
|
33
|
+
TypesenseModule.registerAsync({
|
34
|
+
useFactory: () => ({
|
35
|
+
apiKey: 'test',
|
36
|
+
}),
|
37
|
+
}),
|
38
|
+
],
|
39
|
+
}).compile();
|
40
|
+
|
41
|
+
expect(module.get(TYPESENSE_MODULE_OPTIONS)).toBeDefined();
|
42
|
+
});
|
43
|
+
|
44
|
+
it(`register async use class`, async () => {
|
45
|
+
class TestTypesenseModuleOptions {
|
46
|
+
createTypesenseOptions(): TypesenseModuleOptions {
|
47
|
+
return {
|
48
|
+
apiKey: 'test',
|
49
|
+
};
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
module = await Test.createTestingModule({
|
54
|
+
imports: [
|
55
|
+
TypesenseModule.registerAsync({
|
56
|
+
useClass: TestTypesenseModuleOptions,
|
57
|
+
}),
|
58
|
+
],
|
59
|
+
}).compile();
|
60
|
+
|
61
|
+
expect(module.get(TYPESENSE_MODULE_OPTIONS)).toBeDefined();
|
62
|
+
});
|
63
|
+
|
64
|
+
it(`register async use exists`, async () => {
|
65
|
+
class TestTypesenseModuleOptions {
|
66
|
+
createTypesenseOptions(): TypesenseModuleOptions {
|
67
|
+
return {
|
68
|
+
apiKey: 'test',
|
69
|
+
};
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
@Module({})
|
74
|
+
class TestTypesenseModule {}
|
75
|
+
|
76
|
+
module = await Test.createTestingModule({
|
77
|
+
imports: [
|
78
|
+
TypesenseModule.registerAsync({
|
79
|
+
imports: [
|
80
|
+
{
|
81
|
+
module: TestTypesenseModule,
|
82
|
+
providers: [TestTypesenseModuleOptions],
|
83
|
+
exports: [TestTypesenseModuleOptions],
|
84
|
+
},
|
85
|
+
],
|
86
|
+
useExisting: TestTypesenseModuleOptions,
|
87
|
+
}),
|
88
|
+
],
|
89
|
+
}).compile();
|
90
|
+
|
91
|
+
expect(module.get(TYPESENSE_MODULE_OPTIONS)).toBeDefined();
|
92
|
+
});
|
93
|
+
});
|
94
|
+
});
|
@@ -0,0 +1,87 @@
|
|
1
|
+
import { DynamicModule, Provider, Module } from '@nestjs/common';
|
2
|
+
import { DiscoveryModule } from '@nestjs/core';
|
3
|
+
|
4
|
+
import {
|
5
|
+
TypesenseModuleAsyncOptions,
|
6
|
+
TypesenseOptionsFactory,
|
7
|
+
TypesenseModuleOptions,
|
8
|
+
} from './interface.module';
|
9
|
+
import { TYPESENSE_MODULE_OPTIONS } from './typesense.constant';
|
10
|
+
import {
|
11
|
+
createTypesenseExportsProvider,
|
12
|
+
createTypesenseProvider,
|
13
|
+
createTypesenseOptionsProvider,
|
14
|
+
} from './typesense.provider';
|
15
|
+
|
16
|
+
@Module({
|
17
|
+
imports: [DiscoveryModule],
|
18
|
+
})
|
19
|
+
export class TypesenseModule {
|
20
|
+
static register(options: TypesenseModuleOptions = {}): DynamicModule {
|
21
|
+
const optionsProviders = createTypesenseOptionsProvider(options);
|
22
|
+
const exportsProviders = createTypesenseExportsProvider();
|
23
|
+
const providers = createTypesenseProvider();
|
24
|
+
|
25
|
+
return {
|
26
|
+
global: true,
|
27
|
+
module: TypesenseModule,
|
28
|
+
providers: [...optionsProviders, ...providers, ...exportsProviders],
|
29
|
+
exports: exportsProviders,
|
30
|
+
};
|
31
|
+
}
|
32
|
+
|
33
|
+
static registerAsync(options: TypesenseModuleAsyncOptions): DynamicModule {
|
34
|
+
const exportsProviders = createTypesenseExportsProvider();
|
35
|
+
const providers = createTypesenseProvider();
|
36
|
+
|
37
|
+
return {
|
38
|
+
global: true,
|
39
|
+
module: TypesenseModule,
|
40
|
+
imports: options.imports || [],
|
41
|
+
providers: [
|
42
|
+
...this.createAsyncProviders(options),
|
43
|
+
...providers,
|
44
|
+
...exportsProviders,
|
45
|
+
],
|
46
|
+
exports: exportsProviders,
|
47
|
+
};
|
48
|
+
}
|
49
|
+
|
50
|
+
private static createAsyncProviders(
|
51
|
+
options: TypesenseModuleAsyncOptions,
|
52
|
+
): Provider[] {
|
53
|
+
if (options.useExisting || options.useFactory) {
|
54
|
+
return [this.createAsyncOptionsProvider(options)];
|
55
|
+
}
|
56
|
+
|
57
|
+
return [
|
58
|
+
this.createAsyncOptionsProvider(options),
|
59
|
+
{
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
61
|
+
provide: options.useClass!,
|
62
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
63
|
+
useClass: options.useClass!,
|
64
|
+
},
|
65
|
+
];
|
66
|
+
}
|
67
|
+
|
68
|
+
private static createAsyncOptionsProvider(
|
69
|
+
options: TypesenseModuleAsyncOptions,
|
70
|
+
): Provider {
|
71
|
+
if (options.useFactory) {
|
72
|
+
return {
|
73
|
+
provide: TYPESENSE_MODULE_OPTIONS,
|
74
|
+
useFactory: options.useFactory,
|
75
|
+
inject: options.inject || [],
|
76
|
+
};
|
77
|
+
}
|
78
|
+
|
79
|
+
return {
|
80
|
+
provide: TYPESENSE_MODULE_OPTIONS,
|
81
|
+
useFactory: (optionsFactory: TypesenseOptionsFactory) =>
|
82
|
+
optionsFactory.createTypesenseOptions(),
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
84
|
+
inject: [options.useExisting! || options.useClass!],
|
85
|
+
};
|
86
|
+
}
|
87
|
+
}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import { Provider } from '@nestjs/common';
|
2
|
+
|
3
|
+
import { Client } from 'typesense';
|
4
|
+
|
5
|
+
import { TypesenseCollectionsCreator } from '../collections';
|
6
|
+
import { TypesenseMetadataAccessor } from '../metadata';
|
7
|
+
import { TypesenseMetadataExplorer } from '../metadata';
|
8
|
+
import { TypesenseMetadataRegistry } from '../metadata';
|
9
|
+
import { TypesenseModuleOptions } from './typesense-module.interface';
|
10
|
+
import { TYPESENSE_MODULE_OPTIONS } from './typesense.constants';
|
11
|
+
|
12
|
+
export const createTypesenseOptionsProvider = (
|
13
|
+
options: TypesenseModuleOptions = {},
|
14
|
+
): Provider[] => [
|
15
|
+
{
|
16
|
+
provide: TYPESENSE_MODULE_OPTIONS,
|
17
|
+
useValue: options,
|
18
|
+
},
|
19
|
+
];
|
20
|
+
|
21
|
+
export const createTypesenseProvider = (): Provider[] => [
|
22
|
+
TypesenseMetadataAccessor,
|
23
|
+
TypesenseMetadataExplorer,
|
24
|
+
TypesenseMetadataRegistry,
|
25
|
+
TypesenseCollectionsCreator,
|
26
|
+
];
|
27
|
+
|
28
|
+
export const createTypesenseExportsProvider = (): Provider[] => [
|
29
|
+
TypesenseMetadataRegistry,
|
30
|
+
{
|
31
|
+
provide: Client,
|
32
|
+
useFactory: (options: TypesenseModuleOptions) =>
|
33
|
+
new Client({
|
34
|
+
nodes: options.nodes || [
|
35
|
+
{
|
36
|
+
host:
|
37
|
+
process.env.TYPESENSE_HOST ||
|
38
|
+
process.env.NODE_ENV === 'production'
|
39
|
+
? 'ts.typesense.svc.cluster.local'
|
40
|
+
: 'localhost',
|
41
|
+
port: 8108,
|
42
|
+
protocol: 'http',
|
43
|
+
},
|
44
|
+
],
|
45
|
+
numRetries: options.numRetries || 10,
|
46
|
+
apiKey: options.apiKey || process.env.TYPESENSE_API_KEY,
|
47
|
+
connectionTimeoutSeconds: options.connectionTimeoutSeconds || 10,
|
48
|
+
retryIntervalSeconds: options.retryIntervalSeconds || 0.1,
|
49
|
+
healthcheckIntervalSeconds: options.healthcheckIntervalSeconds || 2,
|
50
|
+
logLevel: options.logLevel || 'info',
|
51
|
+
}),
|
52
|
+
inject: [TYPESENSE_MODULE_OPTIONS],
|
53
|
+
},
|
54
|
+
];
|
package/src/response/index.ts
CHANGED
File without changes
|
@@ -1,12 +1,27 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
import { ApiPropertyOptional } from '@nestjs/swagger';
|
2
|
+
import { Type } from 'class-transformer';
|
3
|
+
export class PaginationResponse<TData> {
|
4
|
+
@ApiPropertyOptional()
|
5
|
+
data: TData[];
|
6
|
+
|
7
|
+
@ApiPropertyOptional()
|
8
|
+
@Type(() => Number)
|
9
|
+
total?: number = 0;
|
10
|
+
|
11
|
+
@ApiPropertyOptional()
|
12
|
+
@Type(() => Number)
|
13
|
+
offset?: number = 0;
|
14
|
+
|
15
|
+
@ApiPropertyOptional()
|
16
|
+
@Type(() => Number)
|
17
|
+
limit?: number = 25;
|
18
|
+
|
19
|
+
@ApiPropertyOptional()
|
20
|
+
@Type(() => Boolean)
|
21
|
+
hasNext?: boolean = false;
|
7
22
|
|
8
23
|
constructor(
|
9
|
-
data:
|
24
|
+
data: TData[],
|
10
25
|
total: number = data?.length || 0,
|
11
26
|
limit?: number,
|
12
27
|
offset?: number,
|
@@ -29,7 +44,7 @@ export class PaginationResponse<T> {
|
|
29
44
|
};
|
30
45
|
}
|
31
46
|
|
32
|
-
promise(): Promise<PaginationResponse<
|
47
|
+
promise(): Promise<PaginationResponse<TData>> {
|
33
48
|
return new Promise((resolve, reject) => {
|
34
49
|
resolve(this);
|
35
50
|
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './microservice.setup';
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import type { INestApplication } from '@nestjs/common';
|
2
|
+
import { Logger } from '@nestjs/common';
|
3
|
+
import { Transport } from '@nestjs/microservices';
|
4
|
+
|
5
|
+
import { AppUtils } from '../util';
|
6
|
+
|
7
|
+
export async function microserviceSetup(
|
8
|
+
app: INestApplication,
|
9
|
+
serviceName: string,
|
10
|
+
) {
|
11
|
+
AppUtils.killAppWithGrace(app);
|
12
|
+
const protoBasePath = process.cwd() + '/libs/proto-schema/src/proto';
|
13
|
+
const host = process.env['GRPC_HOST'];
|
14
|
+
const port = process.env['GRPC_PORT'];
|
15
|
+
const options = {
|
16
|
+
transport: Transport.GRPC,
|
17
|
+
options: {
|
18
|
+
url: `${host}:${port}`,
|
19
|
+
protoPath: `${protoBasePath}/${serviceName}.proto`,
|
20
|
+
package: `${serviceName}`,
|
21
|
+
},
|
22
|
+
};
|
23
|
+
app.connectMicroservice(options);
|
24
|
+
await app.startAllMicroservices();
|
25
|
+
Logger.log(
|
26
|
+
`GRPC ${serviceName} running on ${options.options.url}`,
|
27
|
+
'Bootstrap',
|
28
|
+
);
|
29
|
+
}
|
File without changes
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import type { INestApplication } from '@nestjs/common';
|
2
|
+
|
3
|
+
export class AppUtils {
|
4
|
+
public static killAppWithGrace = (app: INestApplication) => {
|
5
|
+
process.on('SIGINT', async () => {
|
6
|
+
setTimeout(() => process.exit(1), 5000);
|
7
|
+
await app.close();
|
8
|
+
process.exit(0);
|
9
|
+
});
|
10
|
+
|
11
|
+
// kill -15
|
12
|
+
process.on('SIGTERM', async () => {
|
13
|
+
setTimeout(() => process.exit(1), 5000);
|
14
|
+
await app.close();
|
15
|
+
process.exit(0);
|
16
|
+
});
|
17
|
+
};
|
18
|
+
}
|
package/src/util/index.ts
CHANGED