Skip to content

Commit f013155

Browse files
Alan-ChaErikWittern
authored andcommitted
Add Typescript definition file
Related to #113 Signed-off-by: Alan Cha <[email protected]>
1 parent 168772a commit f013155

26 files changed

+998
-39
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Functions to create viewers that allow users to pass credentials to resolve
3+
* functions used by OASGraph.
4+
*/
5+
import { Oas3 } from './types/oas3';
6+
import { GraphQLObjectType as GQObjectType } from 'graphql';
7+
import { Args, ResolveFunction } from './types/graphql';
8+
import { PreprocessingData } from './types/preprocessing_data.js';
9+
declare type Viewer = {
10+
type: GQObjectType;
11+
resolve: ResolveFunction;
12+
args: Args;
13+
description: string;
14+
};
15+
/**
16+
* Load the field object in the appropriate root object
17+
*
18+
* i.e. inside either rootQueryFields/rootMutationFields or inside
19+
* rootQueryFields/rootMutationFields for further processing
20+
*/
21+
export declare function createAndLoadViewer(queryFields: Object, data: PreprocessingData, oas: Oas3, isMutation?: boolean): {
22+
[key: string]: Viewer;
23+
};
24+
export {};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Utilities related to GraphQL.
3+
*/
4+
import { GraphQLObjectType as GQObjectType, GraphQLInputObjectType as GQInputObjectType } from 'graphql';
5+
/**
6+
* Returns empty GraphQLObjectType.
7+
*/
8+
export declare function getEmptyObjectType(name: string): GQObjectType;
9+
/**
10+
* Returns empty GraphQLInputObjectType.
11+
*/
12+
export declare function getEmptyInputObjectType(): GQInputObjectType;

