diff --git a/packages/gitbeaker-core/src/services/Commits.ts b/packages/gitbeaker-core/src/services/Commits.ts index c7d22369..a818dfe4 100644 --- a/packages/gitbeaker-core/src/services/Commits.ts +++ b/packages/gitbeaker-core/src/services/Commits.ts @@ -55,8 +55,8 @@ export interface CommitAction { } export interface GPGSignature { - signature_type: "PGP", - verification_status: "verified"|"unverified"; + signature_type: 'PGP'; + verification_status: 'verified' | 'unverified'; gpg_key_id: number; gpg_key_primary_keyid: string; gpg_key_user_name: string; @@ -66,8 +66,8 @@ export interface GPGSignature { } export interface X509Signature { - signature_type: "X509"; - verification_status: "verified"|"unverified", + signature_type: 'X509'; + verification_status: 'verified' | 'unverified'; x509_certificate: { id: number; subject: string; @@ -190,7 +190,11 @@ export class Commits extends BaseService { ); } - signature(projectId: string | number, sha: string, options?: BaseRequestOptions): Promise { + signature( + projectId: string | number, + sha: string, + options?: BaseRequestOptions, + ): Promise { const pId = encodeURIComponent(projectId); return RequestHelper.get( diff --git a/packages/gitbeaker-requester-utils/src/RequesterUtils.ts b/packages/gitbeaker-requester-utils/src/RequesterUtils.ts index e069f360..25a1aa56 100644 --- a/packages/gitbeaker-requester-utils/src/RequesterUtils.ts +++ b/packages/gitbeaker-requester-utils/src/RequesterUtils.ts @@ -78,13 +78,13 @@ export function createRequesterFn( optionsHandler, requestHandler, ): (serviceOptions: DefaultServiceOptions) => RequesterType { - const requester: RequesterType = {} as RequesterType; const methods = ['get', 'post', 'put', 'delete', 'stream']; return (serviceOptions) => { + const requester: RequesterType = {} as RequesterType; + methods.forEach((m) => { - /* eslint func-names:0 */ - requester[m] = function (endpoint: string, options: Record) { + requester[m] = (endpoint: string, options: Record) => { const requestOptions = optionsHandler(serviceOptions, { ...options, method: m }); return requestHandler(endpoint, requestOptions); @@ -110,6 +110,7 @@ export function modifyServices(services: T, customConfig: Record { updated[k] = extendClass(s, customConfig); + if (k === 'GroupMembers') console.log(updated[k]); }); return updated as T; diff --git a/packages/gitbeaker-requester-utils/test/unit/RequesterUtils.ts b/packages/gitbeaker-requester-utils/test/unit/RequesterUtils.ts index dbfc05b5..637f179d 100644 --- a/packages/gitbeaker-requester-utils/test/unit/RequesterUtils.ts +++ b/packages/gitbeaker-requester-utils/test/unit/RequesterUtils.ts @@ -1,6 +1,11 @@ /* eslint-disable max-classes-per-file */ import FormData from 'form-data'; -import { createRequesterFn, defaultOptionsHandler, modifyServices } from '../../src/RequesterUtils'; +import { + createRequesterFn, + defaultOptionsHandler, + modifyServices, + formatQuery, +} from '../../src/RequesterUtils'; const methods = ['get', 'put', 'delete', 'stream', 'post']; @@ -53,6 +58,12 @@ describe('defaultOptionsHandler', () => { expect(headers.sudo).toBe('testsudo'); }); + it('should assign the prefixUrl property if passed', async () => { + const { prefixUrl } = defaultOptionsHandler(serviceOptions); + + expect(prefixUrl).toBe('testurl'); + }); + it('should default searchParams to an empty string if undefined', async () => { const { searchParams } = defaultOptionsHandler(serviceOptions, { query: undefined, @@ -88,11 +99,11 @@ describe('createInstance', () => { requestTimeout: 50, }; - it('should have a createInstance function', async () => { + it('should have a createInstance function', () => { expect(createRequesterFn).toBeFunction(); }); - it('should return an object with function names equal to those in the methods array when the createInstance function is called', async () => { + it('should return an object with function names equal to those in the methods array when the createInstance function is called', () => { const requester = createRequesterFn(optionsHandler, handler)(serviceOptions); expect(requester).toContainAllKeys(methods); @@ -102,16 +113,44 @@ describe('createInstance', () => { }); }); - it('should call the handler with the correct endpoint when passed to any of the method functions', async () => { + it('should call the handler with the correct endpoint when passed to any of the method functions', () => { const testEndpoint = 'test endpoint'; const requester = createRequesterFn(optionsHandler, handler)(serviceOptions); methods.forEach((m) => { requester[m](testEndpoint, {}); + expect(optionsHandler).toBeCalledWith(serviceOptions, { method: m }); expect(handler).toBeCalledWith(testEndpoint, {}); }); }); + + it('should respect the closure variables', () => { + const serviceOptions1 = { + headers: { test: '5' }, + url: 'testurl', + rejectUnauthorized: false, + requestTimeout: 50, + }; + const serviceOptions2 = { + headers: { test: '5' }, + url: 'testurl2', + rejectUnauthorized: true, + requestTimeout: 100, + }; + + const requesterFn = createRequesterFn(optionsHandler, handler); + const requesterA = requesterFn(serviceOptions1); + const requesterB = requesterFn(serviceOptions2); + + requesterA.get('test'); + + expect(optionsHandler).toBeCalledWith(serviceOptions1, { method: 'get' }); + + requesterB.get('test'); + + expect(optionsHandler).toBeCalledWith(serviceOptions2, { method: 'get' }); + }); }); describe('modifyServices', () => { @@ -205,3 +244,17 @@ describe('modifyServices', () => { expect(b.x).toBe(5); }); }); + +describe('formatQuery', () => { + it('should decamelize keys and stringify the object', async () => { + const string = formatQuery({ test: 6 }); + + expect(string).toBe('test=6'); + }); + + it('should decamelize sub keys in not property and stringify the object', async () => { + const string = formatQuery({ test: 6, not: { test: 7 } }); + + expect(string).toBe('not=%7B%22test%22%3A7%7D&test=6'); + }); +});