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
29
src/index.ts
29
src/index.ts
|
@ -1,22 +1,37 @@
|
||||||
import path from 'path';
|
import path from "path";
|
||||||
import fg from 'fast-glob';
|
import fg from "fast-glob";
|
||||||
|
|
||||||
import { print } from "graphql/language/printer.js";
|
import { print } from "graphql/language/printer.js";
|
||||||
|
|
||||||
export const capitalize = (value: string) => value.charAt(0).toUpperCase() + value.slice(1);
|
const capitalize = (value: string) =>
|
||||||
export const dropFileExtension = (filename: string) => path.parse(filename).name;
|
value.charAt(0).toUpperCase() + value.slice(1);
|
||||||
|
|
||||||
|
const dropFileExtension = (filename: string) => path.parse(filename).name;
|
||||||
|
|
||||||
export const inferWebhooks = async (baseURL: string, generatedGraphQL: any) => {
|
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) => {
|
return entries.map(dropFileExtension).map((name: string) => {
|
||||||
const camelcaseName = name.split("-").map(capitalize).join("");
|
const camelcaseName = name.split("-").map(capitalize).join("");
|
||||||
|
|
||||||
const eventName = name.toUpperCase().replace(new RegExp("-", "g"), "_");
|
const eventName = name.toUpperCase().replace(new RegExp("-", "g"), "_");
|
||||||
let eventType: string;
|
let eventType: string;
|
||||||
if (Object.values(generatedGraphQL.WebhookEventTypeAsyncEnum).includes(eventName)) {
|
if (
|
||||||
|
Object.values(generatedGraphQL.WebhookEventTypeAsyncEnum).includes(
|
||||||
|
eventName
|
||||||
|
)
|
||||||
|
) {
|
||||||
eventType = "asyncEvents";
|
eventType = "asyncEvents";
|
||||||
} else if (Object.values(generatedGraphQL.WebhookEventTypeSyncEnum).includes(eventName)) {
|
} else if (
|
||||||
|
Object.values(generatedGraphQL.WebhookEventTypeSyncEnum).includes(
|
||||||
|
eventName
|
||||||
|
)
|
||||||
|
) {
|
||||||
eventType = "syncEvents";
|
eventType = "syncEvents";
|
||||||
} else {
|
} else {
|
||||||
throw Error("Event type not found.");
|
throw Error("Event type not found.");
|
||||||
|
|
Loading…
Reference in a new issue