first commit

This commit is contained in:
2026-04-09 13:05:27 +02:00
commit 3bbd7d6413
3084 changed files with 84284 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
Copyright (c) 2014 Forbes Lindesay
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,44 @@
# http-response-object
A simple object to represent an http response (with flow and typescript types)
[![Build Status](https://img.shields.io/travis/ForbesLindesay/http-response-object/master.svg)](https://travis-ci.org/ForbesLindesay/http-response-object)
[![Dependency Status](https://img.shields.io/david/ForbesLindesay/http-response-object.svg)](https://david-dm.org/ForbesLindesay/http-response-object)
[![NPM version](https://img.shields.io/npm/v/http-response-object.svg)](https://www.npmjs.org/package/http-response-object)
## Installation
npm install http-response-object
## Usage
```js
var Response = require('http-response-object');
var res = new Response(200, {}, new Buffer('A ok'), 'http://example.com');
//res.statusCode === 200
//res.headers === {}
//res.body === new Buffer('A ok')
//res.url === 'http://example.com'
res.getBody();
// => new Buffer('A ok')
var res = new Response(404, {'Header': 'value'}, new Buffer('Wheres this page'), 'http://example.com');
//res.statusCode === 404
//res.headers === {header: 'value'}
//res.body === new Buffer('Wheres this page')
//res.url === 'http://example.com'
res.getBody();
// => throws error with `statusCode`, `headers`, `body` and `url` properties copied from the response
```
## Properties
- `statusCode`: Number - the status code of the response
- `headers`: Object - the headers of the response. The keys are automatically made lower case.
- `body`: Buffer | String - the body of the response. Should be a buffer on the server side, but may be a simple string for lighter weight clients.
- `url`: String - the url that was requested. If there were redirects, this should be the last url to get requested.
## License
MIT

View File

@@ -0,0 +1,3 @@
export declare type Headers = {
readonly [name: string]: string | string[];
};

View File

@@ -0,0 +1,2 @@
"use strict";
exports.__esModule = true;

View File

@@ -0,0 +1,5 @@
// @flow
// Generated using flowgen2
type Headers = {+[name: string]: string | Array<string>};
export type {Headers};

View File

@@ -0,0 +1,15 @@
import { IncomingHttpHeaders } from 'http';
/**
* A response from a web request
*/
declare class Response<TBody> {
readonly statusCode: number;
readonly headers: IncomingHttpHeaders;
readonly body: TBody;
readonly url: string;
constructor(statusCode: number, headers: IncomingHttpHeaders, body: TBody, url: string);
isError(): boolean;
getBody(encoding: string): string;
getBody(): TBody;
}
export = Response;

View File

@@ -0,0 +1,60 @@
"use strict";
/**
* A response from a web request
*/
var Response = /** @class */ (function () {
function Response(statusCode, headers, body, url) {
if (typeof statusCode !== 'number') {
throw new TypeError('statusCode must be a number but was ' + typeof statusCode);
}
if (headers === null) {
throw new TypeError('headers cannot be null');
}
if (typeof headers !== 'object') {
throw new TypeError('headers must be an object but was ' + typeof headers);
}
this.statusCode = statusCode;
var headersToLowerCase = {};
for (var key in headers) {
headersToLowerCase[key.toLowerCase()] = headers[key];
}
this.headers = headersToLowerCase;
this.body = body;
this.url = url;
}
Response.prototype.isError = function () {
return this.statusCode === 0 || this.statusCode >= 400;
};
Response.prototype.getBody = function (encoding) {
if (this.statusCode === 0) {
var err = new Error('This request to ' +
this.url +
' resulted in a status code of 0. This usually indicates some kind of network error in a browser (e.g. CORS not being set up or the DNS failing to resolve):\n' +
this.body.toString());
err.statusCode = this.statusCode;
err.headers = this.headers;
err.body = this.body;
err.url = this.url;
throw err;
}
if (this.statusCode >= 300) {
var err = new Error('Server responded to ' +
this.url +
' with status code ' +
this.statusCode +
':\n' +
this.body.toString());
err.statusCode = this.statusCode;
err.headers = this.headers;
err.body = this.body;
err.url = this.url;
throw err;
}
if (!encoding || typeof this.body === 'string') {
return this.body;
}
return this.body.toString(encoding);
};
return Response;
}());
module.exports = Response;

View File

@@ -0,0 +1,22 @@
// @flow
// Generated using flowgen2
type IncomingHttpHeaders = Object;
declare class Response<TBody> {
+statusCode: number;
+headers: IncomingHttpHeaders;
+body: TBody;
+url: string;
constructor(
statusCode: number,
headers: IncomingHttpHeaders,
body: TBody,
url: string,
): void;
isError(): boolean;
getBody(encoding: string): string;
getBody(): TBody;
}
module.exports = Response;

View File

@@ -0,0 +1,35 @@
{
"name": "http-response-object",
"version": "3.0.2",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib"
],
"description": "A simple object to represent an http response",
"keywords": [
"http",
"https",
"response",
"request"
],
"scripts": {
"prepublishOnly": "npm run build",
"build": "tsc && flowgen lib/**/*",
"pretest": "npm run build",
"test": "node test"
},
"repository": {
"type": "git",
"url": "https://github.com/ForbesLindesay/http-response-object.git"
},
"author": "ForbesLindesay",
"license": "MIT",
"dependencies": {
"@types/node": "^10.0.3"
},
"devDependencies": {
"flowgen2": "^2.2.1",
"typescript": "^2.3.4"
}
}