Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIクライアントのFactoryを作って、動的にAPIクライアントを変更できるようにする #420

Merged
merged 6 commits into from
Oct 31, 2021
26 changes: 26 additions & 0 deletions src/infrastructures/EngineConnector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Configuration, DefaultApi, DefaultApiInterface } from "@/openapi";
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved

export interface IEngineConnectorFactory {
// FIXME: hostという名前の時点で外部APIに接続するという知識が出てきてしまっているので
// Factory自体に型パラメータを付けて、接続方法だったり設定、IDみたいな名前で表現する
instance: (host: string) => DefaultApiInterface;
}

const OpenAPIEngineConnectorFactoryImpl = (): IEngineConnectorFactory => {
const instanceMapper: Record<string, DefaultApiInterface> = {};
return {
instance: (host: string) => {
const cached = instanceMapper[host];
if (cached !== undefined) {
return cached;
}
const api = new DefaultApi(new Configuration({ basePath: host }));
instanceMapper[host] = api;

return api;
},
};
};

export const OpenAPIEngineConnectorFactory =
OpenAPIEngineConnectorFactoryImpl();
Loading