Repository URL to install this package:
|
Version:
7.1.2 ▾
|
import { URL, URLSearchParams } from 'url'
import {
RESTDataSource,
RequestOptions,
HTTPCache,
} from 'apollo-datasource-rest'
import { Request as ExpressRequestBase } from 'express'
import { Response as ExpressResponseBase } from 'express'
export { URL, URLSearchParams } from 'url'
export interface ExpressRequest extends ExpressRequestBase {
referer?: string
hash?: string
cookie?: string
_parsedOriginalUrl?: {
// @todo
[key: string]: string
}
_parsedUrl?: {
// @todo
[key: string]: string
}
}
export type ExpressResponse = ExpressResponseBase
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?: LoggerType
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
/** @todo - was not here before... */
get(key: 'basePath'): string
/** @todo - was not here before... */
set(key: 'basePath', 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 OneRequestBodyArg = string | RequestBody
export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'
export type TransformParams<ParamsIn = ParamsObj, ParamsOut = ParamsObj> = (
params: ParamsIn
) => ParamsOut
// could be any Serialized
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 type TransformResponse<
ResponseIn = ResponseDataIn,
ResponseOut = ResponseDataOut
> = (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 = 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 type 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<ResponseExtended>
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: OneRequestBodyArg): 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 interface AdapterType {
doRequest<Type = any>(): Promise<Type>
[key: string]: any
}
export type 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
set(key: 'defaultCache', value: CacheOptions): this
get(key: 'defaultCache'): CacheOptions
set(key: 'logger', value: LoggerType): this
get(key: 'logger'): LoggerType
get<Type = any>(key: string): Type
set<Type = any>(key: string, value: Type): this
}
// what our default is...
export type LoggerType = Logger
// | {
// warn: typeof console.warn
// error: typeof console.error
// info: typeof console.info
// debug: typeof console.debug
// }
/**
* @api https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
*/
export interface CacheOptions {
ttl?: number
}
export interface DefaultOneResponseError {
code?: number | string
message?: string
errors?: AnyObj[]
timestamp?: number
}
export type DefaultOneResponseData = DefaultOneResponseError
export interface OneResponse<Data = AnyObj & DefaultOneResponseData>
extends Body,
Response {
// readonly body?: {
// [key: string]: any
// }
readonly 'Symbol(Body internals)': {
readonly headers: Headers
readonly status: number
readonly statusText: string
readonly url: string
readonly type?: ResponseType
}
readonly prettyError?: DefaultOneResponseError
readonly data?: Data
}
export interface OneResponseSerialized<Data = AnyObj & DefaultOneResponseData>
extends Body,
Response {
'Symbol(Body internals)': {
readonly headers: Headers
readonly status: number
readonly statusText: string
readonly url: string
readonly type?: ResponseType
}
prettyError?: DefaultOneResponseError
data?: Data
// status
// statusText
}
export interface OneFetchRequestAdapted {
/** @description takes buffer|nested obj & hoists|serializes it */
body: { [key: string]: any }
method: Method
/** @description fromHeadersToSerialized */
headers: { [key: string]: string | string[] }
/** @description const { href } = new URL('...') */
url: string
cache?: RequestCache
mode?: RequestMode
referrer?: string
// credentials?: RequestCredentials
}
export type Primitive =
| string
| number
| boolean
| symbol
| null
| undefined
| void
export interface SerializedObj {
[key: string]: Primitive | FrozenObj | FrozenArray
[key: number]: Primitive | FrozenObj | FrozenArray
}
export type FrozenObj = Readonly<SerializedObj>
export interface FrozenArray extends Readonly<Serialized[]> {
readonly(x: number): Serialized
}
export type Serialized = SerializedObj | Primitive | FrozenObj | FrozenArray
export type ResponseAddedPropsDataType =
| {
[key: string]: Serialized
}
| string
export interface ResponseAddedProps<DataType> {
body?: any
// @todo @name
// prettyErr?: any
prettyError?: any
data?: DataType
// @todo type better
toDebug: () => { [key: string]: any }
}
// @todo order of compositional types as overrides
export type ResponseExtended<Data = ResponseAddedPropsDataType> = Response &
ResponseAddedProps<Data>