2023-02-15 11:32:33 +00:00
|
|
|
import { AuthData } from "@saleor/app-sdk/APL";
|
|
|
|
import { createDebug } from "../debug";
|
|
|
|
import { createSettingsManager } from "../metadata";
|
2023-06-19 13:59:27 +00:00
|
|
|
import { createGraphQLClient } from "@saleor/apps-shared";
|
2023-02-15 11:32:33 +00:00
|
|
|
|
|
|
|
interface GetAlgoliaConfigurationArgs {
|
|
|
|
authData: AuthData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const debug = createDebug("getAlgoliaConfiguration");
|
|
|
|
|
|
|
|
export const getAlgoliaConfiguration = async ({ authData }: GetAlgoliaConfigurationArgs) => {
|
2023-06-19 13:59:27 +00:00
|
|
|
const client = createGraphQLClient({
|
|
|
|
saleorApiUrl: authData.saleorApiUrl,
|
|
|
|
token: authData.token,
|
|
|
|
});
|
2023-02-15 11:32:33 +00:00
|
|
|
|
2023-08-29 20:53:51 +00:00
|
|
|
const settings = createSettingsManager(client, authData.appId);
|
2023-02-15 11:32:33 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const secretKey = await settings.get("secretKey", authData.domain);
|
2023-04-18 13:10:00 +00:00
|
|
|
|
2023-02-15 11:32:33 +00:00
|
|
|
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);
|
2023-04-18 13:10:00 +00:00
|
|
|
|
2023-02-15 11:32:33 +00:00
|
|
|
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)) || "";
|
2023-04-18 13:10:00 +00:00
|
|
|
|
2023-02-15 11:32:33 +00:00
|
|
|
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" }],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|