Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: enable active global environment and map it to the sdk #7533

Merged
merged 12 commits into from
Jun 26, 2024
32 changes: 20 additions & 12 deletions packages/insomnia-sdk/src/objects/insomnia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class InsomniaObject {
private _test = test;

// TODO: follows will be enabled after Insomnia supports them
private _globals: Environment;
private globals: Environment;
private _iterationData: Environment;
private _settings: Settings;

Expand All @@ -53,7 +53,7 @@ export class InsomniaObject {
},
log: (...msgs: any[]) => void,
) {
this._globals = rawObj.globals;
this.globals = rawObj.globals;
this.environment = rawObj.environment;
this.baseEnvironment = rawObj.baseEnvironment;
this.collectionVariables = this.baseEnvironment; // collectionVariables is mapped to baseEnvironment
Expand Down Expand Up @@ -85,11 +85,6 @@ export class InsomniaObject {
return this._expect(exp);
}

// TODO: remove this after enabled globals
get globals() {
throw unsupportedError('globals', 'base environment');
}

// TODO: remove this after enabled iterationData
get iterationData() {
throw unsupportedError('iterationData', 'environment');
Expand All @@ -102,7 +97,7 @@ export class InsomniaObject {

toObject = () => {
return {
globals: this._globals.toObject(),
globals: this.globals.toObject(),
environment: this.environment.toObject(),
baseEnvironment: this.baseEnvironment.toObject(),
iterationData: this._iterationData.toObject(),
Expand All @@ -121,13 +116,26 @@ export async function initInsomniaObject(
rawObj: RequestContext,
log: (...args: any[]) => void,
) {
ihexxa marked this conversation as resolved.
Show resolved Hide resolved
const globals = new Environment('globals', rawObj.globals);
const environment = new Environment(rawObj.environmentName || '', rawObj.environment);
const baseEnvironment = new Environment(rawObj.baseEnvironmentName || '', rawObj.baseEnvironment);
// Mapping rule for the global environment:
// - when one global environment is selected, `globals` points to the selected one
// Potential mapping rule for the future:
// - The base global environment could also be introduced
const globals = new Environment('globals', rawObj.globals || {}); // could be undefined
// Mapping rule for the environment and base environment:
// - If base environment is selected, both `baseEnvironment` and `environment` point to the selected one.
// - If one sub environment is selected, `baseEnvironment` points to the base env and `environment` points to the selected one.
const baseEnvironment = new Environment(rawObj.baseEnvironment.name || '', rawObj.baseEnvironment.data);
// reuse baseEnvironment when the "selected envrionment" points to the base environment
const environment = rawObj.baseEnvironment.id === rawObj.environment.id ?
baseEnvironment :
new Environment(rawObj.environment.name || '', rawObj.environment.data);
if (rawObj.baseEnvironment.id === rawObj.environment.id) {
log('warning: No environment is selected, modification of insomnia.environment will be applied to the base environment.');
}
// TODO: update "iterationData" name when it is supported
const iterationData = new Environment('iterationData', rawObj.iterationData);
const cookies = new CookieObject(rawObj.cookieJar);
// TODO: update follows when post-request script and iterating are introduced
// TODO: update follows when post-request script and iterationData are introduced
const requestInfo = new RequestInfo({
eventName: 'prerequest',
iteration: 1,
Expand Down
14 changes: 10 additions & 4 deletions packages/insomnia-sdk/src/objects/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import { sendCurlAndWriteTimelineError, sendCurlAndWriteTimelineResponse } from
export interface RequestContext {
request: Request;
timelinePath: string;
environment?: object;
environmentName?: string;
baseEnvironment?: object;
baseEnvironmentName?: string;
environment: {
id: string;
name: string;
data: object;
};
baseEnvironment: {
id: string;
name: string;
data: object;
};
collectionVariables?: object;
globals?: object;
iterationData?: object;
Expand Down
50 changes: 37 additions & 13 deletions packages/insomnia-smoke-test/fixtures/pre-request-collection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ resources:
name: FolderWithEnv
description: ""
environment:
customValue: "fromFolder"
folderEnv: "fromFolder"
environmentPropertyOrder: null
metaSortKey: -1668533312225
_type: request_group
Expand Down Expand Up @@ -315,30 +315,24 @@ resources:
settingRebuildPath: true
settingFollowRedirects: global
preRequestScript: |-
insomnia.environment.set("environmentName", insomnia.environment.name);
insomnia.environment.set("baseEnvironmentName", insomnia.baseEnvironment.name);
insomnia.environment.set("collectionVariablesName", insomnia.collectionVariables.name);
// manipulation
insomnia.baseEnvironment.set('fromBaseEnv', 'baseEnv');
// insomnia.globals.set('fallbackToGlobal', 'fallbackToGlobal');
insomnia.baseEnvironment.set('fallbackToBase', 'fallbackToBase');
insomnia.baseEnvironment.set('scriptValue', 'fromBase');
insomnia.environment.set('scriptValue', 'fromEnv');
// "preDefinedValue" is already defined in the base environment modal.
// but it is rewritten here
insomnia.baseEnvironment.set('preDefinedValue', 'fromScript');
// "customValue" is already defined in the folder environment.
// "folderEnv" is already defined in the folder environment.
// folder version will override the following one
insomnia.baseEnvironment.set('customValue', 'fromScript');
insomnia.baseEnvironment.set('folderEnv', 'fromScript');
body:
mimeType: "application/json"
text: |-
{
"environmentName": "{{ _.environmentName }}",
"baseEnvironmentName": "{{ _.baseEnvironmentName }}",
"collectionVariablesName": "{{ _.collectionVariablesName }}",
"fromBaseEnv": "{{ _.fromBaseEnv }}",
"fallbackToBase": "{{ _.fallbackToBase }}",
"scriptValue": "{{ _.scriptValue }}",
"preDefinedValue": "{{ _.preDefinedValue }}",
"customValue": "{{ _.customValue }}"
"folderEnv": "{{ _.folderEnv }}"
}
_type: request
- _id: req_89dade2ee9ee42fbb22d588783a9df01
Expand Down Expand Up @@ -1170,3 +1164,33 @@ resources:
text: |-
{}
_type: request
- _id: req_89dade2ee9ee42fbb22d588783a9df19
parentId: fld_01de564274824ecaad272330339ea6b2
modified: 1636707449231
created: 1636141014552
url: http://127.0.0.1:4010/echo
name: can manipulate global envs
description: ""
method: POST
parameters: []
headers:
- name: 'Content-Type'
value: 'application/json'
authentication: {}
metaSortKey: -1636141014553
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
preRequestScript: |-
insomnia.globals.set('setByScript', 'setByScript');
body:
mimeType: "application/json"
text: |-
{
"setByScript": "{{ setByScript }}"
}
_type: request
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ test.describe('after-response script features tests', async () => {
const bodyJson = JSON.parse(rows.join(' '));

expect(bodyJson).toEqual({
// no environment is selected so the environment value is not persisted
// no environment is selected so the environment value will be persisted to the base environment
'__fromAfterScript1': 'baseEnvironment',
'__fromAfterScript2': 'collection',
'__fromAfterScript': 'environment',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ test.describe('pre-request features tests', async () => {
{
name: 'environments setting and overriding',
expectedBody: {
environmentName: '',
baseEnvironmentName: 'Base Environment',
collectionVariablesName: 'Base Environment',
fromBaseEnv: 'baseEnv',
// fallbackToGlobal: 'fallbackToGlobal',
fallbackToBase: 'fallbackToBase',
scriptValue: 'fromEnv',
preDefinedValue: 'fromScript',
customValue: 'fromFolder',
folderEnv: 'fromFolder',
},
},
{
Expand Down Expand Up @@ -176,6 +174,12 @@ test.describe('pre-request features tests', async () => {
asyncTaskDone: true,
},
},
// {
// name: 'can manipulate global envs',
// expectedBody: {
// setByScript: 'setByScript',
// },
// },
];

for (let i = 0; i < testCases.length; i++) {
Expand Down Expand Up @@ -428,9 +432,10 @@ test.describe('pre-request features tests', async () => {
const bodyJson = JSON.parse(rows.join(' '));

expect(bodyJson).toEqual({
// no environment is selected so the environment value is not persisted
// no environment is selected so the environment value will be persisted to the base environment
'__fromScript1': 'baseEnvironment',
'__fromScript2': 'collection',
'__fromScript': 'environment',
});
});
});
Expand Down
13 changes: 11 additions & 2 deletions packages/insomnia/src/hidden-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,21 @@ const runScript = async (

return {
...context,
environment: mutatedContextObject.environment,
baseEnvironment: mutatedContextObject.baseEnvironment,
environment: {
id: context.environment.id,
name: context.environment.name,
data: mutatedContextObject.environment,
},
baseEnvironment: {
id: context.baseEnvironment.id,
name: context.baseEnvironment.name,
data: mutatedContextObject.baseEnvironment,
},
request: updatedRequest,
settings: updatedSettings,
clientCertificates: updatedCertificates,
cookieJar: updatedCookieJar,
globals: mutatedContextObject.globals,
};
};

Expand Down
Loading
Loading