Skip to content

Commit

Permalink
feat; add factory method for extensions (#7218)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszherba authored Jul 16, 2024
1 parent 29f626f commit 6bac6f5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
28 changes: 28 additions & 0 deletions .changeset/gentle-lies-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
"@vue-storefront/middleware": minor
---

[ADDED] Added factory function for the extension API. Previously the extension API was an object with a set of methods. Now it can be created using a factory function the same way as the base API.

Previously only object was allowed:

```ts
export const extension: ApiClientExtension = {
name: "extension",
extendApiMethods: {
...extendedMethods, //methods as an object
},
};
```

Now you can either use an object or a factory function:

```ts
export const extension: ApiClientExtension = {
name: "extension",
// methods as a factory function with injected config object
extendApiMethods: ({ config }) => {
return createMyMethods(config);
},
};
```
29 changes: 18 additions & 11 deletions packages/middleware/src/apiClientFactory/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isFunction } from "../helpers";
import {
ApiClientConfig,
ApiClientExtension,
Expand All @@ -10,7 +11,6 @@ import {
ExtensionHookWith,
ExtensionWith,
} from "../types";
import { isFunction } from "../helpers";
import { applyContextToApi } from "./applyContextToApi";

const apiClientFactory = <
Expand All @@ -19,6 +19,14 @@ const apiClientFactory = <
>(
factoryParams: ApiClientFactoryParams<ALL_SETTINGS, ALL_FUNCTIONS>
): ApiClientFactory<any, ALL_FUNCTIONS> => {
const resolveApi = async (api: any, settings: any) => {
if (typeof api === "function") {
return await api(settings);
}

return api ?? {};
};

const createApiClient: CreateApiClientFn<any, ALL_FUNCTIONS> =
async function createApiClient(
this: CallableContext<ALL_FUNCTIONS>,
Expand Down Expand Up @@ -97,29 +105,28 @@ const apiClientFactory = <

const context = { ...settings, ...(this?.middleware || {}) };

const { api: apiOrApiFactory = {} } = factoryParams;

const isApiFactory = typeof apiOrApiFactory === "function";
const api = isApiFactory
? await apiOrApiFactory(settings)
: apiOrApiFactory;
const api = await resolveApi(factoryParams.api, settings);

const namespacedExtensions: Record<string, any> = {};
let sharedExtensions = customApi;

rawExtensions.forEach((extension) => {
for await (const extension of rawExtensions) {
const extendedApiMethods = await resolveApi(
extension.extendApiMethods,
settings
);
if (extension.isNamespaced) {
namespacedExtensions[extension.name] = {
...(namespacedExtensions?.[extension.name] ?? {}),
...extension.extendApiMethods,
...extendedApiMethods,
};
} else {
sharedExtensions = {
...sharedExtensions,
...extension.extendApiMethods,
...extendedApiMethods,
};
}
});
}

const integrationApi = applyContextToApi(
api,
Expand Down
10 changes: 6 additions & 4 deletions packages/middleware/src/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { Express, Request, Response } from "express";
import { WithRequired } from "./index";
import {
ApiClientMethod,
ContextQuery,
CustomQuery,
CustomQueryFunction,
TObject,
} from "./base";
import { WithRequired } from "./index";
import { ApiClient, ApiClientConfig, ApiClientFactory } from "./server";

export type ApiMethods = Record<string, ApiClientMethod>;
export type ApiMethodsFactory<
API extends ApiMethods,
CONFIG extends ApiClientConfig
> = (configuration: CONFIG) => API;
> = (configuration: { config: CONFIG }) => API;

export type ApiClientMethodWithContext<CONTEXT> = (
context: CONTEXT,
Expand Down Expand Up @@ -55,10 +55,12 @@ export interface ApiClientExtensionHooks<C = any> {
) => AfterCallArgs;
}

export interface ApiClientExtension<API = any, CONTEXT = any> {
export interface ApiClientExtension<API = any, CONTEXT = any, CONFIG = any> {
name: string;
isNamespaced?: boolean;
extendApiMethods?: ExtendApiMethod<API, CONTEXT>;
extendApiMethods?:
| ExtendApiMethod<API, CONTEXT>
| ApiMethodsFactory<ExtendApiMethod<API, CONTEXT>, CONFIG>;
extendApp?: ({
app,
configuration,
Expand Down

0 comments on commit 6bac6f5

Please sign in to comment.