@@ -23,75 +23,81 @@ export type HttpTransportTypes = BaseEndpointTypes & {
2323// HttpTransport is used to fetch and process data from a Provider using HTTP(S) protocol. It usually needs two methods
2424// `prepareRequests` and `parseResponse`
2525< % } - %>
26- export const httpTransport = new HttpTransport<HttpTransportTypes >({
26+ export class <%= normalizedEndpointNameCap %> HttpTransport extends HttpTransport<HttpTransportTypes > {
27+ constructor() {
28+ super({
2729<% if (includeComments) { -% >
28- // `prepareRequests` method receives request payloads sent to associated endpoint alongside adapter config(environment variables)
29- // and should return 'request information' to the Data Provider. Use this method to construct one or many requests, and the framework
30- // will send them to Data Provider
31- < % } - %>
32- prepareRequests: (params, config) => {
33- return params.map((param) => {
34- return {
35- <% if (includeComments) { -% >
36- // `params` are parameters associated to this single request and will also be available in the 'parseResponse' method.
37- < % } - %>
38- params: [param],
39- <% if (includeComments) { -% >
40- // `request` contains any valid axios request configuration
41- < % } - %>
42- request: {
43- baseURL: config.API_ENDPOINT,
44- url: '/cryptocurrency/price',
45- headers: {
46- 'X_API_KEY': config.API_KEY,
47- },
48- params: {
49- symbol: param.base.toUpperCase(),
50- convert: param.quote.toUpperCase(),
51- },
52- },
53- }
54- })
55- },
56- <% if (includeComments) { -% >
57- // `parseResponse` takes the 'params' specified in the `prepareRequests` and the 'response' from Data Provider and should return
58- // an array of response objects to be stored in cache. Use this method to construct a list of response objects for every parameter in 'params'
59- // and the framework will save them in cache and return to user
60- < % } - %>
61- parseResponse: (params, response) => {
62- <% if (includeComments) { -% >
63- // For each 'param' a new response object is created and returned as an array
64- < % } - %>
65- return params.map((param) => {
66- <% if (includeComments) { -% >
67- // In case error was received, it's a good practice to return meaningful information to user
68- < % } - %>
69- const baseSymbol = param.base.toUpperCase()
70- if (!response.data || !response.data[baseSymbol]) {
71- return {
72- params: param,
73- response: {
74- errorMessage: `The data provider didn't return any value for ${param.base}/${param.quote}`,
75- statusCode: 502,
76- },
77- }
78- }
30+ // `prepareRequests` method receives request payloads sent to associated endpoint alongside adapter config(environment variables)
31+ // and should return 'request information' to the Data Provider. Use this method to construct one or many requests, and the framework
32+ // will send them to Data Provider
33+ < % } - %>
34+ prepareRequests: (params, config) => {
35+ return params.map((param) => {
36+ return {
37+ <% if (includeComments) { -% >
38+ // `params` are parameters associated to this single request and will also be available in the 'parseResponse' method.
39+ < % } - %>
40+ params: [param],
41+ <% if (includeComments) { -% >
42+ // `request` contains any valid axios request configuration
43+ < % } - %>
44+ request: {
45+ baseURL: config.API_ENDPOINT,
46+ url: '/cryptocurrency/price',
47+ headers: {
48+ 'X_API_KEY': config.API_KEY,
49+ },
50+ params: {
51+ symbol: param.base.toUpperCase(),
52+ convert: param.quote.toUpperCase(),
53+ },
54+ },
55+ }
56+ })
57+ },
58+ <% if (includeComments) { -% >
59+ // `parseResponse` takes the 'params' specified in the `prepareRequests` and the 'response' from Data Provider and should return
60+ // an array of response objects to be stored in cache. Use this method to construct a list of response objects for every parameter in 'params'
61+ // and the framework will save them in cache and return to user
62+ < % } - %>
63+ parseResponse: (params, response) => {
64+ <% if (includeComments) { -% >
65+ // For each 'param' a new response object is created and returned as an array
66+ < % } - %>
67+ return params.map((param) => {
68+ <% if (includeComments) { -% >
69+ // In case error was received, it's a good practice to return meaningful information to user
70+ < % } - %>
71+ const baseSymbol = param.base.toUpperCase()
72+ if (!response.data || !response.data[baseSymbol]) {
73+ return {
74+ params: param,
75+ response: {
76+ errorMessage: `The data provider didn't return any value for ${param.base}/${param.quote}`,
77+ statusCode: 502,
78+ },
79+ }
80+ }
7981
80- const result = response.data[baseSymbol].price
81- <% if (includeComments) { -% >
82- // Response objects, whether successful or errors, contain two properties, 'params' and 'response'. 'response' is what will be
83- // stored in the cache and returned as adapter response and 'params' determines the identifier so that the next request with same 'params'
84- // will immediately return the response from the cache
85- < % } - %>
86- return {
87- params: param,
88- response: {
89- result,
90- data: {
91- result
82+ const result = response.data[baseSymbol].price
83+ <% if (includeComments) { -% >
84+ // Response objects, whether successful or errors, contain two properties, 'params' and 'response'. 'response' is what will be
85+ // stored in the cache and returned as adapter response and 'params' determines the identifier so that the next request with same 'params'
86+ // will immediately return the response from the cache
87+ < % } - %>
88+ return {
89+ params: param,
90+ response: {
91+ result,
92+ data: {
93+ result,
94+ },
95+ },
9296 }
93- },
94- }
97+ })
98+ },
9599 })
96- },
97- })
100+ }
101+ }
102+
103+ export const httpTransport = new <%= normalizedEndpointNameCap %> HttpTransport()
0 commit comments