-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add environment to plugin #443
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few minor comments 😄
app/network/network.js
Outdated
@@ -661,13 +662,13 @@ export async function send (requestId: string, environmentId: string) { | |||
return _actuallySend(renderedRequest, workspace, settings); | |||
} | |||
|
|||
async function _applyRequestPluginHooks (renderedRequest: RenderedRequest): Promise<RenderedRequest> { | |||
async function _applyRequestPluginHooks (renderedRequest: RenderedRequest, renderdContext: Object = {}): Promise<RenderedRequest> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default value = {}
for renderdContext
is not needed here since it is a required parameter. Also, it should be spelled renderedContext
😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got, I will fix it.
app/plugins/context/app.js
Outdated
@@ -3,7 +3,7 @@ import type {Plugin} from '../'; | |||
import * as electron from 'electron'; | |||
import {showAlert} from '../../ui/components/modals/index'; | |||
|
|||
export function init (plugin: Plugin): {app: Object} { | |||
export function init (plugin: Plugin, renderdContext: Object = {}): {app: Object} { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make more sense for the renderedContext
to be part of plugins/context/request.js
instead of here. The renderedContext
parameter should also be required (no default value).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only for request ? Maybe template tags also need this ability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are correct that environments might be useful for more than just request hooks. It may also be useful to have in the response hooks as well.
The main reason that I don't want it in app
is that app
is meant for application-level actions (eg. getting user input) so environment functionality doesn't fit very well.
I would be open to adding a context/environment.js
that is exposed in both request and response hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And maybe later I can also fix the template tags plugin API to use this as well 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will add it in request
instead of app
.
app/plugins/context/app.js
Outdated
@@ -17,6 +17,12 @@ export function init (plugin: Plugin): {app: Object} { | |||
throw new Error(`Unknown path name ${name}`); | |||
} | |||
}, | |||
getEnv (name: string): string | Object { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename these to getEnvironmentVariable
and getEnvironment
? Also, the return type should be any type that can be represented in JSON string | number | boolean | Object | Array<any> | null | undefined
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I will do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
undefined
looks like can't be used in type annotation, I faced this error
app/plugins/context/request.js:77
77: getEnvironment (): string | number | boolean | Object | Array<any> | null | undefined {
^^^^^^^^^ undefined. Ineligible value used in/as type annotation (did you forget 'typeof'?)
77: getEnvironment (): string | number | boolean | Object | Array<any> | null | undefined {
^^^^^^^^^ undefined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you might have to use the question mark instead.
?(string | number | boolean | Object | Array<any> | null)
app/plugins/context/app.js
Outdated
@@ -17,6 +17,12 @@ export function init (plugin: Plugin): {app: Object} { | |||
throw new Error(`Unknown path name ${name}`); | |||
} | |||
}, | |||
getEnv (name: string): string | Object { | |||
return (renderdContext && renderdContext[name]) || ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine just returning renderedContext[name]
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, I will do it.
app/plugins/context/request.js
Outdated
@@ -33,6 +33,9 @@ export function init (plugin: Plugin, renderedRequest: RenderedRequest): {reques | |||
return null; | |||
} | |||
}, | |||
getHeaders (): Array<RequestHeader>|null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the RequestHeader
type has some extra field in it that we don't want to share with the user, this could be changed to just return name
and value
:
getHeaders (): Array<{name: string, value: string}> {
return renderedRequest.headers.map(h => ({ name: h.name, value: h.value }));
},
This also ensures that we don't accidentally expose any new fields to plugins that are added internally.
Also, the null
type is not necessary because this will still return an empty array if there are no headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, and I made a mistake that this API should be getAllHeaders
, what do you think about that?
// Strip quotes off of the value | ||
info[match[1]] = match[2]; | ||
} | ||
const info = JSON.parse(stdout); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the old way can't handle these:
insomnia:
{ name: 'insomnia-qingstor',
description: 'QingStor signature plugin for insomnia.' },
maybe we should find a better way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, you are correct.
After some searching, I found that you can specify --json
flag on the npm show
to get JSON output. That should work with your code 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I just need to add --json
for the commands, great, I wiil do it.
@gschier All changes have been done except the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, almost there 😄
expect(Object.keys(result)).toEqual(['request']); | ||
expect(Object.keys(result.request)).toEqual([ | ||
'getId', | ||
'getName', | ||
'getUrl', | ||
'getMethod', | ||
'getHeader', | ||
'getAllHeaders', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make sure the new methods you added are tested in this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I will do it.
app/plugins/context/request.js
Outdated
@@ -33,6 +38,9 @@ export function init (plugin: Plugin, renderedRequest: RenderedRequest): {reques | |||
return null; | |||
} | |||
}, | |||
getAllHeaders (): Array<RequestHeader> | null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I like name getHeaders
like you had before, and I think you forgot to address this comment: #443 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, you are right, I wiil fix it.
Signed-off-by: Xuanwo <xuanwo.cn@gmail.com>
Signed-off-by: Xuanwo <xuanwo.cn@gmail.com>
Signed-off-by: Xuanwo <xuanwo.cn@gmail.com>
Is everything ready to go? |
Just heading to bed. I will take another look tomorrow. |
Wow, it's an another new day for me. 😃 , see you tomorrow (for your timezone). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, looks good now @Xuanwo! I'll make sure it gets into the next release 😄
As discussed in slack: https://getinsomnia.slack.com/archives/C4Z8M9E86/p1502912037000177, this pr intended to add these features:
With these improves:
npm show
's output, it's indeed a JSON content, we don't need to parse it with split in\n
.