@octokit/endpoint
Advanced tools
Comparing version 1.3.0 to 2.0.0
@@ -17,3 +17,3 @@ module.exports = endpointWithDefaults | ||
// lowercase header names | ||
// lowercase header names before merging with defaults to avoid duplicates | ||
if (options.headers) { | ||
@@ -28,8 +28,8 @@ options.headers = Object.keys(options.headers).reduce((newObj, key) => { | ||
let body = options.body | ||
let method = options.method.toLowerCase() | ||
let baseUrl = options.baseUrl | ||
// https://fetch.spec.whatwg.org/#methods | ||
let method = options.method.toUpperCase() | ||
let url = options.url | ||
let headers = options.headers | ||
let remainingOptions = omit(options, ['method', 'baseUrl', 'url', 'headers']) | ||
let body | ||
let remainingOptions = omit(options, ['method', 'baseUrl', 'url', 'headers', 'request']) | ||
@@ -45,3 +45,3 @@ // replace :varname with {varname} to make it RFC 6570 compatible | ||
if (!/^http/.test(url)) { | ||
url = baseUrl + url | ||
url = options.baseUrl + url | ||
} | ||
@@ -53,3 +53,3 @@ | ||
if (['get', 'head'].includes(method)) { | ||
if (['GET', 'HEAD'].includes(method)) { | ||
url = addQueryParameters(url, remainingOptions) | ||
@@ -68,2 +68,13 @@ } else { | ||
// default content-type for JSON if body is set | ||
if (!headers['content-type'] && typeof body !== 'undefined') { | ||
headers['content-type'] = 'application/json; charset=utf-8' | ||
} | ||
// GitHub expects "content-length: 0" header for PUT/PATCH requests without body | ||
// fetch does not allow to set `content-length` header, but we can set body to an empty string | ||
if (['PATCH', 'PUT'].includes(method) && typeof body === 'undefined') { | ||
body = '' | ||
} | ||
const requestOptions = { | ||
@@ -79,2 +90,6 @@ method, | ||
if (options.request) { | ||
requestOptions.request = options.request | ||
} | ||
return requestOptions | ||
@@ -81,0 +96,0 @@ } |
{ | ||
"name": "@octokit/endpoint", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"publishConfig": { | ||
@@ -5,0 +5,0 @@ "access": "public", |
@@ -178,2 +178,25 @@ # endpoint.js | ||
</tr> | ||
<tr> | ||
<th align=left> | ||
<code>data</code> | ||
</th> | ||
<td> | ||
Any | ||
</td> | ||
<td> | ||
Set request body directly instead of setting it to JSON based on additional parameters. See <a href="#data-parameter">"The <code>data</code> parameter"</a> below. | ||
</td> | ||
</tr> | ||
<tr> | ||
<th align=left> | ||
<code>request</code> | ||
</th> | ||
<td> | ||
Object | ||
</td> | ||
<td> | ||
Pass request-related options here, such as [node-fetch extensions options](https://github.com/bitinn/node-fetch#options). | ||
The <code>request</code> object will be returned as is. | ||
</td> | ||
</tr> | ||
</table> | ||
@@ -185,3 +208,3 @@ | ||
2. If the `method` is `GET` or `HEAD`, the option is passed as query parameter | ||
3. Otherwise the parameter is passed as request body. | ||
3. Otherwise the parameter is passed in the request body as JSON key. | ||
@@ -195,3 +218,3 @@ ## endpoint.defaults() | ||
const myEndpoint = require('@octokit/endpoint').defaults({ | ||
baseUrl: 'http://github-enterprise.acme-inc.com/api/v3', | ||
baseUrl: 'https://github-enterprise.acme-inc.com/api/v3', | ||
headers: { | ||
@@ -212,3 +235,3 @@ 'user-agent': 'myApp/1.2.3', | ||
const myProjectEndpoint = endpoint.defaults({ | ||
baseUrl: 'http://github-enterprise.acme-inc.com/api/v3', | ||
baseUrl: 'https://github-enterprise.acme-inc.com/api/v3', | ||
headers: { | ||
@@ -230,4 +253,17 @@ 'user-agent': 'myApp/1.2.3' | ||
## endpoint.DEFAULTS | ||
The current default options. | ||
```js | ||
endpoint.DEFAULTS.baseUrl // https://api.github.com | ||
const myEndpoint = endpoint.defaults({ | ||
baseUrl: 'https://github-enterprise.acme-inc.com/api/v3' | ||
}) | ||
myEndpoint.DEFAULTS.baseUrl // https://github-enterprise.acme-inc.com/api/v3 | ||
``` | ||
## Special cases | ||
<a name="data-parameter"></a> | ||
### The `data` parameter – set request body directly | ||
@@ -234,0 +270,0 @@ |
@@ -16,3 +16,3 @@ const chai = require('chai') | ||
const myEndpoint = endpoint.defaults({ | ||
baseUrl: 'http://github-enterprise.acme-inc.com/api/v3', | ||
baseUrl: 'https://github-enterprise.acme-inc.com/api/v3', | ||
headers: { | ||
@@ -29,4 +29,4 @@ 'user-agent': 'myApp/1.2.3', | ||
expect(options).to.deep.equal({ | ||
method: 'get', | ||
url: 'http://github-enterprise.acme-inc.com/api/v3/orgs/my-project/repos?per_page=100', | ||
method: 'GET', | ||
url: 'https://github-enterprise.acme-inc.com/api/v3/orgs/my-project/repos?per_page=100', | ||
headers: { | ||
@@ -42,3 +42,3 @@ accept: 'application/vnd.github.v3+json', | ||
const myProjectEndpoint = endpoint.defaults({ | ||
baseUrl: 'http://github-enterprise.acme-inc.com/api/v3', | ||
baseUrl: 'https://github-enterprise.acme-inc.com/api/v3', | ||
headers: { | ||
@@ -58,4 +58,4 @@ 'user-agent': 'myApp/1.2.3' | ||
expect(options2).to.deep.equal({ | ||
method: 'get', | ||
url: 'http://github-enterprise.acme-inc.com/api/v3/orgs/my-project/repos', | ||
method: 'GET', | ||
url: 'https://github-enterprise.acme-inc.com/api/v3/orgs/my-project/repos', | ||
headers: { | ||
@@ -68,2 +68,10 @@ accept: 'application/vnd.github.v3+json', | ||
}) | ||
it('.DEFAULTS', () => { | ||
expect(endpoint.DEFAULTS.baseUrl).to.equal('https://api.github.com') | ||
const myEndpoint = endpoint.defaults({ | ||
baseUrl: 'https://github-enterprise.acme-inc.com/api/v3' | ||
}) | ||
expect(myEndpoint.DEFAULTS.baseUrl).to.equal('https://github-enterprise.acme-inc.com/api/v3') | ||
}) | ||
}) |
@@ -26,3 +26,3 @@ const chai = require('chai') | ||
expect(options).to.deep.equal({ | ||
method: 'get', | ||
method: 'GET', | ||
url: 'https://api.github.com/orgs/octokit/repos?type=private', | ||
@@ -43,3 +43,3 @@ headers: { | ||
expect(options).to.deep.equal({ | ||
method: 'get', | ||
method: 'GET', | ||
url: 'https://api.github.com/orgs/octokit/repos?type=private', | ||
@@ -57,3 +57,3 @@ headers: { | ||
expect(options).to.deep.equal({ | ||
method: 'get', | ||
method: 'GET', | ||
url: 'https://api.github.com/', | ||
@@ -76,3 +76,3 @@ headers: { | ||
expect(options).to.deep.equal({ | ||
method: 'get', | ||
method: 'GET', | ||
url: 'https://api.github.com/', | ||
@@ -90,3 +90,3 @@ headers: { | ||
expect(options).to.deep.equal({ | ||
method: 'get', | ||
method: 'GET', | ||
url: 'https://codeload.github.com/octokit/endpoint-abcde/legacy.tar.gz/master', | ||
@@ -105,4 +105,3 @@ headers: { | ||
headers: { | ||
accept: 'text/html;charset=utf-8', | ||
'content-type': 'text/plain' | ||
accept: 'text/html;charset=utf-8' | ||
}, | ||
@@ -121,7 +120,7 @@ title: 'Found a bug', | ||
expect(options).to.deep.equal({ | ||
method: 'post', | ||
method: 'POST', | ||
url: 'https://api.github.com/repos/octocat/hello-world/issues', | ||
headers: { | ||
accept: 'text/html;charset=utf-8', | ||
'content-type': 'text/plain', | ||
'content-type': 'application/json; charset=utf-8', | ||
'user-agent': userAgent | ||
@@ -144,10 +143,12 @@ }, | ||
it('Put without request body', () => { | ||
const options = endpoint('PUT /user/starred/octocat/hello-world', { | ||
const options = endpoint('PUT /user/starred/:owner/:repo', { | ||
headers: { | ||
authorization: `token 0000000000000000000000000000000000000001` | ||
} | ||
}, | ||
owner: 'octocat', | ||
repo: 'hello-world' | ||
}) | ||
expect(options).to.deep.equal({ | ||
method: 'put', | ||
method: 'PUT', | ||
url: 'https://api.github.com/user/starred/octocat/hello-world', | ||
@@ -159,3 +160,4 @@ headers: { | ||
'user-agent': userAgent | ||
} | ||
}, | ||
body: '' | ||
}) | ||
@@ -177,3 +179,3 @@ }) | ||
expect(options).to.deep.equal({ | ||
method: 'post', | ||
method: 'POST', | ||
url: 'https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets?name=example.zip&label=short%20description', | ||
@@ -197,3 +199,3 @@ headers: { | ||
expect(options).to.deep.equal({ | ||
method: 'get', | ||
method: 'GET', | ||
url: 'https://api.github.com/orgs/octokit/repos?access_token=abc4567&type=private', | ||
@@ -217,3 +219,3 @@ headers: { | ||
expect(options).to.deep.equal({ | ||
method: 'post', | ||
method: 'POST', | ||
url: 'https://api.github.com/markdown/raw', | ||
@@ -235,3 +237,3 @@ headers: { | ||
expect(options).to.deep.equal({ | ||
method: 'get', | ||
method: 'GET', | ||
url: 'https://api.github.com/search/issues?q=location%3AJyv%C3%A4skyl%C3%A4', | ||
@@ -244,2 +246,22 @@ headers: { | ||
}) | ||
it('request parameter', () => { | ||
const options = endpoint('GET /', { | ||
request: { | ||
timeout: 100 | ||
} | ||
}) | ||
expect(options).to.deep.equal({ | ||
method: 'GET', | ||
url: 'https://api.github.com/', | ||
headers: { | ||
accept: 'application/vnd.github.v3+json', | ||
'user-agent': userAgent | ||
}, | ||
request: { | ||
timeout: 100 | ||
} | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30478
14
425
312