|
| 1 | +'use strict' |
| 2 | +const fetch = require('node-fetch') |
| 3 | +const IntentoConnector = require('../src/index') |
| 4 | + |
| 5 | +// Quickly load .env files into the environment |
| 6 | +require('dotenv').load() |
| 7 | +const apikey = process.env.INTENTO_API_KEY |
| 8 | +const host = process.env.INTENTO_API_HOST |
| 9 | + |
| 10 | +const DEBUG = false |
| 11 | + |
| 12 | +/** |
| 13 | + * ZD Client mockup |
| 14 | + * |
| 15 | + * https://developer.zendesk.com/apps/docs/developer-guide/using_sdk#using-secure-settings |
| 16 | + * @param {*} params all the params |
| 17 | + * @returns {undefined} |
| 18 | + */ |
| 19 | +class ZDClient { |
| 20 | + |
| 21 | + request(params) { |
| 22 | + |
| 23 | + const { |
| 24 | + url, |
| 25 | + headers, |
| 26 | + secure, |
| 27 | + type, |
| 28 | + contentType, |
| 29 | + data, |
| 30 | + } = params |
| 31 | + |
| 32 | + |
| 33 | + if (secure) { |
| 34 | + for (const key in headers) { |
| 35 | + headers[key] = headers[key].replace('{{setting.token}}', apikey) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return fetch(url, { |
| 40 | + method: type, |
| 41 | + headers: { ...headers, 'content-type': contentType }, |
| 42 | + body: type !== 'GET' ? data : undefined |
| 43 | + }).then(response => { |
| 44 | + // here mimicing zd request API as we understand it |
| 45 | + // |
| 46 | + |
| 47 | + // zd request API: https://developer.zendesk.com/apps/docs/core-api/client_api#client.requestoptions |
| 48 | + // |
| 49 | + // console.log(response.responseJSON); // body of the HTTP response |
| 50 | + // console.log(response.responseText); // body of the HTTP response |
| 51 | + // console.log(response.status); // HTTP response status |
| 52 | + // console.log(response.statusText); // Is either 'success' or 'error' |
| 53 | + // console.log(response.headers); // HTTP response headers |
| 54 | + |
| 55 | + const { status, headers } = response |
| 56 | + |
| 57 | + return response.text().then(bodytext => { |
| 58 | + // it's not exactly clear whether JSON parsing error |
| 59 | + // forces zd request to return 'error', but let's consider this as true for now |
| 60 | + const zdResponse = { responseText: bodytext, status, statusText: 'error', headers } |
| 61 | + try { |
| 62 | + zdResponse.responseJSON = JSON.parse(bodytext) |
| 63 | + zdResponse.statusText = 'success' |
| 64 | + } catch (exception) { |
| 65 | + zdResponse.error = exception |
| 66 | + } |
| 67 | + |
| 68 | + return zdResponse |
| 69 | + }) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +const zdclient = new ZDClient() |
| 75 | +const HTTP_CODES = { |
| 76 | + 404: 'Not Found' |
| 77 | +} |
| 78 | +/** |
| 79 | + * Fetcher function which can work with zendesk client |
| 80 | + * @param {*} param0 incoming parameters |
| 81 | + * @returns {undefined} |
| 82 | + */ |
| 83 | +function zdfetcher({ requestOptions, /*debug, verbose,*/ data, content }) { |
| 84 | + // console.log('zd fetcher ', requestOptions, data, content) |
| 85 | + let { headers, host, path, method } = requestOptions |
| 86 | + |
| 87 | + delete headers["content-type"] |
| 88 | + headers.apikey = "{{setting.token}}" |
| 89 | + return zdclient.request({ |
| 90 | + url: `https://${host}${path}`, // for ex. api.inten.to/ai/text/translate |
| 91 | + headers, |
| 92 | + secure: true, |
| 93 | + type: method, // POST, GET, etc |
| 94 | + contentType: 'application/json', |
| 95 | + data: data || JSON.stringify(content) || '' |
| 96 | + }).then(zdresponse => { |
| 97 | + // console.log(' got zdresponse ', zdresponse) |
| 98 | + const { status, statusText } = zdresponse |
| 99 | + |
| 100 | + // default fetcher treats 404 as errors and throws, so should we |
| 101 | + |
| 102 | + // here other non 200 statues should be checked |
| 103 | + if (statusText === 'success' && status !== 404) { |
| 104 | + return zdresponse.responseJSON |
| 105 | + } |
| 106 | + |
| 107 | + // might be that zd request returns actual statusMessage in some undocumented field |
| 108 | + let error = { statusCode: status, statusMessage: HTTP_CODES[status] } |
| 109 | + try { |
| 110 | + error.error = zdresponse.responseJSON.error |
| 111 | + } |
| 112 | + catch (exception) { |
| 113 | + error.error = exception |
| 114 | + } |
| 115 | + throw error |
| 116 | + }) |
| 117 | + |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +const client_for_zd = new IntentoConnector({ apikey, host }, { debug: DEBUG, fetcher: zdfetcher }) |
| 123 | + |
| 124 | +describe('zd fetcher test', () => { |
| 125 | + it('get translation', async () => { |
| 126 | + expect.assertions(10) |
| 127 | + const translate = await client_for_zd.ai.text.translate.fulfill({ |
| 128 | + text: 'A sample text', |
| 129 | + to: 'es', |
| 130 | + }) |
| 131 | + if (DEBUG) { |
| 132 | + console.info('Current apikey settings: ', translate) |
| 133 | + } |
| 134 | + |
| 135 | + expect(translate).toBeInstanceOf(Object) |
| 136 | + expect(translate.hasOwnProperty('id')).toBeTruthy() |
| 137 | + expect(translate.hasOwnProperty('done')).toBeTruthy() |
| 138 | + expect(translate.hasOwnProperty('response')).toBeTruthy() |
| 139 | + expect(translate.hasOwnProperty('meta')).toBeTruthy() |
| 140 | + expect(translate.hasOwnProperty('error')).toBeTruthy() |
| 141 | + |
| 142 | + const res = translate.response[0] |
| 143 | + expect(res).toBeDefined() |
| 144 | + expect(res.hasOwnProperty('results')).toBeTruthy() |
| 145 | + expect(res.hasOwnProperty('meta')).toBeTruthy() |
| 146 | + expect(res.hasOwnProperty('service')).toBeTruthy() |
| 147 | + }) |
| 148 | + |
| 149 | + |
| 150 | + it('fails without options specified', async () => { |
| 151 | + expect.assertions(2) |
| 152 | + await client_for_zd.makeRequest().catch(e => { |
| 153 | + expect(e.statusCode).toEqual(404) |
| 154 | + expect(e.statusMessage).toEqual('Not Found') |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + it('fails with an incorrect path specified: /', async () => { |
| 159 | + expect.assertions(2) |
| 160 | + await client_for_zd.makeRequest({ path: '/' }).catch(e => { |
| 161 | + expect(e.statusCode).toEqual(404) |
| 162 | + expect(e.statusMessage).toEqual('Not Found') |
| 163 | + }) |
| 164 | + }) |
| 165 | + |
| 166 | + it('fails with an incorrect path specified: /settings', async () => { |
| 167 | + expect.assertions(3) |
| 168 | + await client_for_zd.makeRequest({ path: '/settings' }).catch(e => { |
| 169 | + expect(e.error).toBeDefined() |
| 170 | + expect(e.error.code).toEqual(404) |
| 171 | + expect(e.error.message).toEqual('no such intent settings/') |
| 172 | + }) |
| 173 | + }) |
| 174 | + |
| 175 | + it('fails with an incorrect path specified: /ai', async () => { |
| 176 | + expect.assertions(3) |
| 177 | + await client_for_zd.makeRequest({ path: '/ai' }).catch(e => { |
| 178 | + expect(e.error).toBeDefined() |
| 179 | + expect(e.error.code).toEqual(404) |
| 180 | + expect(e.error.message).toEqual('no such intent ai/') |
| 181 | + }) |
| 182 | + }) |
| 183 | + |
| 184 | + it('fails with an incorrect path specified: /usage', async () => { |
| 185 | + expect.assertions(2) |
| 186 | + await client_for_zd |
| 187 | + .makeRequest({ path: '/usage' }) |
| 188 | + .catch(e => { |
| 189 | + expect(e.error).toBeDefined() |
| 190 | + expect(e.error).toEqual('No such endpoint.') |
| 191 | + // expect(e.error.code).toEqual(404) |
| 192 | + // expect(e.error.message).toEqual('No such endpoint.') |
| 193 | + }) |
| 194 | + }) |
| 195 | + |
| 196 | + it('shows settings/languages', async () => { |
| 197 | + expect.assertions(1) |
| 198 | + const langSettings = await client_for_zd.makeRequest({ |
| 199 | + path: '/settings/languages', |
| 200 | + }) |
| 201 | + if (DEBUG) { |
| 202 | + console.info('Current apikey settings: ', langSettings) |
| 203 | + } |
| 204 | + |
| 205 | + expect(langSettings).toBeInstanceOf(Object) |
| 206 | + }) |
| 207 | +}) |
0 commit comments