Repository URL to install this package:
|
Version:
5.2.0 ▾
|
import { URL, URLSearchParams } from 'url'
import {
RESTDataSource,
RequestOptions,
HTTPCache,
} from 'apollo-datasource-rest'
export interface OneRequestAsSerializedDebug {
// these really are `mergedHeaders` & `mergedParams` respectively
headers: AnyObj
params: AnyObj
url: string
body: string | AnyObj
method: Method
named?: string
jwt?: string
// @todo @name
debug?: boolean
}
export interface OneRequestStoreAsObj {
headers: Headers
path: string
params: { [key: string]: any }
method: 'get' | 'post' | 'patch' | 'delete'
urlObj: URL
context: OneRequestContextBase
cache: any
body: RequestBody
}
export interface AnyObj {
[key: string]: any
[key: number]: any
}
export interface ApolloRequest {
httpRequest: Request
}
import { Request as ExpressRequest, Response as ExpressResponse } from 'express'
import { Logger } from 'pino'
export interface OneRequestContextBase {
[key: string]: OneRequest | any
req: ExpressRequest
res: ExpressResponse
log?: Logger
correlationId: string
}
export type ApolloRequestOrRequest = ApolloRequest
// @todo
// export type LooseApolloRequest = {
// httpRequest: {
// headers:
// | Headers
// | {
// get(key?: string): any
// }
// [key: string]: any
// }
// [key: string]: any
// }
export type OneRequestStoreKey =
| 'debug'
| 'named'
| 'tapParams'
| 'tapResponse'
| 'method'
| 'url'
| 'base'
| 'path'
| 'jwt'
| 'headers'
| 'params'
| 'body'
| 'parent'
| 'baseUrl'
| 'urlObj'
| 'correlationId'
// export type RequestStore = Map<string, any>
export interface OneRequestStore<
Value = any,
Context extends OneRequestContextBase = OneRequestContextBase
> extends Map<string, any> {
get(key: 'debug'): boolean
set(key: 'debug', value: boolean): this
get(key: 'named'): string
set(key: 'named', value: string): this
// get<In = Params, Out = Params>(key: 'tapParams'): TransformParams<In, Out>
// set<In = Params, Out = Params>(
// key: 'tapParams',
// value: TransformParams<In, Out>
// ): this
// get<In = ResponseDataIn, Out = ResponseDataOut>(
// key: 'tapResponse'
// ): TransformResponse<In, Out>
// set<In = ResponseDataIn, Out = ResponseDataOut>(
// key: 'tapResponse',
// value: TransformResponse<In, Out>
// ): this
get(key: 'tapParams'): TransformParams
set(key: 'tapParams', value: TransformParams): this
get(key: 'tapResponse'): TransformResponse
set(key: 'tapResponse', value: TransformResponse): this
get(key: 'method'): Method
set(key: 'method', value: Method): this
get(key: 'urlObj'): URL
set(key: 'urlObj', value: URL): this
get(key: 'url'): string
set(key: 'url', value: string): this
// url => base & path
get(key: 'baseUrl'): string
set(key: 'baseUrl', value: string): this
get(key: 'path'): string
set(key: 'path', value: string): this
get(key: 'jwt'): string
set(key: 'jwt', value: string): this
get(key: 'headers'): Headers
set(key: 'headers', value: Headers): this
get(key: 'params'): Params
set(key: 'params', value: Params): this
get(key: 'body'): RequestBody
set(key: 'body', value: RequestBody): this
get(key: 'parent'): OneRequest | any
set(key: 'parent', value: OneRequest | any): this
get(key: 'correlationId'): string
set(key: 'correlationId', value: string): this
get(key: 'httpCache'): HTTPCache
set(key: 'httpCache', value: HTTPCache): this
get(key: 'context'): Context
set(key: 'context', value: Context): this
get(key: 'cache'): string | false
set(key: 'cache', value: string | false): this
get(key: 'credentials'): 'include' | 'same-origin'
set(key: 'credentials', value: 'include' | 'same-origin'): this
// no others
// get(key: string): Value
// set(key: string, value: Value): this
}
export interface ScopedRequest {
body?: RequestBody | string
headers: Headers
url?: string | URL
params?: Params
}
/**
* @see https://github.com/angular/angular/blob/6.1.0-beta.3/packages/http/src/url_search_params.ts
*/
export interface ParamsObjObj {
[key: string]:
| ParamsObj
| string
| number
| boolean
| Array<ParamsObj | string | number | boolean>
}
export type ParamsObj = ParamsObjObj | ParamsObjObj[]
export type Params = ParamsObj | URLSearchParams
export interface OneRequestObj {
headers?: Headers
params?: Params
method?: 'GET' | 'POST'
_children?: Map<string, OneRequest>
named?: string
debug?: boolean
parent?: OneRequest
url?: string
path?: string
baseUrl?: string
forwardRequest?: Request
tapResponse?: TransformResponse
tapParams?: TransformParams
}
export type HeaderKey = string
export type HeaderValue = string | number | HeadersObj
export interface HeadersObj {
[key: string]: HeaderValue
}
export interface OneRequestHeaders extends Headers {
set(key: 'Authorization', value: string): this
get(key: 'Authorization'): string
get(key: string): any
set(key: string, value: any): this
}
export interface RequestBody {
[key: string]: string | any
}
export type Body = string | object
export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'
export interface TransformParams<ParamsIn = ParamsObj, ParamsOut = ParamsObj>
extends Function {
(params: ParamsIn): ParamsOut
}
// could be any serializable
export interface ResponseData {
[key: string]: string | boolean | number | ResponseDataOut
}
export interface ResponseDataOut {
[key: string]: string | boolean | number | ResponseDataOut
}
// stream has only strings
export interface ResponseDataIn {
[key: string]: string | ResponseDataIn
}
export interface TransformResponse<
ResponseIn = ResponseDataIn,
ResponseOut = ResponseDataOut
> extends Function {
(responseData: ResponseIn): ResponseOut
}
/**
* @see https://angular.io/guide/http
*/
export enum ResponseType {
Text,
Json,
ArrayBuffer,
Blob,
}
/**
* https://github.com/form-data/form-data
*/
/**
* @api https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
*/
export interface CredentialsArray extends Array<string> {
length: 1 | 2
[0]: 'user' | string
[1]?: 'password' | string
}
export type Credentials = Array<string> | 'user:password' | string
export interface OneRequestAsApolloDataSource<Context> {
baseURL: string
httpCache: HTTPCache
context: Context
fetch<Result>(path: string, options: RequestOptions): Promise<Result>
fetch<Result = Response>(): Promise<Result>
get<Result = Response>(): Promise<Result>
post<Result = Response>(): Promise<Result>
put<Result = Response>(): Promise<Result>
patch<Result = Response>(): Promise<Result>
delete<Result = Response>(): Promise<Result>
}
export interface Adapter {
(store: OneRequestStore): RESTDataSource
}
export interface OneRequest {
store: OneRequestStore
init(obj?: OneRequestObj): OneRequest
from(obj?: OneRequestObj): OneRequest
as(adapter?: Adapter): RESTDataSource
forwardRequest(request: ApolloRequest): this
doRequest(): Promise<Response>
toRequest(): Request
clone(): OneRequest
debug(shouldDebug?: boolean): this
parent(parent: OneRequest): this
named(named: string): this
auth(credentials: Credentials): this
jwt(token?: string): this
method(method: Method): this
header(key: HeaderKey, value: HeaderValue): this
headers(headers: HeadersInit | Headers): this
url(url?: string): this
params(params: Params): this
body(body: Body): this
tapRequestParams<In, Out>(tap: TransformParams<In, Out>): this
tapResponseData<In, Out>(tap: TransformResponse<In, Out>): this
/** @deprecated */
setUrl(url?: any): this
/** @deprecated */
setMethod(method?: any): this
/** @deprecated */
setDefaultParams(params?: any): this
/** @deprecated */
setParamsTransform(transform?: any): this
/** @deprecated */
setMock(mock?: any): this
/** @deprecated */
setFixture(fixture?: any): this
/** @deprecated */
setData(data?: any): this
/** @deprecated */
toMock(args?: any): this
/**
* @protected
* @see https://github.com/apollographql/apollo-server/blob/master/packages/apollo-datasource-rest/src/RESTDataSource.ts#L85
*/
cacheOptionsFor(response: Response, request: Request): CacheOptions
toJSON(): OneRequestAsSerializedDebug
cache(value: string | false): this
}
export type AdapterType = {
doRequest<Type = any>(): Promise<Type>
[key: string]: any
}
export interface OneRequestAdapter<AdapterTypeParam = AdapterType> {
(store: OneRequestStore, oneRequest?: OneRequest): AdapterTypeParam
}
declare class PrivateOneRequest {
public toRequest<AdapterTypeParam>(
adapter?: OneRequestAdapter<AdapterTypeParam>
): AdapterTypeParam
public reset(): this
protected setDebug(shouldDebug: boolean): this
protected setUrl(url: string): this
protected setParams(params: Params): this
protected setMethod(method: Method): this
protected setHeaders(headers: Headers): this
protected setAuth(auth: Credentials): this
protected willSendRequest(requestOptions: RequestOptions): this
protected willSendResponse(response: Response): this
private _set(name: string, value: any): this
private _get(name: string): any
private _has(name: string): boolean
private _delete(name: string): this
}
declare class FuturePossibleOneRequest {
onRequest(): this
onResponse(): this
// tapRequest(tapRequest: TapRequest): this
// tapResponse(tapResponse: TapResponse): this
// @alias tapForward
// tapProxy(tapProxy: TapProxy): this
public onSuccess(): this
public onError(): this
}
export interface CommonChildren {
update?: OneRequest
delete?: OneRequest
create?: OneRequest
list?: OneRequest
}
export interface ConfigMap extends Map<string, any> {
get(key: 'baseUrl'): string
set(key: 'baseUrl', value: string): this
get(key: 'partnerId'): string
set(key: 'partnerId', value: string): this
get(key: 'campaignId'): string
set(key: 'campaignId', value: string): this
get(key: 'storeId'): string
set(key: 'storeId', value: string): this
get(key: 'locale'): string
set(key: 'locale', value: string): this
get(key: 'defaultParams'): AnyObj
set(key: 'defaultParams', value: AnyObj): this
get(key: 'constantHeaders'): AnyObj
set(key: 'constantHeaders', value: AnyObj): this
get(key: 'shouldEncodeGetWithFormBody'): boolean
set(key: 'shouldEncodeGetWithFormBody', value: boolean): this
set(key: 'defaultContext', value: OneRequestContextBase): this
get(key: 'defaultContext'): OneRequestContextBase
get<Type = any>(key: string): Type
set<Type = any>(key: string, value: Type): this
}
/**
* @api https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
*/
export interface CacheOptions {
ttl?: number
}