insomnia/packages/insomnia-app/app/plugins/context/response.js
Gregory Schier 549ce23ce8
Merge All Repositories into Monorepo for easier maintenance (#629)
* All projects into monorepo

* Update CI

* More CI updates

* Extracted a bunch of things into packages

* Publish

 - insomnia-plugin-base64@1.0.1
 - insomnia-plugin-default-headers@1.0.2
 - insomnia-plugin-file@1.0.1
 - insomnia-plugin-hash@1.0.1
 - insomnia-plugin-now@1.0.1
 - insomnia-plugin-request@1.0.1
 - insomnia-plugin-response@1.0.1
 - insomnia-plugin-uuid@1.0.1
 - insomnia-cookies@0.0.2
 - insomnia-importers@1.5.2
 - insomnia-prettify@0.0.3
 - insomnia-url@0.0.2
 - insomnia-xpath@0.0.2

* A bunch of small fixes

* Improved build script

* Fixed

* Merge dangling files

* Usability refactor

* Handle duplicate plugin names
2017-11-26 20:45:40 +00:00

72 lines
2.0 KiB
JavaScript

// @flow
import type {Plugin} from '../';
import type {ResponseHeader} from '../../models/response';
import * as models from '../../models/index';
import {Readable} from 'stream';
type MaybeResponse = {
parentId?: string,
statusCode?: number,
statusMessage?: string,
bytesRead?: number,
bytesContent?: number,
bodyPath?: string,
elapsedTime?: number,
headers?: Array<ResponseHeader>
}
export function init (
plugin: Plugin,
response: MaybeResponse,
bodyBuffer: Buffer | null = null
): {response: Object} {
if (!response) {
throw new Error('contexts.response initialized without response');
}
return {
response: {
// TODO: Make this work. Right now it doesn't because _id is
// not generated in network.js
// getId () {
// return response.parentId;
// },
getRequestId (): string {
return response.parentId || '';
},
getStatusCode (): number {
return response.statusCode || 0;
},
getStatusMessage (): string {
return response.statusMessage || '';
},
getBytesRead (): number {
return response.bytesRead || 0;
},
getTime (): number {
return response.elapsedTime || 0;
},
getBody (): Buffer | null {
return models.response.getBodyBuffer(response);
},
getBodyStream (): Readable | null {
return models.response.getBodyStream(response);
},
getHeader (name: string): string | Array<string> | null {
const headers = response.headers || [];
const matchedHeaders = headers.filter(h => h.name.toLowerCase() === name.toLowerCase());
if (matchedHeaders.length > 1) {
return matchedHeaders.map(h => h.value);
} else if (matchedHeaders.length === 1) {
return matchedHeaders[0].value;
} else {
return null;
}
},
hasHeader (name: string): boolean {
return this.getHeader(name) !== null;
}
}
};
}