UNPKG

8.1 kBTypeScriptView Raw
1/**
2 * Utility functions around the OpenAPI Specification 3.
3 */
4import { Oas2 } from './types/oas2';
5import { Operation } from './types/operation';
6import { Oas3, ServerObject, ParameterObject, SchemaObject, OperationObject, ResponseObject, PathItemObject, RequestBodyObject, ReferenceObject, LinkObject, SecuritySchemeObject } from './types/oas3';
7import { PreprocessingData, ProcessedSecurityScheme } from './types/preprocessing_data';
8import { InternalOptions } from './types/options';
9export declare type SchemaNames = {
10 fromRef?: string;
11 fromSchema?: string;
12 fromPath?: string;
13 /**
14 * Used when the preferred name is known, i.e. a new data def does not need to
15 * be created
16 */
17 preferred?: string;
18};
19export declare type RequestSchemaAndNames = {
20 payloadContentType?: string;
21 payloadSchema?: SchemaObject | ReferenceObject;
22 payloadSchemaNames?: SchemaNames;
23 payloadRequired: boolean;
24};
25export declare type ResponseSchemaAndNames = {
26 responseContentType?: string;
27 responseSchema?: SchemaObject | ReferenceObject;
28 responseSchemaNames?: SchemaNames;
29 statusCode?: string;
30};
31export declare enum HTTP_METHODS {
32 'get' = "get",
33 'put' = "put",
34 'post' = "post",
35 'patch' = "patch",
36 'delete' = "delete",
37 'options' = "options",
38 'head' = "head"
39}
40export declare const SUCCESS_STATUS_RX: RegExp;
41/**
42 * Given an HTTP method, convert it to the HTTP_METHODS enum
43 */
44export declare function methodToHttpMethod(method: string): HTTP_METHODS;
45/**
46 * Resolves on a validated OAS 3 for the given spec (OAS 2 or OAS 3), or rejects
47 * if errors occur.
48 */
49export declare function getValidOAS3(spec: Oas2 | Oas3): Promise<Oas3>;
50/**
51 * Counts the number of operations in an OAS.
52 */
53export declare function countOperations(oas: Oas3): number;
54/**
55 * Counts the number of operations that translate to queries in an OAS.
56 */
57export declare function countOperationsQuery(oas: Oas3): number;
58/**
59 * Counts the number of operations that translate to mutations in an OAS.
60 */
61export declare function countOperationsMutation(oas: Oas3): number;
62/**
63 * Counts the number of operations that translate to subscriptions in an OAS.
64 */
65export declare function countOperationsSubscription(oas: Oas3): number;
66/**
67 * Counts the number of operations with a payload definition in an OAS.
68 */
69export declare function countOperationsWithPayload(oas: Oas3): number;
70/**
71 * Resolves the given reference in the given object.
72 */
73export declare function resolveRef(ref: string, oas: Oas3): any;
74/**
75 * Returns the base URL to use for the given operation.
76 */
77export declare function getBaseUrl(operation: Operation): string;
78/**
79 * Returns object/array/scalar where all object keys (if applicable) are
80 * sanitized.
81 */
82export declare function sanitizeObjectKeys(obj: any, // obj does not necessarily need to be an object
83caseStyle?: CaseStyle): any;
84/**
85 * Desanitizes keys in given object by replacing them with the keys stored in
86 * the given mapping.
87 */
88export declare function desanitizeObjectKeys(obj: object | Array<any>, mapping?: object): object | Array<any>;
89/**
90 * Returns the GraphQL type that the provided schema should be made into
91 *
92 * Does not consider allOf, anyOf, oneOf, or not (handled separately)
93 */
94export declare function getSchemaTargetGraphQLType<TSource, TContext, TArgs>(schema: SchemaObject, data: PreprocessingData<TSource, TContext, TArgs>): string | null;
95/**
96 * Infers a resource name from the given URL path.
97 *
98 * For example, turns "/users/{userId}/car" into "userCar".
99 */
100export declare function inferResourceNameFromPath(path: string): string;
101/**
102 * Returns JSON-compatible schema required by the given operation
103 */
104export declare function getRequestBodyObject(operation: OperationObject, oas: Oas3): {
105 payloadContentType: string;
106 requestBodyObject: RequestBodyObject;
107} | null;
108/**
109 * Returns the request schema (if any) for the given operation,
110 * a dictionary of names from different sources (if available), and whether the
111 * request schema is required for the operation.
112 */
113export declare function getRequestSchemaAndNames(path: string, operation: OperationObject, oas: Oas3): RequestSchemaAndNames;
114/**
115 * Returns JSON-compatible schema produced by the given operation
116 */
117export declare function getResponseObject(operation: OperationObject, statusCode: string, oas: Oas3): {
118 responseContentType: string;
119 responseObject: ResponseObject;
120} | null;
121/**
122 * Returns the response schema for the given operation,
123 * a successful status code, and a dictionary of names from different sources
124 * (if available).
125 */
126export declare function getResponseSchemaAndNames<TSource, TContext, TArgs>(path: string, method: HTTP_METHODS, operation: OperationObject, oas: Oas3, data: PreprocessingData<TSource, TContext, TArgs>, options: InternalOptions<TSource, TContext, TArgs>): ResponseSchemaAndNames;
127/**
128 * Returns a success status code for the given operation
129 */
130export declare function getResponseStatusCode<TSource, TContext, TArgs>(path: string, method: string, operation: OperationObject, oas: Oas3, data: PreprocessingData<TSource, TContext, TArgs>): string | void;
131/**
132 * Returns a hash containing the links in the given operation.
133 */
134export declare function getLinks<TSource, TContext, TArgs>(path: string, method: HTTP_METHODS, operation: OperationObject, oas: Oas3, data: PreprocessingData<TSource, TContext, TArgs>): {
135 [key: string]: LinkObject;
136};
137/**
138 * Returns the list of parameters in the given operation.
139 */
140export declare function getParameters(path: string, method: HTTP_METHODS, operation: OperationObject, pathItem: PathItemObject, oas: Oas3): ParameterObject[];
141/**
142 * Returns an array of server objects for the operation at the given path and
143 * method. Considers in the following order: global server definitions,
144 * definitions at the path item, definitions at the operation, or the OAS
145 * default.
146 */
147export declare function getServers(operation: OperationObject, pathItem: PathItemObject, oas: Oas3): ServerObject[];
148/**
149 * Returns a map of Security Scheme definitions, identified by keys. Resolves
150 * possible references.
151 */
152export declare function getSecuritySchemes(oas: Oas3): {
153 [key: string]: SecuritySchemeObject;
154};
155/**
156 * Returns the list of sanitized keys of non-OAuth2 security schemes
157 * required by the operation at the given path and method.
158 */
159export declare function getSecurityRequirements(operation: OperationObject, securitySchemes: {
160 [key: string]: ProcessedSecurityScheme;
161}, oas: Oas3): string[];
162export declare enum CaseStyle {
163 simple = 0,
164 PascalCase = 1,
165 camelCase = 2,
166 ALL_CAPS = 3
167}
168/**
169 * First sanitizes given string and then also camel-cases it.
170 */
171export declare function sanitize(str: string, caseStyle: CaseStyle): string;
172/**
173 * Sanitizes the given string and stores the sanitized-to-original mapping in
174 * the given mapping.
175 */
176export declare function storeSaneName(saneStr: string, str: string, mapping: {
177 [key: string]: string;
178}): string;
179/**
180 * Stringifies and possibly trims the given string to the provided length.
181 */
182export declare function trim(str: string, length: number): string;
183/**
184 * Determines if the given "method" is indeed an operation. Alternatively, the
185 * method could point to other types of information (e.g., parameters, servers).
186 */
187export declare function isHttpMethod(method: string): boolean;
188/**
189 * Formats a string that describes an operation in the form:
190 * {name of OAS} {HTTP method in ALL_CAPS} {operation path}
191 *
192 * Also used in preprocessing.ts where Operation objects are being constructed
193 */
194export declare function formatOperationString(method: string, path: string, title?: string): string;
195/**
196 * Capitalizes a given string
197 */
198export declare function capitalize(str: string): string;
199/**
200 * Uncapitalizes a given string
201 */
202export declare function uncapitalize(str: string): string;
203/**
204 * For operations that do not have an operationId, generate one
205 */
206export declare function generateOperationId(method: HTTP_METHODS, path: string): string;