Skip to content

Commit 0233bef

Browse files
authored
Merge pull request #137 from zendesk/victor.piolin/zis-oauth
feat: Add support for Zis Connection, Zis Token and zis webhook
2 parents 8c4c753 + 845b213 commit 0233bef

5 files changed

Lines changed: 188 additions & 2 deletions

File tree

__tests__/services/zendesk-api-service.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,4 +697,64 @@ describe("ZendeskService", () => {
697697
expect(requestMock).toHaveBeenCalledTimes(1);
698698
});
699699
});
700+
701+
describe("createAccessToken", () => {
702+
it("should create an access token with the correct data", async () => {
703+
await service.createZendeskAccessToken(102, ["read", "write"]);
704+
705+
expect(requestMock).toHaveBeenNthCalledWith(1, {
706+
url: "/api/v2/oauth/tokens.json",
707+
type: "POST",
708+
contentType: "application/json",
709+
data: JSON.stringify({
710+
token: {
711+
client_id: 102,
712+
scopes: ["read", "write"]
713+
}
714+
})
715+
});
716+
expect(requestMock).toHaveBeenCalledTimes(1);
717+
});
718+
});
719+
720+
describe("createZisConnection", () => {
721+
it("should create an access token with the correct data", async () => {
722+
await service.createZisConnection("integrationName", "token", "connectionName", "zendesk.com");
723+
724+
expect(requestMock).toHaveBeenNthCalledWith(1, {
725+
url: "/api/services/zis/integrations/integrationName/connections/bearer_token",
726+
type: "POST",
727+
contentType: "application/json",
728+
headers: {
729+
Authorization: "Bearer token"
730+
},
731+
data: JSON.stringify({
732+
name: "connectionName",
733+
token: "token",
734+
allowed_domain: "zendesk.com"
735+
})
736+
});
737+
expect(requestMock).toHaveBeenCalledTimes(1);
738+
});
739+
});
740+
741+
describe("createZisInboundWebhook", () => {
742+
it("should create an access token with the correct data", async () => {
743+
await service.createZisInboundWebhook("integrationName", "token", "source", "eventType");
744+
745+
expect(requestMock).toHaveBeenNthCalledWith(1, {
746+
url: "/api/services/zis/inbound_webhooks/generic/integrationName",
747+
type: "POST",
748+
contentType: "application/json",
749+
headers: {
750+
Authorization: "Bearer token"
751+
},
752+
data: JSON.stringify({
753+
source_system: "source",
754+
event_type: "eventType"
755+
})
756+
});
757+
expect(requestMock).toHaveBeenCalledTimes(1);
758+
});
759+
});
700760
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zendesk/zaf-toolbox",
3-
"version": "0.7.0",
3+
"version": "0.8.0",
44
"description": "A toolbox for ZAF application built with 🩷 by Zendesk Labs",
55
"main": "lib/src/index.js",
66
"types": "lib/src/index.d.ts",

src/models/zendesk-api.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,22 @@ export interface IMessage {
153153
export interface IMessagesResults extends IZendeskResponse {
154154
messages: IMessage[];
155155
}
156+
157+
export interface ICreateAccessTokenResponseToken {
158+
url: string;
159+
id: number;
160+
user_id: number;
161+
client_id: number;
162+
token: string;
163+
refresh_token: string | null;
164+
created_at: string;
165+
expires_at: string | null;
166+
used_at: string | null;
167+
scopes: string[];
168+
refresh_token_expires_at: string | null;
169+
full_token: string;
170+
}
171+
172+
export interface ICreateAccessTokenResponse {
173+
token: ICreateAccessTokenResponseToken;
174+
}

src/models/zendesk-integration-services.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,27 @@ export interface IZisJobspecsResponse {
3333
has_more: boolean;
3434
};
3535
}
36+
37+
export interface IBearerTokenResponse {
38+
name: string;
39+
token: string;
40+
allowed_domain: string;
41+
created_at: string;
42+
updated_at: string;
43+
}
44+
45+
export interface ICreateConnectionResponse {
46+
bearer_token: IBearerTokenResponse;
47+
}
48+
49+
export interface ICreateInboundWebhookResponse {
50+
id: string;
51+
uuid: string;
52+
zendesk_account_id: number;
53+
path: string;
54+
integration: string;
55+
source_system: string;
56+
event_type: string;
57+
username: string;
58+
password: string;
59+
}

src/services/zendesk-api-service.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ import {
2626
Line,
2727
IMessage,
2828
IMessagesResults,
29-
IListFilter
29+
IListFilter,
30+
ICreateAccessTokenResponse
3031
} from "@models/index";
3132
import {
33+
ICreateConnectionResponse,
34+
ICreateInboundWebhookResponse,
3235
IZisIntegration,
3336
IZisIntegrationResponse,
3437
IZisJobspec,
@@ -398,4 +401,84 @@ export class ZendeskApiService {
398401
type: "DELETE"
399402
});
400403
}
404+
405+
/**
406+
* Creates an access token for the integration.
407+
*
408+
* @param {number} clientId - The client ID of the integration.
409+
* @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
410+
* @returns {Promise<ICreateAccessTokenResponse>} - The response from the API.
411+
*/
412+
public async createZendeskAccessToken(client_id: number, scopes: string[]): Promise<ICreateAccessTokenResponse> {
413+
return await this.client.request({
414+
url: "/api/v2/oauth/tokens.json",
415+
type: "POST",
416+
contentType: "application/json",
417+
data: JSON.stringify({
418+
token: {
419+
client_id,
420+
scopes
421+
}
422+
})
423+
});
424+
}
425+
426+
/**
427+
* Creates a ZIS connection for the given integration.
428+
*
429+
* @param {string} integration_name - The name of the ZIS integration.
430+
* @param {string} token - The token for the connection.
431+
* @param {string} connection_name - The name of the connection.
432+
* @param {string} allowed_domain - The allowed domain for the connection.
433+
* @returns {Promise<ICreateConnectionResponse>} - The response from the API.
434+
*/
435+
public async createZisConnection(
436+
integration_name: string,
437+
token: string,
438+
connection_name: string,
439+
allowed_domain: string
440+
): Promise<ICreateConnectionResponse> {
441+
return await this.client.request({
442+
url: `/api/services/zis/integrations/${integration_name}/connections/bearer_token`,
443+
type: "POST",
444+
contentType: "application/json",
445+
headers: {
446+
Authorization: `Bearer ${token}`
447+
},
448+
data: JSON.stringify({
449+
name: connection_name,
450+
token,
451+
allowed_domain
452+
})
453+
});
454+
}
455+
456+
/**
457+
* Creates a ZIS inbound webhook for the given integration.
458+
*
459+
* @param {string} integration_name - The name of the ZIS integration.
460+
* @param {string} token - The token for the webhook.
461+
* @param {string} source_system - The source system for the webhook.
462+
* @param {string} event_type - The event type for the webhook.
463+
* @returns {Promise<ICreateInboundWebhookResponse>} - The response from the API.
464+
*/
465+
public async createZisInboundWebhook(
466+
integration_name: string,
467+
token: string,
468+
source_system: string,
469+
event_type: string
470+
): Promise<ICreateInboundWebhookResponse> {
471+
return await this.client.request({
472+
url: `/api/services/zis/inbound_webhooks/generic/${integration_name}`,
473+
type: "POST",
474+
contentType: "application/json",
475+
headers: {
476+
Authorization: `Bearer ${token}`
477+
},
478+
data: JSON.stringify({
479+
source_system,
480+
event_type
481+
})
482+
});
483+
}
401484
}

0 commit comments

Comments
 (0)