Created
April 10, 2019 21:35
-
-
Save jordangarcia/4ce71a00150e13a38ec1197556f39071 to your computer and use it in GitHub Desktop.
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
const module = createAPIModule('projects', { | |
baseUrl: 'http://api.optimizely.com/v2/', | |
serialize: a => a, | |
deserialize: a => a, | |
}) | |
class ProjectActions extends BaseActions { | |
fetch = module.doGETRequest({ | |
url: '/exp-api/projects/{id}', | |
queryParams: { | |
resourceId: null, | |
filters: 'idea_scores', | |
}, | |
}) | |
fetchAll = module.doGETRequest({ | |
url: '/exp-api/projects', | |
queryParams: { | |
resourceId: null, | |
filters: 'idea_scores', | |
}, | |
}) | |
save = module.doPOSTRequest({ | |
url: '/exp-api/projects', | |
queryParams: { | |
resourceId: null, | |
filters: 'idea_scores', | |
}, | |
}) | |
} | |
class BaseActions { | |
constructor({ baseUrl, serialize, deserialize, entityName }) { | |
this.entityName = entityName | |
this.baseUrl = baseUrl | |
this.serialize = serialize | |
this.deserialize = deserialize | |
} | |
fetch(params, headers) { | |
return dispatch => { | |
const url = this.getURL(params) | |
// Don't allow any null params | |
for (let k in params) { | |
if (!params[k]) { | |
return Promise.reject( | |
new Error( | |
JSON.stringify({ | |
message: 'Refused to send request, param values cannot be null or empty', | |
param: k, | |
params, | |
url, | |
}), | |
), | |
) | |
} | |
} | |
return fetch(url, { | |
type: 'GET', | |
mode: 'cors', | |
headers: Object.assign({}, { 'Content-type': 'application/json' }, headers), | |
}) | |
.then(response => { | |
if (response.ok) { | |
return response.json() | |
} | |
throw new Error( | |
JSON.stringify({ | |
status: response.status, | |
statusText: response.statusText, | |
url: response.url, | |
type: response.type, | |
}), | |
) | |
}) | |
.then(payload => | |
this.deserialize( | |
Immutable.fromJS(Array.isArray(payload) ? payload : [payload]), | |
), | |
) | |
.then(payload => { | |
dispatch({ | |
type: FETCH_SUCCESS, | |
entityName: this.entityName, | |
payload, | |
}) | |
return payload | |
}) | |
} | |
} | |
loadEntity(payload) { | |
return dispatch => { | |
dispatch({ | |
type: LOAD_ENTITY, | |
entityName: this.entityName, | |
payload: this.deserialize( | |
Immutable.fromJS(Array.isArray(payload) ? payload : [payload]), | |
), | |
}) | |
} | |
} | |
} | |
class ProjectActions extends BaseActions { | |
fetchAll(params, headers) { | |
return super.makeRequest({ | |
method: 'GET', | |
headers, | |
url: `/exp-api/projects/`, | |
queryParams: params, | |
}) | |
} | |
fetch(params, headers) { | |
return super.makeRequest({ | |
method: 'GET', | |
headers, | |
url: `/exp-api/projects/${params.id}`, | |
queryParams: params, | |
}) | |
} | |
} | |
const fetchFn = module.makeRequestFunction({ | |
method: 'GET', | |
url: '/exp-api/projects/{id}', | |
queryParams: { | |
resourceId: null, | |
filters: 'idea_scores', | |
}, | |
}) | |
fetchFn({ id: 123}, header) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment