Skip to content

Commit

Permalink
more typings, corrected payload and context preparation, updated depe…
Browse files Browse the repository at this point in the history
…ndencies (#132)
  • Loading branch information
bartes authored Mar 30, 2022
1 parent e89c6a9 commit f3a20c6
Show file tree
Hide file tree
Showing 24 changed files with 479 additions and 112 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 2.0.0

**BugFix:**

- [#132](https://github.com/castle/castle-node/pull/132)
* fixed how options in the context prepare are merged
* changed/fixed how payload prepare options are merge

## 1.1.1

**Enhancements**
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,26 @@
"@types/lodash.pickby": "4.6.6",
"@types/lodash.reduce": "4.6.6",
"@types/mockdate": "3.0.0",
"@types/node": "17.0.22",
"@types/node": "17.0.23",
"@types/node-fetch": "2.6.1",
"@types/pino": "7.0.5",
"fetch-mock": "9.11.0",
"husky": "7.0.4",
"jest": "27.5.1",
"mockdate": "3.0.5",
"prettier": "2.6.0",
"prettier": "2.6.1",
"rimraf": "3.0.2",
"source-map-support": "^0.5.21",
"ts-jest": "27.1.3",
"ts-jest": "27.1.4",
"ts-node": "10.7.0",
"tslint": "6.1.3",
"tslint-config-prettier": "1.18.0",
"tslint-config-standard": "9.0.0",
"typescript": "4.6.2"
"typescript": "4.6.3"
},
"dependencies": {
"abort-controller": "^3.0.0",
"express": "4.17.3",
"lodash.get": "^4.4.2",
"lodash.isempty": "^4.4.0",
"lodash.merge": "^4.6.2",
Expand Down
2 changes: 1 addition & 1 deletion src/client-id/services/client-id-extract.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingHttpHeaders } from 'http';
import type { IncomingHttpHeaders } from 'http2';
import { HeadersGetCookieService } from '../../headers/headers.module';

export const ClientIdExtractService = {
Expand Down
4 changes: 2 additions & 2 deletions src/command/services/command-authenticate.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Configuration } from '../../configuraton';
import { ContextSanitizeService } from '../../context/context.module';
import { Payload } from '../../payload/payload.module';
import type { Payload } from '../../payload/payload.module';
import { CommandGenerateService } from './command-generate.service';

export const CommandAuthenticateService = {
Expand All @@ -9,7 +9,7 @@ export const CommandAuthenticateService = {
return CommandGenerateService.call(
controller,
'authenticate',
{ ...options, ...{ context } },
{ ...options, ...{ context } } as Payload,
'POST',
configuration
);
Expand Down
2 changes: 1 addition & 1 deletion src/command/services/command-filter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const CommandFilterService = {
return CommandGenerateService.call(
controller,
'filter',
{ ...options, ...{ context } },
{ ...options, ...{ context } } as FilterPayload,
'POST',
configuration
);
Expand Down
9 changes: 7 additions & 2 deletions src/command/services/command-generate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import {
CoreGenerateDefaultHeadersService,
CoreGenerateRequestBody,
} from '../../core/core.module';
import { Payload } from '../../payload/payload.module';
import {
Payload,
LogPayload,
RiskPayload,
FilterPayload,
} from '../../payload/payload.module';

const combineURLs = (baseURL, relativeURL) => {
return baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '');
Expand All @@ -13,7 +18,7 @@ export const CommandGenerateService = {
call: (
controller,
path: string,
options: Payload,
options: Payload | LogPayload | RiskPayload | FilterPayload,
method: string,
configuration: Configuration
) => {
Expand Down
2 changes: 1 addition & 1 deletion src/command/services/command-log.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const CommandLogService = {
return CommandGenerateService.call(
controller,
'log',
{ ...options, ...{ context } },
{ ...options, ...{ context } } as LogPayload,
'POST',
configuration
);
Expand Down
5 changes: 3 additions & 2 deletions src/command/services/command-risk.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Configuration } from '../../configuraton';
import { ContextSanitizeService } from '../../context/context.module';
import { CommandGenerateService } from './command-generate.service';
import { RiskPayload } from '../../payload/payload.module';

export const CommandRiskService = {
call: (controller, options: any, configuration: Configuration) => {
call: (controller, options: RiskPayload, configuration: Configuration) => {
const context = ContextSanitizeService.call(options.context);
return CommandGenerateService.call(
controller,
'risk',
{ ...options, ...{ context } },
{ ...options, ...{ context } } as RiskPayload,
'POST',
configuration
);
Expand Down
2 changes: 1 addition & 1 deletion src/command/services/command-track.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const CommandTrackService = {
return CommandGenerateService.call(
controller,
'track',
{ ...options, ...{ context } },
{ ...options, ...{ context } } as Payload,
'POST',
configuration
);
Expand Down
13 changes: 9 additions & 4 deletions src/context/services/context-get-default.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { ClientIdExtractService } from '../../client-id/client-id.module';
import { HeadersExtractService } from '../../headers/headers.module';
import { IPsExtractService } from '../../ips/ips.module';
import { version } from '../../../package.json';
import type { Request as ExpressRequest } from 'express';

const requestContextData = (
request: any,
cookies: any,
request: ExpressRequest,
cookies: string | undefined,
configuration: Configuration
) => {
): { [key: string]: any } => {
if (isEmpty(request)) {
return {};
}
Expand All @@ -27,7 +28,11 @@ const requestContextData = (
};

export const ContextGetDefaultService = {
call: (request: any, cookies: any, configuration: Configuration) => {
call: (
request: ExpressRequest,
cookies: string | undefined,
configuration: Configuration
): { [key: string]: any } => {
return {
...pickBy(requestContextData(request, cookies, configuration)),
library: {
Expand Down
13 changes: 9 additions & 4 deletions src/context/services/context-prepare.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import merge from 'lodash.merge';
import { Configuration } from '../../configuraton';
import { ContextGetDefaultService } from './context-get-default.service';
import type { Request as ExpressRequest } from 'express';

export const ContextPrepareService = {
call: (requestContext: any, options: any, configuration: Configuration) => {
call: (
request: ExpressRequest,
options: undefined | { [key: string]: any },
configuration: Configuration
) => {
const defaultContext = ContextGetDefaultService.call(
requestContext,
options.cookies,
request,
options?.cookies,
configuration
);
return merge(requestContext, defaultContext);
return merge(defaultContext, options?.context);
},
};
2 changes: 1 addition & 1 deletion src/context/services/context-sanitize.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const ContextSanitizeService = {
call: (context: any) => {
call: (context: undefined | { [key: string]: any }) => {
if (!context) {
return {};
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/services/core-generate-request-body.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { ContextGetDefaultService } from '../../context/context.module';
import { Payload } from '../../payload/payload.module';

export const CoreGenerateRequestBody = {
call: ({ context, ...payloadOptions }: any, configuration: Configuration) => {
call: (
{ context, ...payloadOptions }: { [key: string]: any },
configuration: Configuration
) => {
const defaultContext = ContextGetDefaultService.call(
context,
'',
Expand Down
9 changes: 6 additions & 3 deletions src/headers/services/headers-extract.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { IncomingHttpHeaders } from 'http';
import type { IncomingHttpHeaders } from 'http2';
import reduce from 'lodash.reduce';
import { Configuration } from '../../configuraton';
import type { Configuration } from '../../configuraton';

const ALWAYS_ALLOWLISTED = ['user-agent'];
const ALWAYS_DENYLISTED = ['cookie', 'authorization'];

export const HeadersExtractService = {
call: (headers: IncomingHttpHeaders, configuration: Configuration) => {
call: (
headers: IncomingHttpHeaders,
configuration: Configuration
): { [key: string]: boolean | string } => {
return reduce(
headers,
(accumulator: object, value: string, key: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/ips/services/ips-extract.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingHttpHeaders } from 'http';
import type { IncomingHttpHeaders } from 'http2';
import { Configuration } from '../../configuraton';
import { TRUSTED_PROXIES } from '../../constants';

Expand Down
2 changes: 1 addition & 1 deletion src/payload/models/filter_payload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingHttpHeaders } from 'http2';
import type { IncomingHttpHeaders } from 'http2';

export interface FilterPayload {
request_token: string;
Expand Down
2 changes: 1 addition & 1 deletion src/payload/models/log_payload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingHttpHeaders } from 'http2';
import type { IncomingHttpHeaders } from 'http2';

export interface LogPayload {
request_token?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/payload/models/payload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingHttpHeaders } from 'http2';
import type { IncomingHttpHeaders } from 'http2';

export interface Payload {
event?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/payload/models/risk_payload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingHttpHeaders } from 'http2';
import type { IncomingHttpHeaders } from 'http2';

export interface RiskPayload {
request_token: string;
Expand Down
13 changes: 7 additions & 6 deletions src/payload/services/payload-prepare-service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import merge from 'lodash.merge';
import { Configuration } from '../../configuraton';
import { ContextPrepareService } from '../../context/context.module';
import type { Request } from 'express';

export const PayloadPrepareService = {
call: (
payloadOptions: any,
requestContext: any,
payloadParams: { [key: string]: any },
request: Request,
configuration: Configuration,
options: any = {}
options: { [key: string]: any } = {}
) => {
const context = ContextPrepareService.call(
requestContext,
merge(payloadOptions, options),
request,
merge(payloadParams, options),
configuration
);
return merge(payloadOptions, context);
return merge(payloadParams, { context });
},
};
14 changes: 10 additions & 4 deletions test/context/services/context-get-default.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ContextGetDefaultService } from '../../../src/context/context.module';
import { Configuration } from '../../../src/configuraton';
import { version } from '../../../package.json';
import type { Request as ExpressRequest } from 'express';

describe('ContextGetDefaultService', () => {
describe('call', () => {
Expand All @@ -24,16 +25,21 @@ describe('ContextGetDefaultService', () => {
allowlisted: [],
});

const context = {
client_id: 'client_id',
const mockRequest = {
headers: {
'x-forwarded-for': '1.2.3.4',
'x-castle-client-id': 'client_id',
cookies: 'client_id',
},
};
body: {},
} as ExpressRequest;

it('generates default context', () => {
const received = ContextGetDefaultService.call(context, {}, config);
const received = ContextGetDefaultService.call(
mockRequest,
undefined,
config
);
expect(received).toMatchObject(expected);
});
});
Expand Down
19 changes: 12 additions & 7 deletions test/context/services/context-prepare.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ContextPrepareService } from '../../../src/context/context.module';
import { Configuration } from '../../../src/configuraton';
import { version } from '../../../package.json';
import type { Request as ExpressRequest } from 'express';

describe('ContextPrepareService', () => {
describe('call', () => {
Expand All @@ -9,7 +10,7 @@ describe('ContextPrepareService', () => {
name: 'castle-node',
version,
},
client_id: 'abcd',
client_id: 'client_id',
active: true,
};

Expand All @@ -20,17 +21,21 @@ describe('ContextPrepareService', () => {
allowlisted: [],
});

const context = {
client_id: 'client_id',
active: true,
};

const options = {
cookies: '__cid=abcd;',
context: {
client_id: 'client_id',
active: true,
},
};

const mockRequest = {
headers: {},
body: {},
} as ExpressRequest;

it('generates context', () => {
const received = ContextPrepareService.call(context, options, config);
const received = ContextPrepareService.call(mockRequest, options, config);
expect(received).toMatchObject(expected);
});
});
Expand Down
Loading

0 comments on commit f3a20c6

Please sign in to comment.