Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
pkskpro committed Jan 18, 2025
2 parents a502f2d + ff9dfca commit 55f7785
Show file tree
Hide file tree
Showing 22 changed files with 345 additions and 25 deletions.
15 changes: 14 additions & 1 deletion packages/cli/test/data/envs/petstore-imported.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
6 changes: 4 additions & 2 deletions packages/commons-server/src/libs/openapi-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ export class OpenAPIConverter {
port?: number
): Environment {
const newEnvironment = BuildEnvironment({
hasDefaultHeader: false,
hasContentTypeHeader: false,
hasCorsHeaders: true,
hasDefaultRoute: false,
port
});
Expand Down Expand Up @@ -224,7 +225,8 @@ export class OpenAPIConverter {
port?: number
): Environment {
const newEnvironment = BuildEnvironment({
hasDefaultHeader: false,
hasContentTypeHeader: false,
hasCorsHeaders: true,
hasDefaultRoute: false,
port
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { fromSafeString } from '../../utils';

const find = function (...args) {
const parameters = args.slice(0, -1);

if (parameters.length === 0) {
return '';
}

const [arr, ...filters] = parameters;

if (!(arr instanceof Array)) {
return '';
}
if (!arr.length || !filters.length) {
return undefined;
}

const validate = (payload, condition) => {
if (
condition !== null &&
typeof condition === 'object' &&
!Array.isArray(condition)
) {
if (typeof payload === 'object') {
const keys = Object.keys(condition);

return keys.every((k) =>
validate(payload[k], fromSafeString(condition[k]))
);
}

return false;
}

return payload === condition;
};

const and = (item, conditions, { or }) =>
conditions.every((subConditions) => {
if (Array.isArray(subConditions)) {
return or(item, subConditions);
}

return validate(item, subConditions);
});
const or = (item, conditions) =>
conditions.some((subConditions) => {
if (Array.isArray(subConditions)) {
return and(item, subConditions, { or });
}

return validate(item, subConditions);
});

return arr.find((item) => or(item, filters));
};

export default find;
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import domain from './domain';
import email from './email';
import eq from './eq';
import filter from './filter';
import find from './find';
import firstName from './firstName';
import float from './float';
import floor from './floor';
Expand Down Expand Up @@ -117,6 +118,7 @@ export const Helpers = {
email,
eq,
filter,
find,
firstName,
float,
floor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3126,6 +3126,53 @@ describe('Template parser', () => {
});
});

describe('Helper: find', () => {
it('should return the first item matching the condition', () => {
const parseResult = TemplateParser({
shouldOmitDataHelper: false,
content:
'{{{ stringify (find (array (object b="b1" a="a1") (object b="b2" a="a2") 3) (object b="b1")) }}}',
environment: {} as any,
processedDatabuckets: [],
globalVariables: {},
request: {} as any,
envVarsPrefix: ''
});
strictEqual(parseResult, JSON.stringify({ a: 'a1', b: 'b1' }, null, 2));
});

it('should return the first item matching nested values', () => {
const parseResult = TemplateParser({
shouldOmitDataHelper: false,
content:
'{{{ stringify (find (array (object parent=(object child="child-val") b="b1") (object parent=(object child="child-val2") b="b2") 2 3) (object parent=(object child="child-val"))) }}}',
environment: {} as any,
processedDatabuckets: [],
globalVariables: {},
request: {} as any,
envVarsPrefix: ''
});
strictEqual(
parseResult,
JSON.stringify({ b: 'b1', parent: { child: 'child-val' } }, null, 2)
);
});

it('should return undefined when no item matches the condition', () => {
const parseResult = TemplateParser({
shouldOmitDataHelper: false,
content:
'{{{ stringify (find (array (object parent=(object child="child-val") b="b1") (object parent=(object child="child-val2") b="b2") 2 3) (object parent="parent-val")) }}}',
environment: {} as any,
processedDatabuckets: [],
globalVariables: {},
request: {} as any,
envVarsPrefix: ''
});
strictEqual(parseResult, '');
});
});

describe('Helper: sort', () => {
it('should return correctly sorted array of numbers in ascending order', () => {
const parseResult = TemplateParser({
Expand Down
20 changes: 15 additions & 5 deletions packages/commons/src/libs/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,32 @@ export const BuildCallback = (): Callback => ({ ...CallbackDefault });
export const BuildEnvironment = (
params: {
hasDefaultRoute: boolean;
hasDefaultHeader: boolean;
hasContentTypeHeader: boolean;
hasCorsHeaders: boolean;
port?: number;
} = {
hasDefaultRoute: true,
hasDefaultHeader: true
hasContentTypeHeader: true,
hasCorsHeaders: true
}
): Environment => {
const newRoute = BuildHTTPRoute();

const headers: Header[] = [];

if (params.hasContentTypeHeader) {
headers.push(BuildHeader('Content-Type', 'application/json'));
}

if (params.hasCorsHeaders) {
headers.push(...CORSHeaders);
}

return {
...EnvironmentDefault,
port: params.port ?? EnvironmentDefault.port,
routes: params.hasDefaultRoute ? [newRoute] : [],
headers: params.hasDefaultHeader
? [BuildHeader('Content-Type', 'application/json'), ...CORSHeaders]
: [],
headers,
proxyReqHeaders: [BuildHeader()],
proxyResHeaders: [BuildHeader()],
rootChildren: params.hasDefaultRoute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const helpersAutocompletions = [
{ caption: 'concat', value: '{{concat value value}}', meta: '' },
{ caption: 'jwtPayload', value: "{{jwtPayload token 'key'}}", meta: '' },
{ caption: 'jwtHeader', value: "{{jwtHeader token 'key'}}", meta: '' },
{ caption: 'filter', value: "{{filter array 'key'}}", meta: '' },
{
caption: 'indexOf',
value: '{{indexOf value search startIndex}}',
Expand Down Expand Up @@ -138,6 +139,7 @@ export const helpersAutocompletions = [
{ caption: 'color', value: '{{color}}', meta: '' },
{ caption: 'hexColor', value: '{{hexColor}}', meta: '' },
{ caption: 'guid', value: '{{guid}}', meta: '' },
{ caption: 'uuid', value: '{{uuid}}', meta: '' },
{ caption: 'ipv4', value: '{{ipv4}}', meta: '' },
{ caption: 'ipv6', value: '{{ipv6}}', meta: '' },
{ caption: 'lorem', value: '{{lorem length}}', meta: '' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ export class EnvironmentsService extends Logger {
const newEnvironment = options.environment
? options.environment
: BuildEnvironment({
hasDefaultHeader: true,
hasContentTypeHeader: true,
hasCorsHeaders: true,
hasDefaultRoute: true,
port: this.dataService.getNewEnvironmentPort()
});
Expand Down Expand Up @@ -719,7 +720,8 @@ export class EnvironmentsService extends Logger {
if (!environment) {
environment = {
...BuildEnvironment({
hasDefaultHeader: true,
hasContentTypeHeader: true,
hasCorsHeaders: true,
hasDefaultRoute: true
}),
// provide a name or the filename (UUID) will be used
Expand Down Expand Up @@ -1267,7 +1269,8 @@ export class EnvironmentsService extends Logger {
setActive: true,
environment: {
...BuildEnvironment({
hasDefaultHeader: true,
hasContentTypeHeader: true,
hasCorsHeaders: true,
hasDefaultRoute: true,
port: this.dataService.getNewEnvironmentPort()
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6736,7 +6736,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4336,7 +4336,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Loading

0 comments on commit 55f7785

Please sign in to comment.