Socket
Socket
Sign inDemoInstall

@octokit/endpoint

Package Overview
Dependencies
Maintainers
3
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@octokit/endpoint - npm Package Compare versions

Comparing version 2.0.2 to 2.1.0

lib/default-options.js

2

lib/defaults.js

@@ -7,3 +7,3 @@ const getUserAgent = require('universal-user-agent')

module.exports = {
method: 'get',
method: 'GET',
baseUrl: 'https://api.github.com',

@@ -10,0 +10,0 @@ headers: {

module.exports = endpointWithDefaults
const merge = require('deepmerge')
const isPlainObject = require('is-plain-object')
const urlTemplate = require('url-template')
const defaultOptions = require('./default-options')
const toRequestOptions = require('./to-request-options')
const addQueryParameters = require('./add-query-parameters')
const extractUrlVariableNames = require('./extract-url-variable-names')
const omit = require('./omit')
function endpointWithDefaults (defaults, route, options) {
if (typeof route === 'string') {
const [method, url] = route.split(' ')
options = Object.assign({ method, url }, options)
} else {
options = route
}
// lowercase header names before merging with defaults to avoid duplicates
if (options.headers) {
options.headers = Object.keys(options.headers).reduce((newObj, key) => {
newObj[key.toLowerCase()] = options.headers[key]
return newObj
}, {})
}
options = merge.all([defaults, options].filter(Boolean), { isMergeableObject: isPlainObject })
// https://fetch.spec.whatwg.org/#methods
let method = options.method.toUpperCase()
// replace :varname with {varname} to make it RFC 6570 compatible
let url = options.url.replace(/:([a-z]\w+)/g, '{+$1}')
let headers = options.headers
let body
let remainingOptions = omit(options, ['method', 'baseUrl', 'url', 'headers', 'request'])
// extract variable names from URL to calculate remaining variables later
const urlVariableNames = extractUrlVariableNames(url)
url = urlTemplate.parse(url).expand(remainingOptions)
if (!/^http/.test(url)) {
url = options.baseUrl + url
}
const omittedOptions = Object.keys(options).filter((option) => urlVariableNames.includes(option)).concat('baseUrl')
remainingOptions = omit(remainingOptions, omittedOptions)
// for GET/HEAD requests, set URL query parameters from remaining parameters
// for PATCH/POST/PUT/DELETE requests, set request body from remaining parameters
if (['GET', 'HEAD'].includes(method)) {
url = addQueryParameters(url, remainingOptions)
} else {
if ('data' in remainingOptions) {
body = remainingOptions.data
} else {
if (Object.keys(remainingOptions).length) {
body = remainingOptions
} else {
headers['content-length'] = 0
}
}
}
// 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 = ''
}
// Only return body/request keys if present
return Object.assign(
{ method, url, headers },
typeof body !== 'undefined' ? { body } : null,
options.request ? { request: options.request } : null
)
const endpointOptions = defaultOptions(defaults, route, options)
return toRequestOptions(endpointOptions)
}
{
"name": "@octokit/endpoint",
"version": "2.0.2",
"version": "2.1.0",
"publishConfig": {

@@ -5,0 +5,0 @@ "access": "public",

@@ -261,2 +261,39 @@ # endpoint.js

## endpoint.options()
Get the defaulted endpoint options, but without parsing them into request options
```js
const myProjectEndpoint = endpoint.defaults({
baseUrl: 'https://github-enterprise.acme-inc.com/api/v3',
headers: {
'user-agent': 'myApp/1.2.3'
},
org: 'my-project'
})
myProjectEndpoint.options('GET /orgs/:org/repos', {
headers: {
authorization: `token 0000000000000000000000000000000000000001`
},
type: 'private'
})
// {
// baseUrl: 'https://github-enterprise.acme-inc.com/api/v3',
// method: 'GET',
// url: '/orgs/:org/repos',
// headers: {
// accept: 'application/vnd.github.v3+json',
// authorization: `token 0000000000000000000000000000000000000001`,
// 'user-agent': 'myApp/1.2.3'
// },
// org: 'my-project',
// type: 'private'
// }
```
## endpoint.parse()
Stateless method to turn endpoint options into request options. Calling
`endpoint(options)` is the same as calling `endpoint.parse(endpoint.options(options))`
## Special cases

@@ -263,0 +300,0 @@

@@ -52,5 +52,5 @@ const chai = require('chai')

const options2 = myProjectEndpointWithAuth(`GET /orgs/:org/repos`)
const options = myProjectEndpointWithAuth(`GET /orgs/:org/repos`)
expect(options2).to.deep.equal({
expect(options).to.deep.equal({
method: 'GET',

@@ -57,0 +57,0 @@ url: 'https://github-enterprise.acme-inc.com/api/v3/orgs/my-project/repos',

@@ -7,4 +7,8 @@ module.exports = withDefaults

const endpointWithDefaults = require('./lib/endpoint-with-defaults')
const lowercaseKeys = require('./lib/lowercase-keys')
const optionsWithDefaults = require('./lib/default-options')
const toRequestOptions = require('./lib/to-request-options')
function withDefaults (oldDefaults, newDefaults) {
newDefaults.headers = lowercaseKeys(newDefaults.headers)
const DEFAULTS = merge.all([oldDefaults, newDefaults].filter(Boolean), { isMergeableObject: isPlainObject })

@@ -14,3 +18,5 @@ const endpoint = endpointWithDefaults.bind(null, DEFAULTS)

endpoint.defaults = withDefaults.bind(null, DEFAULTS)
endpoint.options = optionsWithDefaults.bind(null, DEFAULTS)
endpoint.parse = toRequestOptions
return endpoint
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc