Skip to content

Commit

Permalink
feat: added query params injection to api plugin url & headers
Browse files Browse the repository at this point in the history
  • Loading branch information
chesterkmr committed Nov 18, 2024
1 parent d25cf30 commit 742e726
Show file tree
Hide file tree
Showing 31 changed files with 2,304 additions and 655 deletions.
7 changes: 7 additions & 0 deletions apps/backoffice-v2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @ballerine/backoffice-v2

## 0.7.69

### Patch Changes

- @ballerine/workflow-browser-sdk@0.6.65
- @ballerine/workflow-node-sdk@0.6.65

## 0.7.68

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions apps/backoffice-v2/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ballerine/backoffice-v2",
"version": "0.7.68",
"version": "0.7.69",
"description": "Ballerine - Backoffice",
"homepage": "https://github.com/ballerine-io/ballerine",
"type": "module",
Expand Down Expand Up @@ -55,8 +55,8 @@
"@ballerine/common": "0.9.51",
"@ballerine/react-pdf-toolkit": "^1.2.44",
"@ballerine/ui": "^0.5.44",
"@ballerine/workflow-browser-sdk": "0.6.64",
"@ballerine/workflow-node-sdk": "0.6.64",
"@ballerine/workflow-browser-sdk": "0.6.65",
"@ballerine/workflow-node-sdk": "0.6.65",
"@botpress/webchat": "^2.1.10",
"@botpress/webchat-generator": "^0.2.9",
"@fontsource/inter": "^4.5.15",
Expand Down
6 changes: 6 additions & 0 deletions apps/kyb-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# kyb-app

## 0.3.80

### Patch Changes

- @ballerine/workflow-browser-sdk@0.6.65

