Skip to content

Commit

Permalink
Expanding fetching system for FBI to handle multiple urls
Browse files Browse the repository at this point in the history
In that way we can change FBI graphql url depending on which query that
is being executed.
We need that because DDF wants to change profile name depending on which
query that is being run.
  • Loading branch information
spaceo committed Dec 19, 2023
1 parent e5065f2 commit 80678f8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
13 changes: 7 additions & 6 deletions src/core/dbc-gateway/graphql-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { QueryFunctionContext } from "react-query";
import FetchFailedCriticalError from "../fetchers/FetchFailedCriticalError";
import { getToken, TOKEN_LIBRARY_KEY, TOKEN_USER_KEY } from "../token";
import {
getServiceBaseUrl,
serviceUrlKeys
} from "../utils/reduxMiddleware/extractServiceBaseUrls";
import DbcGateWayHttpError from "./DbcGateWayHttpError";
import { getQueryUrlFromContext } from "./helper";

export const fetcher = <TData, TVariables>(
query: string,
variables?: TVariables
) => {
return (): Promise<TData> => {
return (context?: QueryFunctionContext): Promise<TData> => {
// Resolve the url based on the query name if present.
const url = getQueryUrlFromContext(context);

// The whole concept of agency id, profile and and bearer token needs to be refined.
// First version is with a library token.
const token = getToken(TOKEN_USER_KEY) || getToken(TOKEN_LIBRARY_KEY);
Expand All @@ -22,7 +23,7 @@ export const fetcher = <TData, TVariables>(
? ({ Authorization: `Bearer ${token}` } as object)
: {};

return fetch(getServiceBaseUrl(serviceUrlKeys.fbi), {
return fetch(url, {
method: "POST",
...{
headers: {
Expand Down
33 changes: 33 additions & 0 deletions src/core/dbc-gateway/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { QueryFunctionContext } from "react-query";
import {
getServiceBaseUrl,
serviceUrlKeys
} from "../utils/reduxMiddleware/extractServiceBaseUrls";

const map = {
searchWithPagination: serviceUrlKeys.fbiSearch,
getMaterial: serviceUrlKeys.fbiMaterial,
default: serviceUrlKeys.fbi
} as const;

const resolveBaseUrl = (query: string | null) => {
if (!query) {
return getServiceBaseUrl(map.default);
}
return getServiceBaseUrl(map[query as keyof typeof map] || map.default);
};

export const getQueryUrlFromContext = (
context: QueryFunctionContext | undefined
) => {
// Get the default base url if no context.
if (!context) {
return resolveBaseUrl(null);
}

const { queryKey } = context;
const [queryName] = queryKey;
return resolveBaseUrl(queryName as string);
};

export default {};
10 changes: 10 additions & 0 deletions src/core/storybook/serviceUrlArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,15 @@ export default {
name: "Base url for the FBI API",
defaultValue: "https://fbi-api.dbc.dk/next/graphql",
control: { type: "text" }
},
[serviceUrlKeys.fbiSearch]: {
name: "Base url for the FBI API (search)",
defaultValue: "https://fbi-api.dbc.dk/next/graphql",
control: { type: "text" }
},
[serviceUrlKeys.fbiMaterial]: {
name: "Base url for the FBI API (material)",
defaultValue: "https://fbi-api.dbc.dk/next/graphql",
control: { type: "text" }
}
};
24 changes: 19 additions & 5 deletions src/core/utils/reduxMiddleware/extractServiceBaseUrls.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { EnhancedStore } from "@reduxjs/toolkit";
import { Middleware } from "redux";

type Api = "publizon" | "fbs" | "dplCms" | "cover" | "materialList" | "fbi";
type Api =
| "publizon"
| "fbs"
| "dplCms"
| "cover"
| "materialList"
| "fbi"
| "fbiSearch"
| "fbiMaterial";

export type ApiBaseUrlKey = `${Api}BaseUrl`;

export type ServiceBaseUrls =
| Record<Api, ApiBaseUrlKey>
| Record<string, never>;

type ServiceBaseUrlKey = keyof ServiceBaseUrls;

export const serviceUrlKeys: Record<Api, ApiBaseUrlKey> = {
fbs: "fbsBaseUrl",
publizon: "publizonBaseUrl",
dplCms: "dplCmsBaseUrl",
cover: "coverBaseUrl",
materialList: "materialListBaseUrl",
fbi: "fbiBaseUrl"
fbi: "fbiBaseUrl",
fbiSearch: "fbiSearchBaseUrl",
fbiMaterial: "fbiMaterialBaseUrl"
} as const;

type ServiceBaseUrls = Record<Api, ApiBaseUrlKey> | Record<string, never>;
type ServiceBaseUrlKey = keyof typeof serviceBaseUrls;

// ServiceBaseUrls "store". We use this to store the base urls for the different services.
let serviceBaseUrls: ServiceBaseUrls = {};

Expand Down
2 changes: 2 additions & 0 deletions src/core/utils/types/global-url-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface GlobalUrlEntryPropsInterface {
coverBaseUrl: string;
materialBaseUrl: string;
fbiBaseUrl: string;
fbiSearchBaseUrl: string;
fbiMaterialBaseUrl: string;
authUrl: string;
ereolenHomepageUrl: string;
}
Expand Down

0 comments on commit 80678f8

Please sign in to comment.