@actions/http-client 1.0.10 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,18 +1,11 @@
1
+ # `@actions/http-client`
1
2
 
2
- <p align="center">
3
- <img src="actions.png">
4
- </p>
5
-
6
- # Actions Http-Client
7
-
8
- [![Http Status](https://github.com/actions/http-client/workflows/http-tests/badge.svg)](https://github.com/actions/http-client/actions)
9
-
10
- A lightweight HTTP client optimized for use with actions, TypeScript with generics and async await.
3
+ A lightweight HTTP client optimized for building actions.
11
4
 
12
5
  ## Features
13
6
 
14
7
  - HTTP client with TypeScript generics and async/await/Promises
15
- - Typings included so no need to acquire separately (great for intellisense and no versioning drift)
8
+ - Typings included!
16
9
  - [Proxy support](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/about-self-hosted-runners#using-a-proxy-server-with-self-hosted-runners) just works with actions and the runner
17
10
  - Targets ES2019 (runner runs actions with node 12+). Only supported on node 12+.
18
11
  - Basic, Bearer and PAT Support out of the box. Extensible handlers for others.
@@ -28,7 +21,7 @@ npm install @actions/http-client --save
28
21
 
29
22
  ## Samples
30
23
 
31
- See the [HTTP](./__tests__) tests for detailed examples.
24
+ See the [tests](./__tests__) for detailed examples.
32
25
 
33
26
  ## Errors
34
27
 
@@ -39,13 +32,13 @@ The HTTP client does not throw unless truly exceptional.
39
32
  * A request that successfully executes resulting in a 404, 500 etc... will return a response object with a status code and a body.
40
33
  * Redirects (3xx) will be followed by default.
41
34
 
42
- See [HTTP tests](./__tests__) for detailed examples.
35
+ See the [tests](./__tests__) for detailed examples.
43
36
 
44
37
  ## Debugging
45
38
 
46
39
  To enable detailed console logging of all HTTP requests and responses, set the NODE_DEBUG environment varible:
47
40
 
48
- ```
41
+ ```shell
49
42
  export NODE_DEBUG=http
50
43
  ```
51
44
 
@@ -63,17 +56,18 @@ We welcome PRs. Please create an issue and if applicable, a design before proce
63
56
 
64
57
  once:
65
58
 
66
- ```bash
67
- $ npm install
59
+ ```
60
+ npm install
68
61
  ```
69
62
 
70
63
  To build:
71
64
 
72
- ```bash
73
- $ npm run build
65
+ ```
66
+ npm run build
74
67
  ```
75
68
 
76
69
  To run all tests:
77
- ```bash
78
- $ npm test
70
+
71
+ ```
72
+ npm test
79
73
  ```
package/lib/auth.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ /// <reference types="node" />
2
+ import * as http from 'http';
3
+ import * as ifm from './interfaces';
4
+ import { HttpClientResponse } from './index';
5
+ export declare class BasicCredentialHandler implements ifm.RequestHandler {
6
+ username: string;
7
+ password: string;
8
+ constructor(username: string, password: string);
9
+ prepareRequest(options: http.RequestOptions): void;
10
+ canHandleAuthentication(): boolean;
11
+ handleAuthentication(): Promise<HttpClientResponse>;
12
+ }
13
+ export declare class BearerCredentialHandler implements ifm.RequestHandler {
14
+ token: string;
15
+ constructor(token: string);
16
+ prepareRequest(options: http.RequestOptions): void;
17
+ canHandleAuthentication(): boolean;
18
+ handleAuthentication(): Promise<HttpClientResponse>;
19
+ }
20
+ export declare class PersonalAccessTokenCredentialHandler implements ifm.RequestHandler {
21
+ token: string;
22
+ constructor(token: string);
23
+ prepareRequest(options: http.RequestOptions): void;
24
+ canHandleAuthentication(): boolean;
25
+ handleAuthentication(): Promise<HttpClientResponse>;
26
+ }
package/lib/auth.js ADDED
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PersonalAccessTokenCredentialHandler = exports.BearerCredentialHandler = exports.BasicCredentialHandler = void 0;
13
+ class BasicCredentialHandler {
14
+ constructor(username, password) {
15
+ this.username = username;
16
+ this.password = password;
17
+ }
18
+ prepareRequest(options) {
19
+ if (!options.headers) {
20
+ throw Error('The request has no headers');
21
+ }
22
+ options.headers['Authorization'] = `Basic ${Buffer.from(`${this.username}:${this.password}`).toString('base64')}`;
23
+ }
24
+ // This handler cannot handle 401
25
+ canHandleAuthentication() {
26
+ return false;
27
+ }
28
+ handleAuthentication() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ throw new Error('not implemented');
31
+ });
32
+ }
33
+ }
34
+ exports.BasicCredentialHandler = BasicCredentialHandler;
35
+ class BearerCredentialHandler {
36
+ constructor(token) {
37
+ this.token = token;
38
+ }
39
+ // currently implements pre-authorization
40
+ // TODO: support preAuth = false where it hooks on 401
41
+ prepareRequest(options) {
42
+ if (!options.headers) {
43
+ throw Error('The request has no headers');
44
+ }
45
+ options.headers['Authorization'] = `Bearer ${this.token}`;
46
+ }
47
+ // This handler cannot handle 401
48
+ canHandleAuthentication() {
49
+ return false;
50
+ }
51
+ handleAuthentication() {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ throw new Error('not implemented');
54
+ });
55
+ }
56
+ }
57
+ exports.BearerCredentialHandler = BearerCredentialHandler;
58
+ class PersonalAccessTokenCredentialHandler {
59
+ constructor(token) {
60
+ this.token = token;
61
+ }
62
+ // currently implements pre-authorization
63
+ // TODO: support preAuth = false where it hooks on 401
64
+ prepareRequest(options) {
65
+ if (!options.headers) {
66
+ throw Error('The request has no headers');
67
+ }
68
+ options.headers['Authorization'] = `Basic ${Buffer.from(`PAT:${this.token}`).toString('base64')}`;
69
+ }
70
+ // This handler cannot handle 401
71
+ canHandleAuthentication() {
72
+ return false;
73
+ }
74
+ handleAuthentication() {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ throw new Error('not implemented');
77
+ });
78
+ }
79
+ }
80
+ exports.PersonalAccessTokenCredentialHandler = PersonalAccessTokenCredentialHandler;
81
+ //# sourceMappingURL=auth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":";;;;;;;;;;;;AAIA,MAAa,sBAAsB;IAIjC,YAAY,QAAgB,EAAE,QAAgB;QAC5C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAC1B,CAAC;IAED,cAAc,CAAC,OAA4B;QACzC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;YACpB,MAAM,KAAK,CAAC,4BAA4B,CAAC,CAAA;SAC1C;QACD,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC,GAAG,SAAS,MAAM,CAAC,IAAI,CACrD,GAAG,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,QAAQ,EAAE,CACpC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAA;IACxB,CAAC;IAED,iCAAiC;IACjC,uBAAuB;QACrB,OAAO,KAAK,CAAA;IACd,CAAC;IAEK,oBAAoB;;YACxB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAA;QACpC,CAAC;KAAA;CACF;AA1BD,wDA0BC;AAED,MAAa,uBAAuB;IAGlC,YAAY,KAAa;QACvB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IAED,yCAAyC;IACzC,sDAAsD;IACtD,cAAc,CAAC,OAA4B;QACzC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;YACpB,MAAM,KAAK,CAAC,4BAA4B,CAAC,CAAA;SAC1C;QACD,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC,GAAG,UAAU,IAAI,CAAC,KAAK,EAAE,CAAA;IAC3D,CAAC;IAED,iCAAiC;IACjC,uBAAuB;QACrB,OAAO,KAAK,CAAA;IACd,CAAC;IAEK,oBAAoB;;YACxB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAA;QACpC,CAAC;KAAA;CACF;AAxBD,0DAwBC;AAED,MAAa,oCAAoC;IAI/C,YAAY,KAAa;QACvB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IAED,yCAAyC;IACzC,sDAAsD;IACtD,cAAc,CAAC,OAA4B;QACzC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;YACpB,MAAM,KAAK,CAAC,4BAA4B,CAAC,CAAA;SAC1C;QACD,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC,GAAG,SAAS,MAAM,CAAC,IAAI,CACrD,OAAO,IAAI,CAAC,KAAK,EAAE,CACpB,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAA;IACxB,CAAC;IAED,iCAAiC;IACjC,uBAAuB;QACrB,OAAO,KAAK,CAAA;IACd,CAAC;IAEK,oBAAoB;;YACxB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAA;QACpC,CAAC;KAAA;CACF;AA3BD,oFA2BC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,123 @@
1
+ /// <reference types="node" />
2
+ import * as http from 'http';
3
+ import * as ifm from './interfaces';
4
+ export declare enum HttpCodes {
5
+ OK = 200,
6
+ MultipleChoices = 300,
7
+ MovedPermanently = 301,
8
+ ResourceMoved = 302,
9
+ SeeOther = 303,
10
+ NotModified = 304,
11
+ UseProxy = 305,
12
+ SwitchProxy = 306,
13
+ TemporaryRedirect = 307,
14
+ PermanentRedirect = 308,
15
+ BadRequest = 400,
16
+ Unauthorized = 401,
17
+ PaymentRequired = 402,
18
+ Forbidden = 403,
19
+ NotFound = 404,
20
+ MethodNotAllowed = 405,
21
+ NotAcceptable = 406,
22
+ ProxyAuthenticationRequired = 407,
23
+ RequestTimeout = 408,
24
+ Conflict = 409,
25
+ Gone = 410,
26
+ TooManyRequests = 429,
27
+ InternalServerError = 500,
28
+ NotImplemented = 501,
29
+ BadGateway = 502,
30
+ ServiceUnavailable = 503,
31
+ GatewayTimeout = 504
32
+ }
33
+ export declare enum Headers {
34
+ Accept = "accept",
35
+ ContentType = "content-type"
36
+ }
37
+ export declare enum MediaTypes {
38
+ ApplicationJson = "application/json"
39
+ }
40
+ /**
41
+ * Returns the proxy URL, depending upon the supplied url and proxy environment variables.
42
+ * @param serverUrl The server URL where the request will be sent. For example, https://api.github.com
43
+ */
44
+ export declare function getProxyUrl(serverUrl: string): string;
45
+ export declare class HttpClientError extends Error {
46
+ constructor(message: string, statusCode: number);
47
+ statusCode: number;
48
+ result?: any;
49
+ }
50
+ export declare class HttpClientResponse {
51
+ constructor(message: http.IncomingMessage);
52
+ message: http.IncomingMessage;
53
+ readBody(): Promise<string>;
54
+ }
55
+ export declare function isHttps(requestUrl: string): boolean;
56
+ export declare class HttpClient {
57
+ userAgent: string | undefined;
58
+ handlers: ifm.RequestHandler[];
59
+ requestOptions: ifm.RequestOptions | undefined;
60
+ private _ignoreSslError;
61
+ private _socketTimeout;
62
+ private _allowRedirects;
63
+ private _allowRedirectDowngrade;
64
+ private _maxRedirects;
65
+ private _allowRetries;
66
+ private _maxRetries;
67
+ private _agent;
68
+ private _proxyAgent;
69
+ private _keepAlive;
70
+ private _disposed;
71
+ constructor(userAgent?: string, handlers?: ifm.RequestHandler[], requestOptions?: ifm.RequestOptions);
72
+ options(requestUrl: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
73
+ get(requestUrl: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
74
+ del(requestUrl: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
75
+ post(requestUrl: string, data: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
76
+ patch(requestUrl: string, data: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
77
+ put(requestUrl: string, data: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
78
+ head(requestUrl: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
79
+ sendStream(verb: string, requestUrl: string, stream: NodeJS.ReadableStream, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
80
+ /**
81
+ * Gets a typed object from an endpoint
82
+ * Be aware that not found returns a null. Other errors (4xx, 5xx) reject the promise
83
+ */
84
+ getJson<T>(requestUrl: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<ifm.TypedResponse<T>>;
85
+ postJson<T>(requestUrl: string, obj: any, additionalHeaders?: http.OutgoingHttpHeaders): Promise<ifm.TypedResponse<T>>;
86
+ putJson<T>(requestUrl: string, obj: any, additionalHeaders?: http.OutgoingHttpHeaders): Promise<ifm.TypedResponse<T>>;
87
+ patchJson<T>(requestUrl: string, obj: any, additionalHeaders?: http.OutgoingHttpHeaders): Promise<ifm.TypedResponse<T>>;
88
+ /**
89
+ * Makes a raw http request.
90
+ * All other methods such as get, post, patch, and request ultimately call this.
91
+ * Prefer get, del, post and patch
92
+ */
93
+ request(verb: string, requestUrl: string, data: string | NodeJS.ReadableStream | null, headers?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
94
+ /**
95
+ * Needs to be called if keepAlive is set to true in request options.
96
+ */
97
+ dispose(): void;
98
+ /**
99
+ * Raw request.
100
+ * @param info
101
+ * @param data
102
+ */
103
+ requestRaw(info: ifm.RequestInfo, data: string | NodeJS.ReadableStream | null): Promise<HttpClientResponse>;
104
+ /**
105
+ * Raw request with callback.
106
+ * @param info
107
+ * @param data
108
+ * @param onResult
109
+ */
110
+ requestRawWithCallback(info: ifm.RequestInfo, data: string | NodeJS.ReadableStream | null, onResult: (err?: Error, res?: HttpClientResponse) => void): void;
111
+ /**
112
+ * Gets an http agent. This function is useful when you need an http agent that handles
113
+ * routing through a proxy server - depending upon the url and proxy environment variables.
114
+ * @param serverUrl The server URL where the request will be sent. For example, https://api.github.com
115
+ */
116
+ getAgent(serverUrl: string): http.Agent;
117
+ private _prepareRequest;
118
+ private _mergeHeaders;
119
+ private _getExistingOrDefaultHeader;
120
+ private _getAgent;
121
+ private _performExponentialBackoff;
122
+ private _processResponse;
123
+ }