Skip to content

Commit 6dde828

Browse files
Alan-ChaErikWittern
authored andcommitted
Export sanitize
Small fixes Signed-off-by: Alan Cha <[email protected]>
1 parent 2127fd9 commit 6dde828

File tree

8 files changed

+54
-53
lines changed

8 files changed

+54
-53
lines changed

packages/openapi-to-graphql/lib/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ declare type Result = {
3636
* Creates a GraphQL interface from the given OpenAPI Specification (2 or 3).
3737
*/
3838
export declare function createGraphQlSchema(spec: Oas3 | Oas2 | (Oas3 | Oas2)[], options?: Options): Promise<Result>;
39-
export {};
39+
export { sanitize } from './oas_3_tools';

packages/openapi-to-graphql/lib/index.js

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

packages/openapi-to-graphql/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.

packages/openapi-to-graphql/lib/oas_3_tools.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export declare function getRequestSchema(endpoint: OperationObject, oas: Oas3):
100100
payloadSchema: SchemaObject;
101101
} | null;
102102
/**
103-
* Returns the request schema (if any) for endpoint at given path and method, a
104-
* dictionary of names from different sources (if available), and whether the
103+
* Returns the request schema (if any) for an endpoint at given path and method,
104+
* a dictionary of names from different sources (if available), and whether the
105105
* request schema is required for the endpoint.
106106
*/
107107
export declare function getRequestSchemaAndNames(path: string, method: string, oas: Oas3): RequestSchemaAndNames;

packages/openapi-to-graphql/lib/oas_3_tools.js

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

packages/openapi-to-graphql/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.

packages/openapi-to-graphql/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,5 @@ function preliminaryChecks(
539539
})
540540
}
541541
}
542+
543+
export { sanitize } from './oas_3_tools'

packages/openapi-to-graphql/src/oas_3_tools.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ export function getBaseUrl(operation: Operation): string {
248248
*/
249249
function buildUrl(server: ServerObject): string {
250250
let url = server.url
251+
252+
// Replace with variable defaults, if applicable
251253
if (
252254
typeof server.variables === 'object' &&
253255
Object.keys(server.variables).length > 0
@@ -466,7 +468,7 @@ export function inferResourceNameFromPath(path: string): string {
466468
}
467469
})
468470

469-
return sanitize(pathNoPathParams)
471+
return pathNoPathParams
470472
}
471473

472474
/**
@@ -493,19 +495,19 @@ export function getRequestSchema(
493495
if (typeof requestBody.content === 'object') {
494496
const content: MediaTypesObject = requestBody.content
495497

496-
// Prioritizes content-type JSON
498+
// Prioritize content-type JSON
497499
if (Object.keys(content).includes('application/json')) {
498500
return {
499501
payloadContentType: 'application/json',
500502
payloadSchema: content['application/json'].schema as SchemaObject
501503
}
502504
} else {
503-
// Picks a random content type
504-
for (let contentType in content) {
505-
return {
506-
payloadContentType: contentType,
507-
payloadSchema: content[contentType].schema as SchemaObject
508-
}
505+
// Pick first (random) content type
506+
const randomContentType = Object.keys(content)[0]
507+
508+
return {
509+
payloadContentType: randomContentType,
510+
payloadSchema: content[randomContentType].schema as SchemaObject
509511
}
510512
}
511513
}
@@ -514,8 +516,8 @@ export function getRequestSchema(
514516
}
515517

516518
/**
517-
* Returns the request schema (if any) for endpoint at given path and method, a
518-
* dictionary of names from different sources (if available), and whether the
519+
* Returns the request schema (if any) for an endpoint at given path and method,
520+
* a dictionary of names from different sources (if available), and whether the
519521
* request schema is required for the endpoint.
520522
*/
521523
export function getRequestSchemaAndNames(
@@ -557,12 +559,11 @@ export function getRequestSchemaAndNames(
557559
* Rather, interpret the request body as a string
558560
*/
559561
if (payloadContentType !== 'application/json') {
560-
let saneContentTypeName: string = ''
561-
const terms = payloadContentType.split('/')
562-
for (let index in terms) {
563-
saneContentTypeName +=
564-
terms[index].charAt(0).toUpperCase() + terms[index].slice(1)
565-
}
562+
const saneContentTypeName = uncapitalize(
563+
payloadContentType.split('/').reduce((name, term) => {
564+
return name + capitalize(term)
565+
})
566+
)
566567

567568
payloadSchemaNames = {
568569
fromPath: saneContentTypeName
@@ -624,19 +625,19 @@ export function getResponseSchema(
624625
if (response.content && typeof response.content !== 'undefined') {
625626
const content: MediaTypesObject = response.content
626627

627-
// Prioritizes content-type JSON
628+
// Prioritize content-type JSON
628629
if (Object.keys(content).includes('application/json')) {
629630
return {
630631
responseContentType: 'application/json',
631632
responseSchema: content['application/json'].schema as SchemaObject
632633
}
633634
} else {
634-
// Picks a random content type
635-
for (let contentType in content) {
636-
return {
637-
responseContentType: contentType,
638-
responseSchema: content[contentType].schema as SchemaObject
639-
}
635+
// Pick first (random) content type
636+
const randomContentType = Object.keys(content)[0]
637+
638+
return {
639+
responseContentType: randomContentType,
640+
responseSchema: content[randomContentType].schema as SchemaObject
640641
}
641642
}
642643
}

0 commit comments

Comments
 (0)