-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typescript): introduce API Promise so that we can return respons…
…e headers
- Loading branch information
Showing
40 changed files
with
463 additions
and
1,586 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
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
7 changes: 7 additions & 0 deletions
7
generators/typescript/utils/commons/src/core-utilities/api-promise/APIPromise.ts
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,7 @@ | ||
import { ts } from "ts-morph"; | ||
|
||
export interface APIPromise { | ||
_getReferenceToType: (response: ts.TypeNode) => ts.TypeNode; | ||
|
||
from: (statements: ts.Statement[]) => ts.Expression; | ||
} |
49 changes: 49 additions & 0 deletions
49
generators/typescript/utils/commons/src/core-utilities/api-promise/APIPromiseImpl.ts
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,49 @@ | ||
import { AbsoluteFilePath, RelativeFilePath } from "@fern-api/fs-utils"; | ||
import { ts } from "ts-morph"; | ||
import { DependencyManager } from "../../dependency-manager/DependencyManager"; | ||
import { CoreUtility } from "../CoreUtility"; | ||
import { APIPromise } from "./APIPromise"; | ||
|
||
export class APIPromiseImpl extends CoreUtility implements APIPromise { | ||
public readonly MANIFEST = { | ||
name: "api-promise", | ||
repoInfoForTesting: { | ||
path: RelativeFilePath.of("generators/typescript/utils/core-utilities/fetcher/src/runtime") | ||
}, | ||
originalPathOnDocker: AbsoluteFilePath.of("/assets/fetcher/api-promise"), | ||
pathInCoreUtilities: [{ nameOnDisk: "api-promise", exportDeclaration: { exportAll: true } }], | ||
addDependencies: (dependencyManager: DependencyManager): void => { | ||
return; | ||
}, | ||
}; | ||
|
||
public _getReferenceToType = (response: ts.TypeNode): ts.TypeNode => { | ||
return ts.factory.createTypeReferenceNode("APIPromise", [response]); | ||
}; | ||
|
||
public from = (body: ts.Statement[]): ts.Expression => { | ||
return ts.factory.createCallExpression( | ||
ts.factory.createPropertyAccessExpression( | ||
ts.factory.createIdentifier("APIPromise"), | ||
ts.factory.createIdentifier("from") | ||
), | ||
undefined, | ||
[ | ||
ts.factory.createCallExpression( | ||
ts.factory.createParenthesizedExpression( | ||
ts.factory.createArrowFunction( | ||
[ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)], | ||
undefined, | ||
[], | ||
undefined, | ||
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), | ||
ts.factory.createBlock(body, true) | ||
) | ||
), | ||
undefined, | ||
[] | ||
) | ||
] | ||
); | ||
}; | ||
} |
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
54 changes: 54 additions & 0 deletions
54
generators/typescript/utils/core-utilities/fetcher/src/api-promise/APIPromise.ts
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,54 @@ | ||
import { APIResponse } from "../fetcher/APIResponse"; | ||
|
||
/** | ||
* APIPromise wraps a Promise that resolves with an APIResponse. | ||
* It provides convenient methods for handling both successful responses and errors. | ||
* | ||
* By default, when awaited, it will return just the response body data. | ||
* Use the `asRaw()` method to get access to both the response data and headers. | ||
* | ||
* @example | ||
* // Get just the response data | ||
* const data = await apiPromise; | ||
* | ||
* // Get response with headers | ||
* const { data, headers } = await apiPromise.asRaw(); | ||
* | ||
* @template T The type of the successful response body | ||
*/ | ||
export class APIPromise<T> extends Promise<T> { | ||
constructor( | ||
private readonly responsePromise: Promise<APIResponse<T, unknown>>, | ||
executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void | ||
) { | ||
super(executor); | ||
} | ||
|
||
public async asRaw(): Promise<{ | ||
data: T; | ||
headers?: Record<string, any>; | ||
}> { | ||
const response = await this.responsePromise; | ||
if (!response.ok) { | ||
throw response.error; | ||
} | ||
return { | ||
data: response.body, | ||
headers: response.headers | ||
}; | ||
} | ||
|
||
public static from<T>(responsePromise: Promise<APIResponse<T, unknown>>): APIPromise<T> { | ||
return new APIPromise(responsePromise, (resolve, reject) => { | ||
responsePromise | ||
.then((response) => { | ||
if (response.ok) { | ||
resolve(response.body); | ||
} else { | ||
reject(response.error); | ||
} | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
generators/typescript/utils/core-utilities/fetcher/src/api-promise/index.ts
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 @@ | ||
export { APIPromise } from "./APIPromise"; |
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
Oops, something went wrong.