This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 300
refactor: convert config API to async await #1155
Merged
+106
−116
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1d9d263
refactor: convert config.get to async await
359f13f
refactor: convert config.profiles.* and config.replace/set to async/a…
113ffec
fix: error responses
56a7b2d
fix: yay asserting on error message
ea74f9d
chore: add logging
cdc97ad
chore: more log
67994bd
fix: error message assertion
f4d5128
fix: revert error message change
d4377c6
Merge branch 'master' into refactor/config-async-await
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,27 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const configure = require('../lib/configure') | ||
|
||
const toObject = function (res, callback) { | ||
if (Buffer.isBuffer(res)) { | ||
callback(null, JSON.parse(res.toString())) | ||
} else { | ||
callback(null, res) | ||
} | ||
} | ||
|
||
module.exports = (send) => { | ||
return promisify((key, callback) => { | ||
if (typeof key === 'function') { | ||
callback = key | ||
key = undefined | ||
module.exports = configure(({ ky }) => { | ||
return async (key, options) => { | ||
if (key && typeof key === 'object') { | ||
options = key | ||
key = null | ||
} | ||
|
||
if (!key) { | ||
send.andTransform({ | ||
path: 'config/show', | ||
buffer: true | ||
}, toObject, callback) | ||
return | ||
} | ||
options = options || {} | ||
|
||
send.andTransform({ | ||
path: 'config', | ||
args: key, | ||
buffer: true | ||
}, toObject, (err, response) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
callback(null, response.Value) | ||
}) | ||
}) | ||
} | ||
const searchParams = new URLSearchParams(options.searchParams) | ||
if (key) searchParams.set('arg', key) | ||
|
||
const url = key ? 'config' : 'config/show' | ||
const data = await ky.get(url, { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}).json() | ||
|
||
return key ? data.Value : data | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
'use strict' | ||
|
||
module.exports = (send, config) => { | ||
return { | ||
get: require('./get')(send), | ||
set: require('./set')(send), | ||
replace: require('./replace')(send), | ||
profiles: { | ||
apply: require('./profiles/apply')(config), | ||
list: require('./profiles/list')(config) | ||
} | ||
} | ||
} | ||
const callbackify = require('callbackify') | ||
|
||
module.exports = config => ({ | ||
get: callbackify.variadic(require('./get')(config)), | ||
set: callbackify.variadic(require('./set')(config)), | ||
replace: callbackify.variadic(require('./replace')(config)), | ||
profiles: require('./profiles')(config) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,24 @@ | ||
'use strict' | ||
|
||
const callbackify = require('callbackify') | ||
const configure = require('../../lib/configure') | ||
|
||
module.exports = configure(({ ky }) => { | ||
return callbackify.variadic(async (profile, options) => { | ||
return async (profile, options) => { | ||
options = options || {} | ||
|
||
const searchParams = new URLSearchParams(options.searchParams) | ||
searchParams.set('arg', profile) | ||
if (options.dryRun != null) searchParams.set('dry-run', options.dryRun) | ||
|
||
const res = await ky.post('config/profile/apply', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams: { | ||
arg: profile, | ||
// can only pass strings or numbers as values https://github.com/sindresorhus/ky/issues/182 | ||
'dry-run': options.dryRun ? 'true' : 'false' | ||
} | ||
}) | ||
|
||
const parsed = await res.json() | ||
searchParams | ||
}).json() | ||
|
||
return { | ||
original: parsed.OldCfg, updated: parsed.NewCfg | ||
original: res.OldCfg, updated: res.NewCfg | ||
} | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
|
||
const callbackify = require('callbackify') | ||
|
||
module.exports = config => ({ | ||
apply: callbackify.variadic(require('./apply')(config)), | ||
list: callbackify.variadic(require('./list')(config)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
'use strict' | ||
|
||
const callbackify = require('callbackify') | ||
const configure = require('../../lib/configure') | ||
const toCamel = require('../../lib/object-to-camel') | ||
|
||
module.exports = configure(({ ky }) => { | ||
return callbackify.variadic(async (options) => { | ||
return async (options) => { | ||
options = options || {} | ||
|
||
const res = await ky.get('config/profile/list', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers | ||
}) | ||
headers: options.headers, | ||
searchParams: options.searchParams | ||
}).json() | ||
|
||
const parsed = await res.json() | ||
|
||
return parsed | ||
.map(profile => toCamel(profile)) | ||
}) | ||
return res.map(profile => toCamel(profile)) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
'use strict' | ||
|
||
const { Readable } = require('readable-stream') | ||
const promisify = require('promisify-es6') | ||
const SendOneFile = require('../utils/send-one-file') | ||
const { Buffer } = require('buffer') | ||
const configure = require('../lib/configure') | ||
const toFormData = require('../lib/buffer-to-form-data') | ||
|
||
function toStream (input) { | ||
return new Readable({ | ||
read () { | ||
this.push(input) | ||
this.push(null) | ||
} | ||
}) | ||
} | ||
module.exports = configure(({ ky }) => { | ||
return async (config, options) => { | ||
options = options || {} | ||
|
||
module.exports = (send) => { | ||
const sendOneFile = SendOneFile(send, 'config/replace') | ||
return promisify((config, callback) => { | ||
if (typeof config === 'object') { | ||
config = toStream(Buffer.from(JSON.stringify(config))) | ||
} | ||
const res = await ky.post('config/replace', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams: options.searchParams, | ||
body: toFormData(Buffer.from(JSON.stringify(config))) | ||
}).text() | ||
|
||
sendOneFile(config, {}, callback) | ||
}) | ||
} | ||
return res | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,36 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const configure = require('../lib/configure') | ||
const toCamel = require('../lib/object-to-camel') | ||
|
||
module.exports = configure(({ ky }) => { | ||
return async (key, value, options) => { | ||
options = options || {} | ||
|
||
module.exports = (send) => { | ||
return promisify((key, value, opts, callback) => { | ||
if (typeof opts === 'function') { | ||
callback = opts | ||
opts = {} | ||
} | ||
if (typeof key !== 'string') { | ||
return callback(new Error('Invalid key type')) | ||
throw new Error('Invalid key type') | ||
} | ||
|
||
if (value === undefined || Buffer.isBuffer(value)) { | ||
return callback(new Error('Invalid value type')) | ||
} | ||
const searchParams = new URLSearchParams(options.searchParams) | ||
|
||
if (typeof value === 'boolean') { | ||
searchParams.set('bool', true) | ||
value = value.toString() | ||
opts = { bool: true } | ||
} else if (typeof value !== 'string') { | ||
searchParams.set('json', true) | ||
value = JSON.stringify(value) | ||
opts = { json: true } | ||
} | ||
|
||
send({ | ||
path: 'config', | ||
args: [key, value], | ||
qs: opts, | ||
files: undefined, | ||
buffer: true | ||
}, callback) | ||
}) | ||
} | ||
searchParams.set('arg', key) | ||
searchParams.append('arg', value) | ||
|
||
const res = await ky.post('config', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}).json() | ||
|
||
return toCamel(res) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaking change? I guess errors aren't really part of the spec other than 'you will get an error'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is breaking. I've updated the PR description with the info and will include it when merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. One small thing - the description says
err.status
has changed but it'serr.statusCode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well this test was looking for
.status
but others are looking for.statusCode
...I've added both to the breaking change message.