Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
Make different client implemntation exist at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
yasaichi committed Mar 10, 2024
1 parent fe97c9f commit 2aa5298
Show file tree
Hide file tree
Showing 19 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"singleQuote": true
},
"imports": {
"@app/fake-api": "./libs/fake-api/src/index.ts"
"@app/fake-api-kiota": "./libs/fake-api-kiota/src/index.ts"
},
"lint": {
"include": ["src/", "apps/", "libs/", "test/"],
Expand Down
10 changes: 10 additions & 0 deletions libs/fake-api/schema.yaml → libs/fake-api-kiota/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ components:
type: string
body:
type: string
required:
- id
- userId
- title
- body
user:
type: object
properties:
Expand All @@ -33,6 +38,11 @@ components:
email:
type: string
format: email
required:
- id
- name
- username
- email
parameters:
user-id:
name: user-id
Expand Down
1 change: 1 addition & 0 deletions libs/fake-api-kiota/src/fake-api-kiota.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FAKE_API_KIOTA_SERVICE_TOKEN = Symbol('FAKE_API_KIOTA_SERVICE');
5 changes: 5 additions & 0 deletions libs/fake-api-kiota/src/fake-api-kiota.module-definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ConfigurableModuleBuilder } from '@nestjs/common';
import { type FakeApiKiotaModuleOptions } from './interfaces/fake-api-kiota-module-options.interface.ts';

export const { ConfigurableModuleClass, OPTIONS_TYPE } =
new ConfigurableModuleBuilder<FakeApiKiotaModuleOptions>().build();
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { AnonymousAuthenticationProvider } from '@microsoft/kiota-abstractions';
import { FetchRequestAdapter } from '@microsoft/kiota-http-fetchlibrary';
import { DynamicModule } from '@nestjs/common';
import { FAKE_API_SERVICE_TOKEN } from './fake-api.constants.ts';
import { FAKE_API_KIOTA_SERVICE_TOKEN } from './fake-api-kiota.constants.ts';
import {
ConfigurableModuleClass,
OPTIONS_TYPE,
} from './fake-api.module-definition.ts';
} from './fake-api-kiota.module-definition.ts';
import { createApiClient } from './generated/apiClient.ts';

export class FakeApiModule extends ConfigurableModuleClass {
export class FakeApiKiotaModule extends ConfigurableModuleClass {
private static nestLifecycleHooks: string[] = [
'onModuleInit',
'onApplicationBootstrap',
Expand All @@ -18,7 +18,7 @@ export class FakeApiModule extends ConfigurableModuleClass {
];
private static apiServicePropsCalledByNest = new Set([
'then',
...FakeApiModule.nestLifecycleHooks,
...FakeApiKiotaModule.nestLifecycleHooks,
]);

static register(
Expand All @@ -34,18 +34,18 @@ export class FakeApiModule extends ConfigurableModuleClass {
return {
...super.register(restOptions),
providers: [{
provide: FAKE_API_SERVICE_TOKEN,
provide: FAKE_API_KIOTA_SERVICE_TOKEN,
// NOTE: This is a workaround to use the API client with NestJS dependency injection mechanism.
// For further details, please refer to the following comment:
// https://github.com/microsoft/kiota-typescript/issues/1075#issuecomment-1987042257
useValue: new Proxy(createApiClient(requestAdapter), {
get: (target, prop) =>
FakeApiModule.apiServicePropsCalledByNest.has(prop.toString())
FakeApiKiotaModule.apiServicePropsCalledByNest.has(prop.toString())
? undefined
: Reflect.get(target, prop),
}),
}],
exports: [FAKE_API_SERVICE_TOKEN],
exports: [FAKE_API_KIOTA_SERVICE_TOKEN],
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { type RequestAdapter } from '@microsoft/kiota-abstractions';
export * from './fake-api.constants.ts';
export * from './fake-api.module.ts';
export * from './fake-api-kiota.constants.ts';
export * from './fake-api-kiota.module.ts';
export { type ApiClient as FakeApiService } from './generated/apiClient.ts';
export { type Post, type User } from './generated/models/index.ts';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type RequestAdapter } from '@microsoft/kiota-abstractions';

export interface FakeApiModuleOptions {
export interface FakeApiKiotaModuleOptions {
baseUrl?: string;
requestAdapter?: RequestAdapter;
}
1 change: 0 additions & 1 deletion libs/fake-api/src/fake-api.constants.ts

This file was deleted.

5 changes: 0 additions & 5 deletions libs/fake-api/src/fake-api.module-definition.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"private": true,
"license": "UNLICENSED",
"scripts": {
"codegen:fake-api": "kiota generate --co -l typescript -d ./libs/fake-api/schema.yaml -o ./libs/fake-api/src/generated/ && ls && find ./libs/fake-api/src/generated/ -type f -exec sed -i '' -E \"s/from '\\.(.+)\\/';/from '.\\1\\/index.ts';/g\" {} +",
"codegen:kiota": "kiota generate --co -l typescript -d ./libs/fake-api-kiota/schema.yaml -o ./libs/fake-api-kiota/src/generated/ && ls && find ./libs/fake-api-kiota/src/generated/ -type f -exec sed -i '' -E \"s/from '\\.(.+)\\/';/from '.\\1\\/index.ts';/g\" {} +",
"format": "deno fmt",
"start": "deno run --allow-env --allow-net --allow-read=. src/main.ts",
"start:dev": "deno run --allow-env --allow-net --allow-read=. --watch src/main.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/simple-users/simple-users.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FakeApiModule } from '@app/fake-api';
import { FakeApiKiotaModule } from '@app/fake-api-kiota';
import { Module } from '@nestjs/common';
import { SimpleUsersController } from './simple-users.controller.ts';
import { SimpleUsersService } from './simple-users.service.ts';

@Module({
imports: [FakeApiModule.register({})],
imports: [FakeApiKiotaModule.register({})],
controllers: [SimpleUsersController],
providers: [SimpleUsersService],
})
Expand Down
2 changes: 1 addition & 1 deletion src/simple-users/simple-users.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type Post,
type RequestAdapter,
type User,
} from '@app/fake-api';
} from '@app/fake-api-kiota';
import { Test, TestingModule } from '@nestjs/testing';
import { beforeEach, describe, it } from '@std/testing/bdd';
import assert from 'node:assert';
Expand Down
8 changes: 6 additions & 2 deletions src/simple-users/simple-users.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { FAKE_API_SERVICE_TOKEN, type FakeApiService } from '@app/fake-api';
import {
FAKE_API_KIOTA_SERVICE_TOKEN,
type FakeApiService,
} from '@app/fake-api-kiota';
import { Inject, Injectable } from '@nestjs/common';

@Injectable()
export class SimpleUsersService {
constructor(
@Inject(FAKE_API_SERVICE_TOKEN) private readonly apiService: FakeApiService,
@Inject(FAKE_API_KIOTA_SERVICE_TOKEN) private readonly apiService:
FakeApiService,
) {}

async findOneWithLatestPosts(id: number) {
Expand Down

0 comments on commit 2aa5298

Please sign in to comment.