-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
feat(typescript-fetch): add docs support #21719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aheckmann
wants to merge
3
commits into
OpenAPITools:master
Choose a base branch
from
makeplane:typescript-fetch-docs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
137 changes: 109 additions & 28 deletions
137
modules/openapi-generator/src/main/resources/typescript-fetch/README.mustache
This file contains hidden or 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,46 +1,127 @@ | ||
## {{npmName}}@{{npmVersion}} | ||
# {{npmName}}@{{npmVersion}} | ||
|
||
This generator creates TypeScript/JavaScript client that utilizes [Fetch API](https://fetch.spec.whatwg.org/). The generated Node module can be used in the following environments: | ||
A TypeScript SDK client for the {{host}} API. | ||
|
||
Environment | ||
* Node.js | ||
* Webpack | ||
* Browserify | ||
## Usage | ||
|
||
Language level | ||
* ES5 - you must have a Promises/A+ library installed | ||
* ES6 | ||
First, install the SDK from npm. | ||
|
||
Module system | ||
* CommonJS | ||
* ES6 module system | ||
```bash | ||
npm install {{npmName}} --save | ||
``` | ||
|
||
Next, try it out. | ||
|
||
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}} | ||
```ts | ||
{{>api_example}} | ||
``` | ||
{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}} | ||
|
||
## Documentation | ||
|
||
### API Endpoints | ||
|
||
All URIs are relative to *{{basePath}}* | ||
|
||
| Class | Method | HTTP request | Description | ||
| ----- | ------ | ------------ | ------------- | ||
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{classname}}* | [**{{operationId}}**]({{apiDocPath}}/{{classname}}.md#{{operationIdLowerCase}}) | **{{httpMethod}}** {{path}} | {{summary}} | ||
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}} | ||
|
||
### Models | ||
|
||
{{#models}}{{#model}}- [{{{classname}}}]({{modelDocPath}}/{{{classname}}}.md){{/model}} | ||
{{/models}} | ||
|
||
### Authorization | ||
|
||
{{^authMethods}}Endpoints do not require authorization.{{/authMethods}} | ||
{{#hasAuthMethods}}Authentication schemes defined for the API:{{/hasAuthMethods}} | ||
{{#authMethods}} | ||
<a id="{{name}}{{#isOAuth}}-{{flow}}{{/isOAuth}}"></a> | ||
#### {{name}}{{#isOAuth}} {{flow}}{{/isOAuth}} | ||
|
||
{{#isApiKey}} | ||
|
||
- **Type**: API key | ||
- **API key parameter name**: `{{keyParamName}}` | ||
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}} | ||
{{/isApiKey}} | ||
{{#isBasicBasic}} | ||
|
||
- **Type**: HTTP basic authentication | ||
{{/isBasicBasic}} | ||
{{#isBasicBearer}} | ||
|
||
- **Type**: HTTP Bearer Token authentication{{#bearerFormat}} ({{{.}}}){{/bearerFormat}} | ||
{{/isBasicBearer}} | ||
{{#isHttpSignature}} | ||
|
||
It can be used in both TypeScript and JavaScript. In TypeScript, the definition will be automatically resolved via `package.json`. ([Reference](https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html)) | ||
- **Type**: HTTP signature authentication | ||
{{/isHttpSignature}} | ||
{{#isOAuth}} | ||
|
||
- **Type**: OAuth | ||
- **Flow**: {{flow}} | ||
- **Authorization URL**: {{authorizationUrl}} | ||
- **Scopes**: {{^scopes}}N/A{{/scopes}} | ||
{{#scopes}} - `{{scope}}`: {{description}} | ||
{{/scopes}} | ||
{{/isOAuth}} | ||
{{/authMethods}} | ||
|
||
## About | ||
|
||
This TypeScript SDK client supports the [Fetch API](https://fetch.spec.whatwg.org/) | ||
and is automatically generated by the | ||
[OpenAPI Generator](https://openapi-generator.tech) project: | ||
|
||
- API version: `{{appVersion}}` | ||
- Package version: `{{npmVersion}}` | ||
{{^hideGenerationTimestamp}} | ||
- Build date: `{{generatedDate}}` | ||
{{/hideGenerationTimestamp}} | ||
- Generator version: `{{generatorVersion}}` | ||
- Build package: `{{generatorClass}}` | ||
|
||
The generated npm module supports the following: | ||
|
||
- Environments | ||
* Node.js | ||
* Webpack | ||
* Browserify | ||
- Language levels | ||
* ES5 - you must have a Promises/A+ library installed | ||
* ES6 | ||
- Module systems | ||
* CommonJS | ||
* ES6 module system | ||
|
||
{{#infoUrl}} | ||
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}}) | ||
{{/infoUrl}} | ||
|
||
## Development | ||
|
||
### Building | ||
|
||
To build and compile the typescript sources to javascript use: | ||
``` | ||
To build the TypeScript source code, you need to have Node.js and npm installed. | ||
After cloning the repository, navigate to the project directory and run: | ||
|
||
```bash | ||
npm install | ||
npm run build | ||
``` | ||
|
||
### Publishing | ||
|
||
First build the package then run `npm publish` | ||
|
||
### Consuming | ||
|
||
navigate to the folder of your consuming project and run one of the following commands. | ||
Once you've built the package, you can publish it to npm: | ||
|
||
_published:_ | ||
|
||
``` | ||
npm install {{npmName}}@{{npmVersion}} --save | ||
```bash | ||
npm publish | ||
``` | ||
|
||
_unPublished (not recommended):_ | ||
## License | ||
|
||
``` | ||
npm install PATH_TO_GENERATED_PACKAGE --save | ||
``` | ||
[{{licenseInfo}}]({{{licenseUrl}}}) |
63 changes: 63 additions & 0 deletions
63
modules/openapi-generator/src/main/resources/typescript-fetch/api_doc.mustache
This file contains hidden or 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,63 @@ | ||
# {{classname}}{{#description}} | ||
|
||
{{.}}{{/description}} | ||
|
||
All URIs are relative to *{{basePath}}* | ||
|
||
| Method | HTTP request | Description | | ||
|------------- | ------------- | -------------| | ||
{{#operations}}{{#operation}}| [**{{operationId}}**]({{classname}}.md#{{operationIdLowerCase}}) | **{{httpMethod}}** {{commonPath}}{{path}} | {{summary}} | | ||
{{/operation}}{{/operations}} | ||
|
||
{{#operations}} | ||
{{#operation}} | ||
|
||
## {{operationId}} | ||
|
||
> {{#returnType}}{{.}} {{/returnType}}{{operationId}}({{#allParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}}) | ||
|
||
{{summary}}{{#notes}} | ||
|
||
{{.}}{{/notes}} | ||
|
||
### Example | ||
|
||
```ts | ||
{{>api_example}} | ||
``` | ||
|
||
### Parameters | ||
|
||
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#-last}} | ||
| Name | Type | Description | Notes | | ||
|------------- | ------------- | ------------- | -------------|{{/-last}}{{/allParams}} | ||
{{#allParams}}| **{{paramName}}** | {{#isEnum}}{{#allowableValues}}{{#values}}`{{{.}}}`{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}{{/isEnum}}{{^isEnum}}{{#isModel}}[{{baseType}}]({{baseType}}.md){{/isModel}}{{^isModel}}`{{{dataType}}}`{{/isModel}}{{/isEnum}} | {{description}} |{{^required}} [Optional]{{/required}}{{^isContainer}}{{#defaultValue}} [Defaults to `{{.}}`]{{/defaultValue}}{{/isContainer}}{{#allowableValues}} [Enum: {{#values}}{{{.}}}{{^-last}}, {{/-last}}{{/values}}]{{/allowableValues}} | | ||
{{/allParams}} | ||
|
||
### Return type | ||
|
||
{{#returnType}}{{#returnTypeIsPrimitive}}**{{{returnType}}}**{{/returnTypeIsPrimitive}}{{^returnTypeIsPrimitive}}[**{{returnType}}**]({{returnBaseType}}.md){{/returnTypeIsPrimitive}}{{/returnType}}{{^returnType}}`void` (Empty response body){{/returnType}} | ||
|
||
### Authorization | ||
|
||
{{^authMethods}}No authorization required{{/authMethods}}{{#authMethods}}[{{{name}}}{{#isOAuth}} {{flow}}{{/isOAuth}}](../README.md#{{{name}}}{{#isOAuth}}-{{flow}}{{/isOAuth}}){{^-last}}, {{/-last}}{{/authMethods}} | ||
|
||
### HTTP request headers | ||
|
||
- **Content-Type**: {{#consumes}}`{{{mediaType}}}`{{^-last}}, {{/-last}}{{/consumes}}{{^consumes}}Not defined{{/consumes}} | ||
- **Accept**: {{#produces}}`{{{mediaType}}}`{{^-last}}, {{/-last}}{{/produces}}{{^produces}}Not defined{{/produces}} | ||
|
||
{{#responses.0}} | ||
|
||
### HTTP response details | ||
| Status code | Description | Response headers | | ||
|-------------|-------------|------------------| | ||
{{#responses}} | ||
| **{{code}}** | {{message}} | {{#headers}} * {{baseName}} - {{description}} <br> {{/headers}}{{^headers.0}} - {{/headers.0}} | | ||
{{/responses}} | ||
{{/responses.0}} | ||
|
||
[[Back to top]](#) [[Back to API list]](../README.md#api-endpoints) [[Back to Model list]](../README.md#models) [[Back to README]](../README.md) | ||
|
||
{{/operation}} | ||
{{/operations}} |
44 changes: 44 additions & 0 deletions
44
modules/openapi-generator/src/main/resources/typescript-fetch/api_example.mustache
This file contains hidden or 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,44 @@ | ||
import { | ||
Configuration, | ||
{{classname}}, | ||
} from '{{npmName}}'; | ||
import type { {{operationIdCamelCase}}Request } from '{{npmName}}'; | ||
|
||
async function example() { | ||
console.log("🚀 Testing {{npmName}} SDK..."); | ||
{{#hasAuthMethods}} | ||
const config = new Configuration({ {{#authMethods}}{{#isBasicBasic}} | ||
// To configure HTTP basic authorization: {{{name}}} | ||
username: "YOUR USERNAME", | ||
password: "YOUR PASSWORD",{{/isBasicBasic}}{{#isBasicBearer}} | ||
// Configure HTTP bearer authorization: {{{name}}} | ||
accessToken: "YOUR BEARER TOKEN",{{/isBasicBearer}}{{#isOAuth}} | ||
// To configure OAuth2 access token for authorization: {{{name}}} {{{flow}}} | ||
accessToken: "YOUR ACCESS TOKEN",{{/isOAuth}}{{#isApiKey}} | ||
// To configure API key authorization: {{{name}}} | ||
apiKey: "YOUR API KEY",{{/isApiKey}}{{#isHttpSignature}} | ||
// To configure HTTP signature authorization: {{{name}}} | ||
headers: { "YOUR HEADER NAME": "YOUR SIGNATURE" },{{/isHttpSignature}}{{/authMethods}} | ||
}); | ||
{{/hasAuthMethods}} | ||
const api = new {{classname}}({{#hasAuthMethods}}config{{/hasAuthMethods}}); | ||
|
||
{{#hasParams}} | ||
const body = { | ||
{{#allParams}} | ||
// {{{dataType}}}{{#description}} | {{{description}}}{{/description}}{{^required}} (optional){{/required}} | ||
{{paramName}}: {{{example}}}{{^example}}...{{/example}}, | ||
{{/allParams}} | ||
} satisfies {{operationIdCamelCase}}Request; | ||
|
||
{{/hasParams}} | ||
try { | ||
const data = await api.{{{operationId}}}({{#hasParams}}body{{/hasParams}}); | ||
console.log(data); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
// Run the test | ||
example().catch(console.error); |
41 changes: 41 additions & 0 deletions
41
modules/openapi-generator/src/main/resources/typescript-fetch/model_doc.mustache
This file contains hidden or 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,41 @@ | ||
{{#models}}{{#model}} | ||
# {{classname}} | ||
|
||
{{#description}}{{&description}} | ||
{{/description}} | ||
|
||
## Properties | ||
|
||
Name | Type | ||
------------ | ------------- | ||
{{#vars}}`{{name}}` | {{#isPrimitiveType}}{{dataType}}{{/isPrimitiveType}}{{^isPrimitiveType}}[{{dataType}}]({{complexType}}.md){{/isPrimitiveType}} | ||
{{/vars}} | ||
|
||
{{^withoutRuntimeChecks}} | ||
## Example | ||
|
||
```typescript | ||
import type { {{classname}} } from '{{npmName}}' | ||
|
||
// TODO: Update the object below with actual values | ||
const example = { | ||
{{#vars}} | ||
"{{name}}": {{{example}}}, | ||
{{/vars}} | ||
} satisfies {{classname}} | ||
|
||
console.log(example) | ||
|
||
// Convert the instance to a JSON string | ||
const exampleJSON: string = JSON.stringify(example) | ||
console.log(exampleJSON) | ||
|
||
// Parse the JSON string back to an object | ||
const exampleParsed = JSON.parse(exampleJSON) as {{classname}} | ||
console.log(exampleParsed) | ||
``` | ||
{{/withoutRuntimeChecks}} | ||
|
||
[[Back to top]](#) [[Back to API list]](../README.md#api-endpoints) [[Back to Model list]](../README.md#models) [[Back to README]](../README.md) | ||
|
||
{{/model}}{{/models}} |
4 changes: 4 additions & 0 deletions
4
samples/client/others/typescript-fetch/self-import-issue/.openapi-generator/FILES
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...because we seem to be using those constantly here for a local path. If we want to use the constants both in a file path context and a web context we should remove the trailing slash and add it when we use them in a web context.