-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract import identifier functions
- Loading branch information
Showing
10 changed files
with
147 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
"license": "MIT", | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "Lubos Menus", | ||
"url": "https://lmen.us" | ||
"name": "Hey API", | ||
"url": "https://heyapi.dev" | ||
}, | ||
"funding": "https://github.com/sponsors/hey-api", | ||
"type": "module", | ||
|
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 |
---|---|---|
|
@@ -13,8 +13,8 @@ | |
"license": "MIT", | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "Lubos Menus", | ||
"url": "https://lmen.us" | ||
"name": "Hey API", | ||
"url": "https://heyapi.dev" | ||
}, | ||
"funding": "https://github.com/sponsors/hey-api", | ||
"keywords": [ | ||
|
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 |
---|---|---|
|
@@ -13,8 +13,8 @@ | |
"license": "MIT", | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "Lubos Menus", | ||
"url": "https://lmen.us" | ||
"name": "Hey API", | ||
"url": "https://heyapi.dev" | ||
}, | ||
"funding": "https://github.com/sponsors/hey-api", | ||
"keywords": [ | ||
|
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
74 changes: 74 additions & 0 deletions
74
packages/openapi-ts/src/plugins/@hey-api/typescript/ref.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,74 @@ | ||
import type { TypeScriptFile } from '../../../generate/files'; | ||
import type { IR } from '../../../ir/types'; | ||
import { operationIrRef } from '../../shared/utils/ref'; | ||
|
||
export const typesId = 'types'; | ||
|
||
export const importIdentifierData = ({ | ||
context, | ||
file, | ||
operation, | ||
}: { | ||
context: IR.Context; | ||
file: TypeScriptFile; | ||
operation: IR.OperationObject; | ||
}): ReturnType<TypeScriptFile['identifier']> => { | ||
const identifierData = context.file({ id: 'types' })!.identifier({ | ||
$ref: operationIrRef({ id: operation.id, type: 'data' }), | ||
namespace: 'type', | ||
}); | ||
if (identifierData.name) { | ||
file.import({ | ||
asType: true, | ||
module: file.relativePathToFile({ context, id: 'types' }), | ||
name: identifierData.name, | ||
}); | ||
} | ||
return identifierData; | ||
}; | ||
|
||
export const importIdentifierError = ({ | ||
context, | ||
file, | ||
operation, | ||
}: { | ||
context: IR.Context; | ||
file: TypeScriptFile; | ||
operation: IR.OperationObject; | ||
}): ReturnType<TypeScriptFile['identifier']> => { | ||
const identifierError = context.file({ id: 'types' })!.identifier({ | ||
$ref: operationIrRef({ id: operation.id, type: 'error' }), | ||
namespace: 'type', | ||
}); | ||
if (identifierError.name) { | ||
file.import({ | ||
asType: true, | ||
module: file.relativePathToFile({ context, id: 'types' }), | ||
name: identifierError.name, | ||
}); | ||
} | ||
return identifierError; | ||
}; | ||
|
||
export const importIdentifierResponse = ({ | ||
context, | ||
file, | ||
operation, | ||
}: { | ||
context: IR.Context; | ||
file: TypeScriptFile; | ||
operation: IR.OperationObject; | ||
}): ReturnType<TypeScriptFile['identifier']> => { | ||
const identifierResponse = context.file({ id: 'types' })!.identifier({ | ||
$ref: operationIrRef({ id: operation.id, type: 'response' }), | ||
namespace: 'type', | ||
}); | ||
if (identifierResponse.name) { | ||
file.import({ | ||
asType: true, | ||
module: file.relativePathToFile({ context, id: 'types' }), | ||
name: identifierResponse.name, | ||
}); | ||
} | ||
return identifierResponse; | ||
}; |
Oops, something went wrong.