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

Merged
merged 3 commits into from Aug 21, 2017

Conversation

Projects
None yet
2 participants
@Xuanwo
Contributor

Xuanwo commented Aug 17, 2017

As discussed in slack: https://getinsomnia.slack.com/archives/C4Z8M9E86/p1502912037000177, this pr intended to add these features:

  • Allow plugin to access current context, so plugin's can get environment data now.
  • Add getAllHeaders API for context request

With these improves:

  • Use JSON to parse npm show's output, it's indeed a JSON content, we don't need to parse it with split in \n.
@gschier

A few minor comments 😄

@@ -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> {

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

The default value = {} for renderdContext is not needed here since it is a required parameter. Also, it should be spelled renderedContext 😄

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

Got, I will fix it.

@@ -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} {

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

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).

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

Only for request ? Maybe template tags also need this ability.

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

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.

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

And maybe later I can also fix the template tags plugin API to use this as well 😄

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

OK, I will add it in request instead of app.

@@ -17,6 +17,12 @@ export function init (plugin: Plugin): {app: Object} {
throw new Error(`Unknown path name ${name}`);
}
},
getEnv (name: string): string | Object {

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

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.

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

Yep, I will do it.

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

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

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

Oh, you might have to use the question mark instead.

?(string | number | boolean | Object | Array<any> | null)
@@ -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]) || '';

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

I'm fine just returning renderedContext[name] here.

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

Great, I will do it.

@@ -33,6 +33,9 @@ export function init (plugin: Plugin, renderedRequest: RenderedRequest): {reques
return null;
}
},
getHeaders (): Array<RequestHeader>|null {

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

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.

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

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);

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

Does this work? npm show seemed to return JS objects, not JSON.

image

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

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.

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

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 👍

https://docs.npmjs.com/cli/view

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

So I just need to add --json for the commands, great, I wiil do it.

@Xuanwo

This comment has been minimized.

Contributor

Xuanwo commented Aug 17, 2017

@gschier All changes have been done except the npm show's one.

@gschier

Nice, almost there 😄

expect(Object.keys(result)).toEqual(['request']);
expect(Object.keys(result.request)).toEqual([
'getId',
'getName',
'getUrl',
'getMethod',
'getHeader',
'getAllHeaders',

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

Can you make sure the new methods you added are tested in this file?

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

Sure, I will do it.

@@ -33,6 +38,9 @@ export function init (plugin: Plugin, renderedRequest: RenderedRequest): {reques
return null;
}
},
getAllHeaders (): Array<RequestHeader> | null {

This comment has been minimized.

@gschier

gschier Aug 17, 2017

Collaborator

I think I like name getHeaders like you had before, and I think you forgot to address this comment: #443 (comment)

This comment has been minimized.

@Xuanwo

Xuanwo Aug 17, 2017

Contributor

Yep, you are right, I wiil fix it.

Xuanwo added some commits Aug 17, 2017

Allow request in plugin to access current context
Signed-off-by: Xuanwo <xuanwo.cn@gmail.com>
Add getAllHeaders API for context request
Signed-off-by: Xuanwo <xuanwo.cn@gmail.com>
Use JSON to parse npm show's output
Signed-off-by: Xuanwo <xuanwo.cn@gmail.com>
@Xuanwo

This comment has been minimized.

Contributor

Xuanwo commented Aug 18, 2017

Is everything ready to go?

@gschier

This comment has been minimized.

Collaborator

gschier commented Aug 18, 2017

Just heading to bed. I will take another look tomorrow.

@Xuanwo

This comment has been minimized.

Contributor

Xuanwo commented Aug 18, 2017

Wow, it's an another new day for me.

😃 , see you tomorrow (for your timezone).

@gschier

Awesome, looks good now @Xuanwo! I'll make sure it gets into the next release 😄

@gschier gschier merged commit e805d86 into getinsomnia:develop Aug 21, 2017

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment