-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetching schema by url in library (#34)
- Loading branch information
1 parent
f36a5f6
commit f625e3a
Showing
7 changed files
with
84 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export interface FetchingSchemaInterface { | ||
url: string; | ||
requestOptions?: RequestInit; | ||
} | ||
|
||
export function isFetchingSchemaInterface(schema: any) { | ||
return (<FetchingSchemaInterface>schema).url !== undefined; | ||
} | ||
|
||
const defaultRequestOptions: RequestInit = { | ||
method: 'GET', | ||
}; | ||
|
||
export const fetchSchema = async ({ | ||
url, | ||
requestOptions = defaultRequestOptions, | ||
}: FetchingSchemaInterface): Promise<any> => { | ||
return fetch(url, requestOptions).then(handleResponse); | ||
}; | ||
|
||
function handleResponse(response: any) { | ||
return response.text().then((data: string) => data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './parser'; | ||
export * from './beautifier'; | ||
export * from './beautifier'; | ||
export * from './fetchSchema'; | ||
export * from './json-parser'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export const parse = <T extends {}>(str?: string): T => { | ||
if (!str) return {} as T; | ||
|
||
try { | ||
return JSON.parse(str) as T; | ||
} catch (e) { | ||
return {} as T; | ||
} | ||
}; | ||
|
||
export const stringify = <T extends {}>(content?: T): string => { | ||
if (!content) return ''; | ||
|
||
try { | ||
return JSON.stringify(content); | ||
} catch (e) { | ||
return ''; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters