Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into expand-test-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Zendesk-NathanBellette committed Oct 24, 2023
2 parents 24e664b + 8a94a7c commit ffa7ad2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/clients/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
* @property {Array} sideLoad - Array to handle side-loaded resources.
* @property {string} userAgent - User agent for the client.
* @property {Array} jsonAPINames - Array to hold names used in the JSON API.
* @property {boolean} useDotJson - Flag to indicate if the API endpoint should use '.json' ending.
* @property {ApiTypes} apiType - Type of Zendesk API to initialize (e.g., 'core', 'helpcenter').
* @property {CustomEventTarget} eventTarget - Event target to handle custom events.
* @property {Transporter} transporter - Transporter for making requests.
Expand All @@ -49,6 +50,7 @@ class Client {
this.options = this._buildOptions(options, apiType);
this.sideLoad = [];
this.jsonAPINames = [];
this.useDotJson = true;
this.userAgent = generateUserAgent();
this.eventTarget = new CustomEventTarget();
}
Expand All @@ -57,7 +59,11 @@ class Client {
get transporter() {
if (this._transporter) return this._transporter;

this._transporter = new Transporter(this.options, this.sideLoad);
this._transporter = new Transporter(
this.options,
this.sideLoad,
this.useDotJson,
);
// Listen to transporter's debug events and re-emit them on the Client
this._transporter.on('debug::request', (eventData) => {
this.emit('debug::request', eventData.detail);
Expand Down
1 change: 1 addition & 0 deletions src/clients/core/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Webhooks extends Client {
constructor(options) {
super(options);
this.jsonAPINames = ['webhooks', 'webhook'];
this.useDotJson = false;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/clients/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ function assembleUrl(self, method, uri) {
);

// Construct the URL
const basePath = `${endpointUri}/${segments
.filter((segment) => segment !== undefined && segment !== '')
.join('/')}.json`;
const path = segments.filter(Boolean).join('/');
const extension = self.useDotJson === false ? '' : '.json'; // Undefined is true (default)
const basePath = `${endpointUri}/${path}${extension}`;
return queryString ? `${basePath}?${queryString}` : basePath;
}

Expand Down
3 changes: 2 additions & 1 deletion src/clients/transporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ const defaultTransportConfig = {
},
};
class Transporter {
constructor(options, sideLoad = []) {
constructor(options, sideLoad = [], useDotJson = true) {
this.options = options;
this.sideLoad = sideLoad;
this.useDotJson = useDotJson;
this.authHandler = new AuthorizationHandler(this.options);
this.eventTarget = new CustomEventTarget();
this.endpointChecker = new EndpointChecker();
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/webhooks_endpoint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"scope": "https://nodejsapi.zendesk.com:443",
"method": "POST",
"path": "/api/v2/webhooks",
"status": 201,
"response": {
"id": 19442893058196
},
"responseIsBinary": false
}
]
33 changes: 33 additions & 0 deletions test/webhooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from 'node:path';
import dotenv from 'dotenv';
import {back as nockBack} from 'nock';
import {beforeAll, describe, expect, it} from 'vitest';
import {setupClient} from './setup.js';

dotenv.config();

describe('Zendesk Client Webhooks', () => {
const client = setupClient();

beforeAll(async () => {
nockBack.setMode('record');
nockBack.fixtures = path.join(__dirname, '/fixtures');
});

it('should call endpoint without .json', async () => {
const {nockDone} = await nockBack('webhooks_endpoint.json');
const {result} = await client.webhooks.create({
webhook: {
name: `Web Hulk`,
endpoint: 'noop://noop',
http_method: 'POST',
request_format: 'json',
status: 'active',
subscriptions: ['conditional_ticket_events'],
},
});
nockDone();

expect(result.id).toBeDefined();
});
});

0 comments on commit ffa7ad2

Please sign in to comment.