Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhook target support #104

Merged
merged 5 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-bags-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@team-plain/typescript-sdk': minor
---

Add support for creating, updating, deleting and fetcing webhook targets.
76 changes: 76 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
CreateCustomerEventDocument,
CreateThreadDocument,
CreateThreadEventDocument,
CreateWebhookTargetDocument,
CustomerByEmailDocument,
CustomerByIdDocument,
type CustomerCardConfigPartsFragment,
Expand All @@ -26,6 +27,7 @@ import {
CustomersDocument,
DeleteCustomerCardConfigDocument,
DeleteCustomerDocument,
DeleteWebhookTargetDocument,
type EmailPartsFragment,
type LabelPartsFragment,
LabelTypeDocument,
Expand All @@ -49,10 +51,14 @@ import {
ThreadsDocument,
UnassignThreadDocument,
UpdateCustomerCardConfigDocument,
UpdateWebhookTargetDocument,
UpsertCustomerDocument,
type UpsertResult,
UserByEmailDocument,
type UserPartsFragment,
WebhookTargetDocument,
type WebhookTargetPartsFragment,
WebhookTargetsDocument,
type WorkspacePartsFragment,
} from './graphql/types';
import { request } from './request';
Expand Down Expand Up @@ -729,4 +735,74 @@ export class PlainClient {

return unwrapData(res, (q) => nonNullable(q.createThreadEvent.threadEvent));
}

async createWebhookTarget(
input: VariablesOf<typeof CreateWebhookTargetDocument>['input']
): SDKResult<WebhookTargetPartsFragment> {
const res = await request(this.#ctx, {
query: CreateWebhookTargetDocument,
variables: {
input,
},
});

return unwrapData(res, (q) => nonNullable(q.createWebhookTarget.webhookTarget));
}

async updateWebhookTarget(
input: VariablesOf<typeof UpdateWebhookTargetDocument>['input']
): SDKResult<WebhookTargetPartsFragment> {
const res = await request(this.#ctx, {
query: UpdateWebhookTargetDocument,
variables: {
input,
},
});

return unwrapData(res, (q) => nonNullable(q.updateWebhookTarget.webhookTarget));
}

async deleteWebhookTarget(
input: VariablesOf<typeof DeleteWebhookTargetDocument>['input']
): SDKResult<null> {
const res = await request(this.#ctx, {
query: DeleteWebhookTargetDocument,
variables: {
input,
},
});

return unwrapData(res, () => null);
}

async getWebhookTargets(variables: VariablesOf<typeof WebhookTargetsDocument>): SDKResult<{
webhookTargets: WebhookTargetPartsFragment[];
pageInfo: PageInfoPartsFragment;
}> {
const res = await request(this.#ctx, {
query: WebhookTargetsDocument,
variables,
});

return unwrapData(res, (q) => ({
pageInfo: q.webhookTargets.pageInfo,
webhookTargets: q.webhookTargets.edges.map((edge) => edge.node),
}));
}

/**
* If the webhook target is not found it will return null
*/
async getWebhookTargetById(
variables: VariablesOf<typeof WebhookTargetDocument>
): SDKResult<WebhookTargetPartsFragment | null> {
const res = await request(this.#ctx, {
query: WebhookTargetDocument,
variables,
});

return unwrapData(res, (q) => {
return q.webhookTarget;
});
}
}
4 changes: 4 additions & 0 deletions src/graphql/fragments/webhookTargetEventSubscriptionParts.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fragment WebhookTargetEventSubscriptionParts on WebhookTargetEventSubscription {
__typename
eventType
}
21 changes: 21 additions & 0 deletions src/graphql/fragments/webhookTargetParts.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fragment WebhookTargetParts on WebhookTarget {
id
url
isEnabled
description
createdAt {
...DateTimeParts
}
createdBy {
...ActorParts
}
updatedAt {
...DateTimeParts
}
updatedBy {
...ActorParts
}
eventSubscriptions {
...WebhookTargetEventSubscriptionParts
}
}
10 changes: 10 additions & 0 deletions src/graphql/mutations/createWebhookTarget.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mutation createWebhookTarget($input: CreateWebhookTargetInput!) {
createWebhookTarget(input: $input) {
webhookTarget {
...WebhookTargetParts
}
error {
...MutationErrorParts
}
}
}
7 changes: 7 additions & 0 deletions src/graphql/mutations/deleteWebhookTarget.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mutation deleteWebhookTarget($input: DeleteWebhookTargetInput!) {
deleteWebhookTarget(input: $input) {
error {
...MutationErrorParts
}
}
}
10 changes: 10 additions & 0 deletions src/graphql/mutations/updateWebhookTarget.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mutation updateWebhookTarget($input: UpdateWebhookTargetInput!) {
updateWebhookTarget(input: $input) {
webhookTarget {
...WebhookTargetParts
}
error {
...MutationErrorParts
}
}
}
5 changes: 5 additions & 0 deletions src/graphql/queries/webhookTarget.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query webhookTarget($id: ID!) {
webhookTarget(webhookTargetId: $id) {
...WebhookTargetParts
}
}
13 changes: 13 additions & 0 deletions src/graphql/queries/webhookTargets.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query webhookTargets($first: Int, $after: String, $last: Int, $before: String) {
webhookTargets(first: $first, after: $after, last: $last, before: $before) {
edges {
cursor
node {
...WebhookTargetParts
}
}
pageInfo {
...PageInfoParts
}
}
}
Loading
Loading