Skip to content

feat: Add support for Zis Connection, Zis Token and zis webhook #137

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

Merged
merged 4 commits into from
May 30, 2025
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
60 changes: 60 additions & 0 deletions __tests__/services/zendesk-api-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,4 +697,64 @@ describe("ZendeskService", () => {
expect(requestMock).toHaveBeenCalledTimes(1);
});
});

describe("createAccessToken", () => {
it("should create an access token with the correct data", async () => {
await service.createZendeskAccessToken(102, ["read", "write"]);

expect(requestMock).toHaveBeenNthCalledWith(1, {
url: "/api/v2/oauth/tokens.json",
type: "POST",
contentType: "application/json",
data: JSON.stringify({
token: {
client_id: 102,
scopes: ["read", "write"]
}
})
});
expect(requestMock).toHaveBeenCalledTimes(1);
});
});

describe("createZisConnection", () => {
it("should create an access token with the correct data", async () => {
await service.createZisConnection("integrationName", "token", "connectionName", "zendesk.com");

expect(requestMock).toHaveBeenNthCalledWith(1, {
url: "/api/services/zis/integrations/integrationName/connections/bearer_token",
type: "POST",
contentType: "application/json",
headers: {
Authorization: "Bearer token"
},
data: JSON.stringify({
name: "connectionName",
token: "token",
allowed_domain: "zendesk.com"
})
});
expect(requestMock).toHaveBeenCalledTimes(1);
});
});

describe("createZisInboundWebhook", () => {
it("should create an access token with the correct data", async () => {
await service.createZisInboundWebhook("integrationName", "token", "source", "eventType");

expect(requestMock).toHaveBeenNthCalledWith(1, {
url: "/api/services/zis/inbound_webhooks/generic/integrationName",
type: "POST",
contentType: "application/json",
headers: {
Authorization: "Bearer token"
},
data: JSON.stringify({
source_system: "source",
event_type: "eventType"
})
});
expect(requestMock).toHaveBeenCalledTimes(1);
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zendesk/zaf-toolbox",
"version": "0.7.0",
"version": "0.8.0",
"description": "A toolbox for ZAF application built with 🩷 by Zendesk Labs",
"main": "lib/src/index.js",
"types": "lib/src/index.d.ts",
Expand Down
19 changes: 19 additions & 0 deletions src/models/zendesk-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,22 @@ export interface IMessage {
export interface IMessagesResults extends IZendeskResponse {
messages: IMessage[];
}

export interface ICreateAccessTokenResponseToken {
url: string;
id: number;
user_id: number;
client_id: number;
token: string;
refresh_token: string | null;
created_at: string;
expires_at: string | null;
used_at: string | null;
scopes: string[];
refresh_token_expires_at: string | null;
full_token: string;
}

export interface ICreateAccessTokenResponse {
token: ICreateAccessTokenResponseToken;
}
24 changes: 24 additions & 0 deletions src/models/zendesk-integration-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ export interface IZisJobspecsResponse {
has_more: boolean;
};
}

export interface IBearerTokenResponse {
name: string;
token: string;
allowed_domain: string;
created_at: string;
updated_at: string;
}

export interface ICreateConnectionResponse {
bearer_token: IBearerTokenResponse;
}

export interface ICreateInboundWebhookResponse {
id: string;
uuid: string;
zendesk_account_id: number;
path: string;
integration: string;
source_system: string;
event_type: string;
username: string;
password: string;
}
85 changes: 84 additions & 1 deletion src/services/zendesk-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ import {
Line,
IMessage,
IMessagesResults,
IListFilter
IListFilter,
ICreateAccessTokenResponse
} from "@models/index";
import {
ICreateConnectionResponse,
ICreateInboundWebhookResponse,
IZisIntegration,
IZisIntegrationResponse,
IZisJobspec,
Expand Down Expand Up @@ -398,4 +401,84 @@ export class ZendeskApiService {
type: "DELETE"
});
}

/**
* Creates an access token for the integration.
*
* @param {number} clientId - The client ID of the integration.
* @param {string[]} scopes - The scopes for the access token, e.g., ["read", "write"]. More information: https://developer.zendesk.com/api-reference/ticketing/oauth/oauth_tokens/#scopes
* @returns {Promise<ICreateAccessTokenResponse>} - The response from the API.
*/
public async createZendeskAccessToken(client_id: number, scopes: string[]): Promise<ICreateAccessTokenResponse> {
return await this.client.request({
url: "/api/v2/oauth/tokens.json",
type: "POST",
contentType: "application/json",
data: JSON.stringify({
token: {
client_id,
scopes
}
})
});
}

/**
* Creates a ZIS connection for the given integration.
*
* @param {string} integration_name - The name of the ZIS integration.
* @param {string} token - The token for the connection.
* @param {string} connection_name - The name of the connection.
* @param {string} allowed_domain - The allowed domain for the connection.
* @returns {Promise<ICreateConnectionResponse>} - The response from the API.
*/
public async createZisConnection(
integration_name: string,
token: string,
connection_name: string,
allowed_domain: string
): Promise<ICreateConnectionResponse> {
return await this.client.request({
url: `/api/services/zis/integrations/${integration_name}/connections/bearer_token`,
type: "POST",
contentType: "application/json",
headers: {
Authorization: `Bearer ${token}`
},
data: JSON.stringify({
name: connection_name,
token,
allowed_domain
})
});
}

/**
* Creates a ZIS inbound webhook for the given integration.
*
* @param {string} integration_name - The name of the ZIS integration.
* @param {string} token - The token for the webhook.
* @param {string} source_system - The source system for the webhook.
* @param {string} event_type - The event type for the webhook.
* @returns {Promise<ICreateInboundWebhookResponse>} - The response from the API.
*/
public async createZisInboundWebhook(
integration_name: string,
token: string,
source_system: string,
event_type: string
): Promise<ICreateInboundWebhookResponse> {
return await this.client.request({
url: `/api/services/zis/inbound_webhooks/generic/${integration_name}`,
type: "POST",
contentType: "application/json",
headers: {
Authorization: `Bearer ${token}`
},
data: JSON.stringify({
source_system,
event_type
})
});
}
}