Created
October 17, 2016 02:47
-
-
Save mfpiccolo/e9c289245e52e9d99fff3ee4bfdd2dd0 to your computer and use it in GitHub Desktop.
Concept for json api deserializer fetch wrapper.
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
api_config = { | |
base_url: 'http://localhost:8065/api/v3', | |
defaluts: { // if none passed here then use JSON.parse() | |
success_deserializer: successJsonApiDerserializer, | |
error_deserializer: errorJsonApiDerserializer, | |
// has the same defaults as fetch | |
}, | |
resources: { | |
all_team_listings: { | |
endpoint: 'teams/all_team_listings', | |
// takes anything that fetch takes as options | |
// uses default deserializers | |
}, | |
channels: { | |
endpoint: 'teams/${team_id}/channels', | |
}, | |
channels_with_param: { | |
endpoint: 'teams/${team_id}/channels?isOpen=${is_open}', | |
} | |
} | |
} | |
const resp = fetchJSON({ | |
config: api_config, | |
resource: 'channels_with_param', | |
params: { | |
team_id: 4, | |
is_open: true | |
} | |
}) | |
const resp = fetchJSON( | |
{ | |
url: url, | |
method: 'GET', | |
success_deserializer: successJsonApiDerserializer, | |
error_deserializer: errorJsonApiDerserializer, | |
} | |
) | |
resp.ok // => boolean | |
resp.data // => returns the response from fetch passed to successJsonApiDerserializer(response.json()) | |
resp.error // => returns the errors from anything that does not return resp.ok or other errors from fetch passed to errorJsonApiDerserializer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment