Skip to content

Commit

Permalink
feat: add new proxy transform attributes (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Weber committed Nov 30, 2022
1 parent e8f2527 commit d03b044
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 18 deletions.
34 changes: 33 additions & 1 deletion src/common/utils.ts
Expand Up @@ -9,7 +9,7 @@ import os from 'os';
import { snakeCase } from 'snake-case';
import snakecaseKeys from 'snakecase-keys';
import type { RequestTransformers } from '@/service';
import type { Reactor, Token, TokenBase } from '@/types/models';
import type { Reactor, Proxy, Token, TokenBase } from '@/types/models';
import type {
ApplicationInfo,
ClientUserAgent,
Expand Down Expand Up @@ -61,6 +61,21 @@ const transformReactorRequestSnakeCase: AxiosTransformer = (
} as Reactor;
};

const transformProxyRequestSnakeCase: AxiosTransformer = (
proxy: Proxy
): Proxy | undefined => {
if (typeof proxy === 'undefined') {
return undefined;
}

return {
...snakecaseKeys(proxy, { deep: true }),
...(proxy.configuration !== undefined
? { configuration: proxy.configuration }
: {}),
} as Proxy;
};

const transformAtomicRequestSnakeCase: AxiosTransformer = <
T extends TokenBase,
S extends TokenBase
Expand Down Expand Up @@ -120,6 +135,21 @@ const transformReactorResponseCamelCase: AxiosTransformer = (
} as Reactor;
};

const transformProxyResponseCamelCase: AxiosTransformer = (
proxy: Proxy
): Proxy | undefined => {
if (typeof proxy === 'undefined') {
return undefined;
}

return {
...camelcaseKeys(proxy, { deep: true }),
...(proxy.configuration !== undefined
? { configuration: proxy.configuration }
: {}),
} as Proxy;
};

const transformResponseCamelCase: AxiosTransformer = <T, C>(
data: T
): C | undefined => {
Expand Down Expand Up @@ -370,10 +400,12 @@ export {
transformRequestSnakeCase,
proxyRaw,
transformReactorRequestSnakeCase,
transformProxyRequestSnakeCase,
transformAtomicRequestSnakeCase,
transformTokenRequestSnakeCase,
transformTokenResponseCamelCase,
transformReactorResponseCamelCase,
transformProxyResponseCamelCase,
transformResponseCamelCase,
transformAtomicResponseCamelCase,
dataExtractor,
Expand Down
28 changes: 27 additions & 1 deletion src/proxies/BasisTheoryProxies.ts
@@ -1,10 +1,36 @@
import type { AxiosTransformer } from 'axios';
import {
transformProxyResponseCamelCase,
transformProxyRequestSnakeCase,
} from '@/common/utils';
import { BasisTheoryService } from '@/service';
import type { BasisTheoryServiceOptions } from '@/service';
import { CrudBuilder } from '@/service/CrudBuilder';
import type { CreateProxy, UpdateProxy, Proxy } from '@/types/models';
import type { ListProxyQuery } from '@/types/sdk';

export const BasisTheoryProxies = new CrudBuilder(
class BasisTheoryProxies extends BasisTheoryService {}
class BasisTheoryProxies extends BasisTheoryService {
public constructor(options: BasisTheoryServiceOptions) {
const _options = {
...options,
};

// eslint-disable-next-line unicorn/prefer-spread
_options.transformRequest = ([] as AxiosTransformer[]).concat(
transformProxyRequestSnakeCase,
options.transformRequest || []
);

// eslint-disable-next-line unicorn/prefer-spread
_options.transformResponse = ([] as AxiosTransformer[]).concat(
transformProxyResponseCamelCase,
options.transformResponse || []
);

super(_options);
}
}
)
.create<Proxy, CreateProxy>()
.retrieve<Proxy>()
Expand Down
16 changes: 15 additions & 1 deletion src/types/models/proxies.ts
@@ -1,3 +1,4 @@
import type { Application } from './applications';
import type { Auditable } from './shared';

interface Proxy extends Auditable {
Expand All @@ -8,17 +9,30 @@ interface Proxy extends Auditable {
destinationUrl: string;
requestReactorId?: string;
responseReactorId?: string;
requestTransform?: ProxyTransform;
responseTransform?: ProxyTransform;
applicationId?: string;
configuration?: Record<string, string>;
requireAuth?: boolean;
}

interface ProxyTransform {
code: string;
}

type CreateProxy = Pick<
Proxy,
| 'name'
| 'destinationUrl'
| 'requestReactorId'
| 'responseReactorId'
| 'requestTransform'
| 'responseTransform'
| 'configuration'
| 'requireAuth'
>;
> & {
application?: Pick<Application, 'id'>;
};

type UpdateProxy = CreateProxy;

Expand Down
2 changes: 1 addition & 1 deletion src/types/models/reactors.ts
Expand Up @@ -8,7 +8,7 @@ interface Reactor extends Auditable {
name: string;
formula: ReactorFormula;
application?: Application;
configuration: Record<string, string>;
configuration?: Record<string, string>;
}

type CreateReactor = Pick<Reactor, 'name' | 'configuration'> & {
Expand Down
73 changes: 59 additions & 14 deletions test/proxies.test.ts
@@ -1,6 +1,7 @@
import type MockAdapter from 'axios-mock-adapter';
import { Chance } from 'chance';
import { BasisTheory } from '@/BasisTheory';
import { transformProxyRequestSnakeCase } from '@/common/utils';
import type { BasisTheory as IBasisTheory } from '@/types/sdk';
import { mockServiceClient, testCRUD } from './setup/utils';

Expand All @@ -23,23 +24,67 @@ describe('Proxies', () => {
});

describe('CRUD', () => {
const _chance = new Chance();

const createPayload = {
name: _chance.animal(),
destinationUrl: _chance.url(),
requestReactorId: _chance.guid(),
responseReactorId: _chance.guid(),
requestTransform: {
code: _chance.string(),
},
responseTransform: {
code: _chance.string(),
},
application: {
id: _chance.guid(),
},
configuration: {
// eslint-disable-next-line camelcase
snake_case: _chance.string(),
camelCase: _chance.string(),
},
requireAuth: _chance.bool(),
};

const updatePayload = {
name: _chance.animal(),
destinationUrl: _chance.url(),
requestReactorId: _chance.guid(),
responseReactorId: _chance.guid(),
requestTransform: {
code: _chance.word(),
},
responseTransform: {
code: _chance.word(),
},
application: {
id: _chance.guid(),
},
configuration: {
// eslint-disable-next-line camelcase
snake_case: _chance.string(),
camelCase: _chance.string(),
},
requireAuth: _chance.bool(),
};

const transformedCreatePayload = transformProxyRequestSnakeCase(
createPayload
);

const transformedUpdatePayload = transformProxyRequestSnakeCase(
updatePayload
);

testCRUD(() => ({
service: bt.proxies,
client,
createPayload: {
name: chance.animal(),
destinationUrl: chance.url(),
requestReactorId: chance.guid(),
responseReactorId: chance.guid(),
requireAuth: chance.bool(),
},
updatePayload: {
name: chance.animal(),
destinationUrl: chance.url(),
requestReactorId: chance.guid(),
responseReactorId: chance.guid(),
requireAuth: chance.bool(),
},
createPayload,
transformedCreatePayload,
updatePayload,
transformedUpdatePayload,
}));
});
});

0 comments on commit d03b044

Please sign in to comment.