Merge pull request #1 from magul/infer-webhooks-in-production-build
Infer webhooks in production build
This commit is contained in:
commit
aaf6bbd0e0
1 changed files with 23 additions and 8 deletions
31
src/index.ts
31
src/index.ts
|
@ -1,22 +1,37 @@
|
|||
import path from 'path';
|
||||
import fg from 'fast-glob';
|
||||
import path from "path";
|
||||
import fg from "fast-glob";
|
||||
|
||||
import { print } from "graphql/language/printer.js";
|
||||
|
||||
export const capitalize = (value: string) => value.charAt(0).toUpperCase() + value.slice(1);
|
||||
export const dropFileExtension = (filename: string) => path.parse(filename).name;
|
||||
const capitalize = (value: string) =>
|
||||
value.charAt(0).toUpperCase() + value.slice(1);
|
||||
|
||||
const dropFileExtension = (filename: string) => path.parse(filename).name;
|
||||
|
||||
export const inferWebhooks = async (baseURL: string, generatedGraphQL: any) => {
|
||||
const entries = await fg(["*.ts"], { cwd: "pages/api/webhooks" });
|
||||
let entries;
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
entries = await fg(["*.js"], { cwd: `${__dirname}/webhooks` });
|
||||
} else {
|
||||
entries = await fg(["*.ts"], { cwd: `pages/api/webhooks` });
|
||||
}
|
||||
|
||||
return entries.map(dropFileExtension).map((name: string) => {
|
||||
const camelcaseName = name.split("-").map(capitalize).join("");
|
||||
|
||||
const eventName = name.toUpperCase().replace(new RegExp("-", "g"), "_");
|
||||
let eventType: string;
|
||||
if (Object.values(generatedGraphQL.WebhookEventTypeAsyncEnum).includes(eventName)) {
|
||||
if (
|
||||
Object.values(generatedGraphQL.WebhookEventTypeAsyncEnum).includes(
|
||||
eventName
|
||||
)
|
||||
) {
|
||||
eventType = "asyncEvents";
|
||||
} else if (Object.values(generatedGraphQL.WebhookEventTypeSyncEnum).includes(eventName)) {
|
||||
} else if (
|
||||
Object.values(generatedGraphQL.WebhookEventTypeSyncEnum).includes(
|
||||
eventName
|
||||
)
|
||||
) {
|
||||
eventType = "syncEvents";
|
||||
} else {
|
||||
throw Error("Event type not found.");
|
||||
|
@ -37,4 +52,4 @@ export const inferWebhooks = async (baseURL: string, generatedGraphQL: any) => {
|
|||
targetUrl: `${baseURL}/api/webhooks/${name}`,
|
||||
};
|
||||
});
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue