Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Created October 17, 2016 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfpiccolo/e9c289245e52e9d99fff3ee4bfdd2dd0 to your computer and use it in GitHub Desktop.
Save mfpiccolo/e9c289245e52e9d99fff3ee4bfdd2dd0 to your computer and use it in GitHub Desktop.
Concept for json api deserializer fetch wrapper.
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