packages/oasgraph/lib/index.d.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Defines the functions exposed by OASGraph.
3+
*
4+
* Some general notes:
5+
*
6+
* - GraphQL interfaces rely on sanitized strings for (Input) Object Type names
7+
* and fields. We perform sanitization only when assigning (field-) names, but
8+
* keep keys in the OAS otherwise as-is, to ensure that inner-OAS references
9+
* work as expected.
10+
*
11+
* - GraphQL (Input) Object Types must have a unique name. Thus, sometimes Input
12+
* Object Types and Object Types need separate names, despite them having the
13+
* same structure. We thus append 'Input' to every Input Object Type's name
14+
* as a convention.
15+
*
16+
* - To pass data between resolve functions, OASGraph uses a _oasgraph object
17+
* returned by every resolver in addition to its original data (OASGraph does
18+
* not use the context to do so, which is an anti-pattern according to=
19+
* https://github.com/graphql/graphql-js/issues/953).
20+
*
21+
* - OasGraph can handle basic authentication and api key-based authentication
22+
* through GraphQL. To do this, OASGraph creates two new intermediate Object
23+
* Types called QueryViewer and MutationViewer that take as input security
24+
* credentials and pass them on using the _oasgraph object to other resolve
25+
* functions.
26+
*/
27+
import { Options, Report } from './types/options';
28+
import { Oas3 } from './types/oas3';
29+
import { Oas2 } from './types/oas2';
30+
import { GraphQLSchema } from 'graphql';
31+
declare type Result = {
32+
schema: GraphQLSchema;
33+
report: Report;
34+
};
35+
/**
36+
* Creates a GraphQL interface from the given OpenAPI Specification (2 or 3).
37+
*/
38+
export declare function createGraphQlSchema(spec: Oas3 | Oas2, options?: Options): Promise<Result>;
39+
export {};

packages/oasgraph/lib/index.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* Utility functions around the OpenAPI Specification 3.
3+
*/
4+
import { Oas2 } from './types/oas2';
5+
import { Operation } from './types/operation';
6+
import { Oas3, ServerObject, ParameterObject, SchemaObject, OperationObject, ReferenceObject, LinkObject, SecuritySchemeObject } from './types/oas3.js';
7+
import { PreprocessingData } from './types/preprocessing_data';
8+
import { InternalOptions } from './types/options';
9+
export declare type SchemaNames = {
10+
fromPath?: string;
11+
fromSchema?: string;
12+
fromRef?: string;
13+
};
14+
export declare type RequestSchemaAndNames = {
15+
payloadContentType?: string;
16+
payloadSchema?: SchemaObject | ReferenceObject;
17+
payloadSchemaNames?: SchemaNames;
18+
payloadRequired: boolean;
19+
};
20+
export declare type ResponseSchemaAndNames = {
21+
responseContentType?: string;
22+
responseSchema?: SchemaObject | ReferenceObject;
23+
responseSchemaNames?: SchemaNames;
24+
statusCode?: string;
25+
};
26+
export declare const OAS_OPERATIONS: string[];
27+
export declare const SUCCESS_STATUS_RX: RegExp;
28+
/**
29+
* Resolves on a validated OAS 3 for the given spec (OAS 2 or OAS 3), or rejects
30+
* if errors occur.
31+
*/
32+
export declare function getValidOAS3(spec: Oas2 | Oas3): Promise<Oas3>;
33+
/**
34+
* Counts the number of operations in an OAS.
35+
*/
36+
export declare function countOperations(oas: Oas3): number;
37+
/**
38+
* Counts the number of operations that translate to queries in an OAS.
39+
*/
40+
export declare function countOperationsQuery(oas: Oas3): number;
41+
/**
42+
* Counts the number of operations that translate to mutations in an OAS.
43+
*/
44+
export declare function countOperationsMutation(oas: Oas3): number;
45+
/**
46+
* Counts the number of operations with a payload definition in an OAS.
47+
*/
48+
export declare function countOperationsWithPayload(oas: Oas3): number;
49+
/**
50+
* Resolves the given reference in the given object.
51+
*/
52+
export declare function resolveRef(ref: string, obj: Object, parts?: string[]): any;
53+
/**
54+
* From the given OAS, returns the base URL to use for the given operation.
55+
*/
56+
export declare function getBaseUrl(oas: Oas3, operation: Operation): string;
57+
/**
58+
* Returns object | array where all object keys are sanitized. Keys passed in
59+
* exceptions are not sanitized.
60+
*/
61+
export declare function sanitizeObjKeys(obj: Object | Array<any>, exceptions?: string[]): Object | Array<any>;
62+
/**
63+
* Desanitizes keys in given object by replacing them with the keys stored in
64+
* the given mapping.
65+
*/
66+
export declare function desanitizeObjKeys(obj: Object | Array<any>, mapping?: Object): Object | Array<any>;
67+
/**
68+
* Replaces the path parameter in the given path with values in the given args.
69+
* Furthermore adds the query parameters for a request.
70+
*/
71+
export declare function instantiatePathAndGetQuery(path: string, parameters: ParameterObject[], args: Object): {
72+
path: string;
73+
query: {
74+
[key: string]: string;
75+
};
76+
headers: {
77+
[key: string]: string;
78+
};
79+
};
80+
/**
81+
* Returns the "type" of the given JSON schema. Makes best guesses if the type
82+
* is not explicitly defined.
83+
*/
84+
export declare function getSchemaType(schema: SchemaObject): string | null;
85+
/**
86+
* Determines an approximate name for the resource at the given path.
87+
*/
88+
export declare function inferResourceNameFromPath(path: string): string;
89+
/**
90+
* Returns JSON-compatible schema required by the given endpoint - or null if it
91+
* does not exist.
92+
*/
93+
export declare function getRequestSchema(endpoint: OperationObject, oas: Oas3): {
94+
payloadContentType: string;
95+
payloadSchema: SchemaObject;
96+
} | null;
97+
/**
98+
* Returns the request schema (if any) for endpoint at given path and method, a
99+
* dictionary of names from different sources (if available), and whether the
100+
* request schema is required for the endpoint.
101+
*/
102+
export declare function getRequestSchemaAndNames(path: string, method: string, oas: Oas3): RequestSchemaAndNames;
103+
/**
104+
* Returns JSON-compatible schema produced by the given endpoint - or null if it
105+
* does not exist.
106+
*/
107+
export declare function getResponseSchema(endpoint: OperationObject, statusCode: string, oas: Oas3): {
108+
responseContentType: string;
109+
responseSchema: SchemaObject;
110+
} | null;
111+
/**
112+
* Returns the response schema for endpoint at given path and method and with
113+
* the given status code, and a dictionary of names from different sources (if
114+
* available).
115+
*/
116+
export declare function getResponseSchemaAndNames(path: string, method: string, oas: Oas3, data: PreprocessingData, options: InternalOptions): ResponseSchemaAndNames;
117+
/**
118+
* Returns the success status code for the operation at the given path and
119+
* method (or null).
120+
*/
121+
export declare function getResponseStatusCode(path: string, method: string, oas: Oas3, data: PreprocessingData): string | void;
122+
/**
123+
* Returns an hash containing the links defined in the given endpoint.
124+
*/
125+
export declare function getEndpointLinks(path: string, method: string, oas: Oas3, data: PreprocessingData): {
126+
[key: string]: LinkObject;
127+
};
128+
/**
129+
* Returns the list of parameters for the endpoint at the given method and path.
130+
* Resolves possible references.
131+
*/
132+
export declare function getParameters(path: string, method: string, oas: Oas3): ParameterObject[];
133+
/**
134+
* Returns an array of server objects for the opeartion at the given path and
135+
* method. Considers in the following order: global server definitions,
136+
* definitions at the path item, definitions at the operation, or the OAS
137+
* default.
138+
*/
139+
export declare function getServers(path: string, method: string, oas: Oas3): ServerObject[];
140+
/**
141+
* Returns a map of Security Scheme definitions, identified by keys. Resolves
142+
* possible references.
143+
*/
144+
export declare function getSecuritySchemes(oas: Oas3): {
145+
[key: string]: SecuritySchemeObject;
146+
};
147+
/**
148+
* Returns the list of BEAUTIFIED keys of NON-OAUTH 2 security schemes
149+
* required by the operation at the given path and method.
150+
*/
151+
export declare function getSecurityRequirements(path: string, method: string, securitySchemes: {
152+
[key: string]: SecuritySchemeObject;
153+
}, oas: Oas3): string[];
154+
/**
155+
* Beautifies the given string and stores the sanitized-to-original mapping in
156+
* the given mapping.
157+
*/
158+
export declare function beautifyAndStore(str: string, mapping: {
159+
[key: string]: string;
160+
}): string;
161+
/**
162+
* First sanitizes given string and then also camel-cases it.
163+
*/
164+
export declare function beautify(str: string, lowercaseFirstChar?: boolean): string;
165+
/**
166+
* Stringifies and possibly trims the given string to the provided length.
167+
*/
168+
export declare function trim(str: string, length: number): string;
169+
/**
170+
* Determines if the given "method" is indeed an operation. Alternatively, the
171+
* method could point to other types of information (e.g., parameters, servers).
172+
*/
173+
export declare function isOperation(method: string): boolean;
174+
/**
175+
* Capitalizes a given string
176+
*/
177+
export declare function capitalize(str: string): string;
178+
/**
179+
* Uncapitalizes a given string
180+
*/
181+
export declare function uncapitalize(str: string): string;

packages/oasgraph/lib/oas_3_tools.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Oas3, SchemaObject } from './types/oas3';
2+
import { InternalOptions } from './types/options';
3+
import { DataDefinition } from './types/operation';
4+
import { PreprocessingData } from './types/preprocessing_data';
5+
import * as Oas3Tools from './oas_3_tools';
6+
/**
7+
* Extract information from the OAS and put it inside a data structure that
8+
* is easier for OASGraph to use
9+
*/
10+
export declare function preprocessOas(oas: Oas3, options: InternalOptions): PreprocessingData;
11+
/**
12+
* Method to either create a new or reuse an existing, centrally stored data
13+
* definition. Data definitions are objects that hold a schema (= JSON schema),
14+
* an otName (= String to use as the name for Object Types), and an iotName
15+
* (= String to use as the name for Input Object Types). Eventually, data
16+
* definitions also hold an ot (= the Object Type for the schema) and an iot
17+
* (= the Input Object Type for the schema).
18+
*/
19+
export declare function createOrReuseDataDef(data: PreprocessingData, schema?: SchemaObject, names?: Oas3Tools.SchemaNames): DataDefinition;

0 commit comments

Comments
 (0)