Skip to content

Commit

Permalink
feat: return raw fetch response of data query
Browse files Browse the repository at this point in the history
closes #14
  • Loading branch information
Daniel J. Lauk committed Jun 24, 2020
1 parent 8ef1135 commit 778df77
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/http-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export const COMMON_FETCH_OPTIONS: RequestInit = {
* @param url The URL to send the POST request to.
* @param options Request options (headers, payload, etc.).
* @param timeoutMs The timeout (in milliseconds) to wait for a response.
* @param returnRaw If `true` returns the raw response object. If `false` (default) returns `response.json()`.
*/
export const fetchWithTimeout = async (
url: string,
options: RequestInit,
timeoutMs: number = 30000
timeoutMs: number = 30000,
returnRaw = false
): Promise<unknown> => {
let timerId
const abortController = new AbortController()
Expand All @@ -49,6 +51,7 @@ export const fetchWithTimeout = async (
clearTimeout(timerId)
if (!response.ok)
throw new Error(`${response.status}: ${response.statusText}`)
if (returnRaw) return response
return await response.json()
} catch (err) {
clearTimeout(timerId)
Expand All @@ -69,16 +72,17 @@ export const fetchWithTimeout = async (
*
* @param url The URL to send the POST request to
* @param body The *optional* payload to be sent with the request
* @param returnRaw If `true` returns the raw response object. If `false` (default) returns `response.json()`.
*
* @typeParam T The type of the request body
*/
export const post = async <T>(url: string, body?: T) => {
export const post = async <T>(url: string, body?: T, returnRaw = false) => {
const options: RequestInit = {
...COMMON_FETCH_OPTIONS,
method: 'POST',
}
if (body !== undefined) options.body = JSON.stringify(body)
return fetchWithTimeout(url, options)
return fetchWithTimeout(url, options, undefined, returnRaw)
}

/**
Expand All @@ -92,12 +96,12 @@ export const post = async <T>(url: string, body?: T) => {
* - Adds a type cast for the payload's response
*
* @param url The URL to send the POST request to
* @param body The *optional* payload to be sent with the request
* @param returnRaw If `true` returns the raw response object. If `false` (default) returns `response.json()`.
*/
export const get = async (url: string) => {
export const get = async (url: string, returnRaw = false) => {
const options: RequestInit = {
...COMMON_FETCH_OPTIONS,
method: 'GET',
}
return fetchWithTimeout(url, options)
return fetchWithTimeout(url, options, undefined, returnRaw)
}
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
ChannelNamesQuery,
ChannelNamesResponse,
} from './query-channel-names'
import { queryData } from './query-data'
import { queryData, queryDataRaw } from './query-data'
import type { DataQuery, DataResponse } from './query-data'

export type {
Expand Down Expand Up @@ -81,4 +81,13 @@ export class QueryRest {
async queryData(query: DataQuery): Promise<DataResponse> {
return queryData(this.url, query)
}

/**
* queryDataRaw see [[queryDataRaw]].
*
* @param query defines the query
*/
async queryDataRaw(query: DataQuery): Promise<Response> {
return queryDataRaw(this.url, query)
}
}
17 changes: 17 additions & 0 deletions src/query-data/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,20 @@ export const queryData = (
const endpoint = `${baseUrl}/query`
return post(endpoint, query) as Promise<DataResponse>
}

/**
* queryDataRaw queries the REST API for data and returns the `Response`
* from the request.
*
* @param baseUrl URL of the REST interfaces startpoint for routing
* @param query defines the query to be run
*
* @returns A `Promise` with the unprocessed fetch response.
*/
export const queryDataRaw = (
baseUrl: string,
query: DataQuery
): Promise<Response> => {
const endpoint = `${baseUrl}/query`
return post(endpoint, query, true) as Promise<Response>
}

0 comments on commit 778df77

Please sign in to comment.