Distinguish between sync/async events for webhooks
This commit is contained in:
parent
2f70574b73
commit
1951559886
1 changed files with 22 additions and 6 deletions
28
src/index.ts
28
src/index.ts
|
@ -5,18 +5,34 @@ import { print } from "graphql/language/printer";
|
||||||
export const capitalize = (value: string) => value.charAt(0).toUpperCase() + value.slice(1);
|
export const capitalize = (value: string) => value.charAt(0).toUpperCase() + value.slice(1);
|
||||||
export const dropFileExtension = (filename: string) => path.parse(filename).name;
|
export const dropFileExtension = (filename: string) => path.parse(filename).name;
|
||||||
|
|
||||||
export const inferWebhooks = async (baseURL: string, generatedGraphQL: object) => {
|
export const inferWebhooks = async (baseURL: string, generatedGraphQL: any) => {
|
||||||
const entries = await fg(['*.ts'], { cwd: 'pages/api/webhooks' });
|
const 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"), "_");
|
||||||
|
let eventType: string;
|
||||||
|
if (Object.values(generatedGraphQL.WebhookEventTypeAsyncEnum).includes(eventName)) {
|
||||||
|
eventType = "asyncEvents";
|
||||||
|
} else if (Object.values(generatedGraphQL.WebhookEventTypeSyncEnum).includes(eventName)) {
|
||||||
|
eventType = "syncEvents";
|
||||||
|
} else {
|
||||||
|
throw Error("Event type not found.");
|
||||||
|
}
|
||||||
|
|
||||||
const statement = `${camelcaseName}SubscriptionDocument`;
|
const statement = `${camelcaseName}SubscriptionDocument`;
|
||||||
const query = statement in generatedGraphQL ? print((generatedGraphQL as any)[statement]) : null;
|
let query: string;
|
||||||
|
if (statement in generatedGraphQL) {
|
||||||
|
query = print((generatedGraphQL as any)[statement]);
|
||||||
|
} else {
|
||||||
|
throw Error("Subscription not found.");
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
asyncEvents: [name.toUpperCase().replace("-", "_")],
|
[eventType]: [eventName],
|
||||||
query,
|
query,
|
||||||
targetUrl: `${baseURL}/api/webhooks/${name}`,
|
targetUrl: `${baseURL}/api/webhooks/${name}`,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue