saleor-apps-redis_apl/apps/search/src/lib/algolia/getAlgoliaConfiguration.ts
Lukasz Ostrowski 0f84985c98
Add tRPC to search app (#940)
* Add tRPC to search app

* Implemented configuration endpoint in trpc

* replace settings manager to use shared factory

* replace configuration calls to trpc
2023-08-29 22:53:51 +02:00

65 lines
1.6 KiB
TypeScript

import { AuthData } from "@saleor/app-sdk/APL";
import { createDebug } from "../debug";
import { createSettingsManager } from "../metadata";
import { createGraphQLClient } from "@saleor/apps-shared";
interface GetAlgoliaConfigurationArgs {
authData: AuthData;
}
const debug = createDebug("getAlgoliaConfiguration");
export const getAlgoliaConfiguration = async ({ authData }: GetAlgoliaConfigurationArgs) => {
const client = createGraphQLClient({
saleorApiUrl: authData.saleorApiUrl,
token: authData.token,
});
const settings = createSettingsManager(client, authData.appId);
try {
const secretKey = await settings.get("secretKey", authData.domain);
if (!secretKey?.length) {
return {
errors: [
{
message:
"Missing secret key to the Algolia API. Please, configure the application first.",
},
],
};
}
const appId = await settings.get("appId", authData.domain);
if (!appId?.length) {
return {
errors: [
{
message: "Missing App ID to the Algolia API. Please, configure the application first.",
},
],
};
}
const indexNamePrefix = (await settings.get("indexNamePrefix", authData.domain)) || "";
debug("Configuration fetched");
return {
settings: {
appId,
secretKey,
indexNamePrefix,
},
};
} catch (error) {
debug("Unexpected error during fetching the configuration");
if (error instanceof Error) {
debug(error.message);
}
return {
errors: [{ message: "Couldn't fetch the settings from the API" }],
};
}
};