Last active
November 20, 2019 17:27
-
-
Save heisian/fdd5b42e35e98c906488eca0193f21c9 to your computer and use it in GitHub Desktop.
FYSTSXHR
This file contains hidden or 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
import Xhr from 'Xhr' | |
const xhr = new Xhr() | |
// Or alternatively, xhr is just functional: | |
// import xhr from 'xhr' | |
// Assume xhr instance (or functional equivalent) is being passed in | |
;(async (xhr) => { | |
// Current API | |
let response = await xhr('patch') | |
.user(2) | |
.identity(2) | |
.data({ | |
email: '[email protected]', | |
password: 'somethignReally.Good!', | |
nameFirst: 'Timothy', | |
nameLast: 'Huang', | |
birthDate: '1985-10-21', | |
gender: 'Male', | |
}) | |
.fetch() | |
// Hypothetical API, closure/DI-style | |
response = await xhr.patch((request) => { | |
request.path.user(2).identity(2) | |
request.headers({ 'Content-Type': 'application/json' }) | |
request.data({ | |
email: '[email protected]', | |
password: 'somethignReally.Good!', | |
nameFirst: 'Timothy', | |
nameLast: 'Huang', | |
birthDate: '1985-10-21', | |
gender: 'Male', | |
}) | |
}) | |
// Hypothetical again, using destructuring | |
response = await xhr.patch(({ path, headers, data }) => { | |
path.user(2).identity(2) | |
headers({ 'Content-Type': 'application/json' }) | |
data({ | |
email: '[email protected]', | |
password: 'somethignReally.Good!', | |
nameFirst: 'Timothy', | |
nameLast: 'Huang', | |
birthDate: '1985-10-21', | |
gender: 'Male', | |
}) | |
}) | |
// Hypothetical again, with custom path and query params | |
response = await xhr.get(({ path, params, headers, data }) => { | |
path('/some/custom/path') | |
params({ | |
some: 'data', | |
will: 'getQueryStringified', | |
}) | |
}) | |
}) |
simplified, after speaking IRL w/ @roblevintennis:
import xhr from 'xhr'
;(async() => {
const path = new xhr.path
.user(userId)
.identity(identityId)
.termPolicy(termPolicyId)
// Xhr calls fetch internally
const { err, response } = await xhr({
baseURL: 'somethingelse.com',
method: xhr.POST,
path,
{ some: 'data' },
})
if (err) {
// handle it
}
})()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how would I invoke
termPolicyCreateXhr
in this example?perhaps,