## 0.3.79

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions apps/kyb-app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/kyb-app",
"private": true,
"version": "0.3.79",
"version": "0.3.80",
"type": "module",
"scripts": {
"dev": "vite",
Expand All @@ -18,7 +18,7 @@
"@ballerine/blocks": "0.2.26",
"@ballerine/ui": "0.5.44",
"@ballerine/common": "^0.9.51",
"@ballerine/workflow-browser-sdk": "0.6.64",
"@ballerine/workflow-browser-sdk": "0.6.65",
"@lukemorales/query-key-factory": "^1.0.3",
"@radix-ui/react-icons": "^1.3.0",
"@rjsf/core": "^5.9.0",
Expand Down
6 changes: 6 additions & 0 deletions examples/headless-example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ballerine/headless-example

## 0.3.64

### Patch Changes

- @ballerine/workflow-browser-sdk@0.6.65

## 0.3.63

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions examples/headless-example/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/headless-example",
"private": true,
"version": "0.3.63",
"version": "0.3.64",
"type": "module",
"scripts": {
"spellcheck": "cspell \"*\"",
Expand Down Expand Up @@ -35,7 +35,7 @@
},
"dependencies": {
"@ballerine/common": "0.9.51",
"@ballerine/workflow-browser-sdk": "0.6.64",
"@ballerine/workflow-browser-sdk": "0.6.65",
"@felte/reporter-svelte": "^1.1.5",
"@felte/validator-zod": "^1.0.13",
"@fontsource/inter": "^4.5.15",
Expand Down
6 changes: 6 additions & 0 deletions packages/workflow-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ballerine/workflow-core

## 0.6.65

### Patch Changes

- Added query to apiPlugin url & headers

## 0.6.64

### Patch Changes
Expand Down
6 changes: 5 additions & 1 deletion packages/workflow-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/workflow-core",
"author": "Ballerine <dev@ballerine.com>",
"version": "0.6.64",
"version": "0.6.65",
"description": "workflow-core",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
Expand Down Expand Up @@ -61,6 +61,8 @@
"@types/lodash.groupby": "^4.6.9",
"@types/lodash.maxby": "^4.6.9",
"@types/lodash.merge": "^4.6.9",
"@types/lodash.set": "^4.3.2",
"@types/lodash.get": "^4.4.2",
"@types/node": "^18.14.0",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
Expand All @@ -76,6 +78,8 @@
"eslint-plugin-unused-imports": "^2.0.0",
"fs-extra": "^11.1.0",
"lodash.merge": "^4.6.2",
"lodash.set": "^4.3.2",
"lodash.get": "^4.4.2",
"msw": "^1.2.2",
"nock": "^13.3.8",
"node-fetch": "^3.3.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, it } from 'vitest';
import { WorkflowRunner } from '../../workflow-runner';
import { WorkflowRunnerArgs } from '../../types';
import { ISerializableHttpPluginParams } from './types';
import { ProcessStatus } from '@ballerine/common';
import { describe, expect, it } from 'vitest';
import { WorkflowRunnerArgs } from '../../../types';
import { WorkflowRunner } from '../../../workflow-runner';
import { ISerializableHttpPluginParams } from '../types';

const createWorkflowRunner = (
definition: WorkflowRunnerArgs['definition'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AnyRecord, isErrorWithMessage, isObject } from '@ballerine/common';
import { logger } from '../../logger';
import { TContext, Transformer, Transformers, Validator } from '../../utils';
import { IApiPluginParams } from './types';
import { logger } from '../../../logger';
import { TContext, Transformer, Transformers, Validator } from '../../../utils';
import { IApiPluginParams } from '../types';
import { getGlobalQueryParams } from './helpers';

export class ApiPlugin {
public static pluginType = 'http';
Expand Down Expand Up @@ -116,6 +117,7 @@ export class ApiPlugin {
}

protected async _getPluginUrl(context: AnyRecord) {
const queryParams = getGlobalQueryParams();
let _url: string;

if (typeof this.url === 'string') {
Expand All @@ -139,6 +141,8 @@ export class ApiPlugin {
}
}

_url = await this.replaceVariablesFromContext(_url, { query: queryParams });

return await this.replaceAllVariables(_url, context);
}

Expand Down Expand Up @@ -251,7 +255,12 @@ export class ApiPlugin {
return { [returnArgKey]: isValid, errorMessage };
}

async composeRequestHeaders(headers: HeadersInit, context: TContext) {
async composeRequestHeaders(headers: HeadersInit, _context: TContext) {
const context = {
query: getGlobalQueryParams(),
..._context,
};

const headersEntries = await Promise.all(
Object.entries(headers).map(async header => [
header[0],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, expect, it } from 'vitest';
import { formatStringValues, getGlobalQueryParams, isBrowser } from './helpers';

describe('helpers', () => {
describe('isBrowser', () => {
it('should return true when window is defined', () => {
// Mock window object
global.window = {} as Window & typeof globalThis;

expect(isBrowser()).toBe(true);

delete global.window;
});

it('should return false when window is undefined', () => {
expect(isBrowser()).toBe(false);
});
});

describe('getGlobalQueryParams', () => {
it('should return empty object when not in browser', () => {
const result = getGlobalQueryParams();
expect(result).toEqual({});
});

it('should parse URL search params into object', () => {
// Mock window with location
global.window = {
location: {
search: '?key1=value1&key2=value2',
},
} as Window & typeof globalThis;

expect(getGlobalQueryParams()).toEqual({
key1: 'value1',
key2: 'value2',
});

delete global.window;
});
});

describe('formatStringValues', () => {
it('should return original string if no placeholders found', () => {
const str = 'Hello World';
const data = { name: 'John' };

expect(formatStringValues(str, data)).toBe(str);
});

it('should replace single placeholder with value', () => {
const str = 'Hello {name}';
const data = { name: 'John' };

expect(formatStringValues(str, data)).toBe('Hello John');
});

it('should replace multiple placeholders with values', () => {
const str = 'Hello {user.firstName} {user.lastName}';
const data = {
user: {
firstName: 'John',
lastName: 'Doe',
},
};

expect(formatStringValues(str, data)).toBe('Hello John Doe');
});

it('should keep placeholder if value not found in data', () => {
const str = 'Hello {name} {unknown}';
const data = { name: 'John' };

expect(formatStringValues(str, data)).toBe('Hello John {unknown}');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import get from 'lodash.get';

export const isBrowser = () => typeof window !== 'undefined';

export const getGlobalQueryParams = () => {
if (!isBrowser()) return {};

const searchParams = new URLSearchParams(window.location.search);
const paramsObject: Record<string, string> = {};

searchParams.forEach((value, key) => {
paramsObject[key] = value;
});

return paramsObject;
};

export const formatStringValues = (
str: string,
data: Record<string, string | Record<string, string>>,
) => {
// Find all placeholders in curly braces
const placeholders = str.match(/{(.*?)}/g);

if (!placeholders) return str;

let formattedString = str;

// Replace each placeholder with its value from data
for (const placeholder of placeholders) {
const path = placeholder.replace(/{|}/g, ''); // Remove curly braces
const value = get(data, path);

// Replace placeholder with value if found
if (value !== undefined) {
formattedString = formattedString.replace(placeholder, String(value));
}
}

return formattedString;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './api-plugin';
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AnyRecord } from '@ballerine/common';
import { logger } from '../../logger';
import { ApiPlugin } from './api-plugin/api-plugin';
import { BallerineApiPlugin, IBallerineApiPluginParams } from './ballerine-plugin';
import { IApiPluginParams } from './types';
import { ApiPlugin } from './api-plugin';
import { ApiEmailTemplates } from './vendor-consts';
import { BallerineApiPlugin, IBallerineApiPluginParams } from './ballerine-plugin';

export interface IBallerineEmailPluginParams {
pluginKind: 'template-email';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiPlugin, IApiPluginParams } from '.';
import { ApiBallerinePlugins, BALLERINE_API_PLUGIN_FACTORY } from './vendor-consts';
import { reqResTransformersObj } from '../../workflow-runner-utils';
import { ApiPlugin } from './api-plugin';
import { IApiPluginParams } from './types';
import { ApiBallerinePlugins, BALLERINE_API_PLUGIN_FACTORY } from './vendor-consts';

export interface IBallerineApiPluginParams {
pluginKind: ApiBallerinePlugins;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiPlugin } from './api-plugin';
import { IApiPluginParams } from './types';
import { AnyRecord } from '@ballerine/common';
import { logger } from '../../logger';
import { ApiPlugin } from './api-plugin/api-plugin';
import { IApiPluginParams } from './types';

export class EmailPlugin extends ApiPlugin {
public static pluginType = 'http';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export { ApiPlugin } from './api-plugin';
export { WebhookPlugin } from './webhook-plugin';
export { ApiPlugin } from './api-plugin/api-plugin';
export { DispatchEventPlugin } from './dispatch-event-plugin';
export type {
WebhookPluginParams,
IApiPluginParams,
ValidatableTransformer,
IterativePluginParams,
SerializableValidatableTransformer,
ValidatableTransformer,
WebhookPluginParams,
} from './types';
export { WebhookPlugin } from './webhook-plugin';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiPlugin } from './api-plugin';
import { AnyRecord } from '@ballerine/common';
import * as countries from 'i18n-iso-countries';
import { ApiPlugin } from './api-plugin/api-plugin';

// This is currently only uses only open corp.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AnyRecord } from '@ballerine/common';
import { ApiPlugin } from './api-plugin';
import { JsonSchemaValidator } from '../../utils/context-validator/json-schema-validator';
import { Validator } from '../../utils';
import { JsonSchemaValidator } from '../../utils/context-validator/json-schema-validator';
import { BallerineApiPlugin } from './ballerine-plugin';

const kycIndividualRequestSchema = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AnyRecord } from '@ballerine/common';
import { ApiPlugin } from './api-plugin';
import { JsonSchemaValidator } from '../../utils/context-validator/json-schema-validator';
import { Validator } from '../../utils';
import { JsonSchemaValidator } from '../../utils/context-validator/json-schema-validator';
import { ApiPlugin } from './api-plugin/api-plugin';
import { BallerineApiPlugin } from './ballerine-plugin';

const kycSessionRequestSchema = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AnyRecord, isErrorWithMessage, isObject } from '@ballerine/common';
import { State } from 'country-state-city';
import { alpha2ToAlpha3 } from 'i18n-iso-countries';
import { logger } from '../../logger';
import { TContext } from '../../utils/types';
import { ApiPlugin } from './api-plugin';
import { ApiPlugin } from './api-plugin/api-plugin';
import { IApiPluginParams } from './types';
import { State } from 'country-state-city';

export class MastercardMerchantScreeningPlugin extends ApiPlugin {
public static pluginType = 'http';
Expand Down
Loading

0 comments on commit 742e726

Please sign in to comment.