Skip to content

Commit

Permalink
Simplify the http-fetch.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Mar 1, 2024
1 parent 63429f1 commit 47f50c1
Show file tree
Hide file tree
Showing 15 changed files with 561 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as Types from "../../types";
import {ensureJSON} from "../../utils";
import {Readable} from "stream";

import HTTPFetchClient from "../../http-fetch";
import HTTPFetchClient, { convertResponseToReadable } from "../../http-fetch";

// ===============================================
// This file is autogenerated - Please do not edit
Expand Down Expand Up @@ -39,7 +39,6 @@ export class {{operations.classname}} {
Authorization: "Bearer " + config.channelAccessToken,
{% endif -%}
},
responseParser: this.parseHTTPResponse.bind(this),
baseURL: config.baseURL,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
form.append("{{param.paramName}}", String({{param.paramName}}));
{% endif -%}
{% endfor %}
const res = this.httpClient.{{op.httpMethod|lower}}{% if op.hasFormParams %}Form{% endif %}Multipart{% if op.returnType %}<{{ op.returnType }}>{% endif %}(
const res = await this.httpClient.{{op.httpMethod|lower}}{% if op.hasFormParams %}Form{% endif %}Multipart(
"{{op.path}}"
{% for param in op.pathParams -%}
.replace("{{ "{" + param.paramName + "}" }}", String({{ param.paramName }}))
{% endfor %},
form,
);
return ensureJSON(res);
const result = (await this.parseHTTPResponse(res)) as {% if op.returnType %}{{ op.returnType }}{% else %}Types.MessageAPIResponseBase{% endif %};
return ensureJSON(result);
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{# @pebvariable name="op" type="org.openapitools.codegen.CodegenOperation" #}

{% if op.isResponseFile %}
return this.httpClient.{{op.httpMethod|lower}}Stream("{{op.path}}"
const response = await this.httpClient.{{op.httpMethod|lower}}("{{op.path}}"
{% for param in op.pathParams %}
.replace('{' + "{{param.baseName}}" + '}', String({{param.paramName}}))
{% endfor %}
);
return convertResponseToReadable(response);
{% else %}
{% if op.hasBodyParam %}const params = {{op.bodyParam.paramName}};
{% elseif op.hasFormParams %}const formParams = {
Expand All @@ -31,7 +32,7 @@
};
{% endif %}

const res = this.httpClient.{{op.httpMethod|lower}}{% if op.hasFormParams %}Form{% endif %}{% if op.isMultipart %}Multipart{% endif %}{% if op.hasBodyParam and op.bodyParam.isFile %}BinaryContent{% endif %}{% if op.returnType %}<{{ op.returnType }}>{% endif %}(
const res = await this.httpClient.{{op.httpMethod|lower}}{% if op.hasFormParams %}Form{% endif %}{% if op.isMultipart %}Multipart{% endif %}{% if op.hasBodyParam and op.bodyParam.isFile %}BinaryContent{% endif %}(
"{{ op.path }}"
{% for param in op.pathParams %}
.replace("{{ "{" + param.paramName + "}" }}", String({{ param.paramName }}))
Expand All @@ -42,5 +43,6 @@
{% elseif op.hasQueryParams %}queryParams,{% endif %}
{% if op.hasHeaderParams %}{headers: headerParams},{% endif %}
);
return ensureJSON(res);
const result = (await this.parseHTTPResponse(res)) as {% if op.returnType %}{{ op.returnType }}{% else %}Types.MessageAPIResponseBase{% endif %};
return ensureJSON(result);
{% endif %}
79 changes: 47 additions & 32 deletions lib/channel-access-token/api/channelAccessTokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as Types from "../../types";
import { ensureJSON } from "../../utils";
import { Readable } from "stream";

import HTTPFetchClient from "../../http-fetch";
import HTTPFetchClient, { convertResponseToReadable } from "../../http-fetch";

// ===============================================
// This file is autogenerated - Please do not edit
Expand All @@ -42,7 +42,6 @@ export class ChannelAccessTokenClient {
}
this.httpClient = new HTTPFetchClient({
defaultHeaders: {},
responseParser: this.parseHTTPResponse.bind(this),
baseURL: config.baseURL,
});
}
Expand Down Expand Up @@ -76,11 +75,14 @@ export class ChannelAccessTokenClient {
clientAssertion: clientAssertion,
};

const res = this.httpClient.get<ChannelAccessTokenKeyIdsResponse>(
const res = await this.httpClient.get(
"/oauth2/v2.1/tokens/kid",
queryParams,
);
return ensureJSON(res);
const result = (await this.parseHTTPResponse(
res,
)) as ChannelAccessTokenKeyIdsResponse;
return ensureJSON(result);
}
/**
* Issue short-lived channel access token
Expand All @@ -106,12 +108,14 @@ export class ChannelAccessTokenClient {
}
});

const res =
this.httpClient.postForm<IssueShortLivedChannelAccessTokenResponse>(
"/v2/oauth/accessToken",
formParams,
);
return ensureJSON(res);
const res = await this.httpClient.postForm(
"/v2/oauth/accessToken",
formParams,
);
const result = (await this.parseHTTPResponse(
res,
)) as IssueShortLivedChannelAccessTokenResponse;
return ensureJSON(result);
}
/**
* Issues a channel access token that allows you to specify a desired expiration date. This method lets you use JWT assertion for authentication.
Expand All @@ -137,11 +141,14 @@ export class ChannelAccessTokenClient {
}
});

const res = this.httpClient.postForm<IssueChannelAccessTokenResponse>(
const res = await this.httpClient.postForm(
"/oauth2/v2.1/token",
formParams,
);
return ensureJSON(res);
const result = (await this.parseHTTPResponse(
res,
)) as IssueChannelAccessTokenResponse;
return ensureJSON(result);
}
/**
* Issues a new stateless channel access token, which doesn\'t have max active token limit unlike the other token types. The newly issued token is only valid for 15 minutes but can not be revoked until it naturally expires.
Expand Down Expand Up @@ -173,12 +180,11 @@ export class ChannelAccessTokenClient {
}
});

const res =
this.httpClient.postForm<IssueStatelessChannelAccessTokenResponse>(
"/oauth2/v3/token",
formParams,
);
return ensureJSON(res);
const res = await this.httpClient.postForm("/oauth2/v3/token", formParams);
const result = (await this.parseHTTPResponse(
res,
)) as IssueStatelessChannelAccessTokenResponse;
return ensureJSON(result);
}
/**
* Revoke short-lived or long-lived channel access token
Expand All @@ -198,8 +204,11 @@ export class ChannelAccessTokenClient {
}
});

const res = this.httpClient.postForm("/v2/oauth/revoke", formParams);
return ensureJSON(res);
const res = await this.httpClient.postForm("/v2/oauth/revoke", formParams);
const result = (await this.parseHTTPResponse(
res,
)) as Types.MessageAPIResponseBase;
return ensureJSON(result);
}
/**
* Revoke channel access token v2.1
Expand All @@ -225,8 +234,14 @@ export class ChannelAccessTokenClient {
}
});

const res = this.httpClient.postForm("/oauth2/v2.1/revoke", formParams);
return ensureJSON(res);
const res = await this.httpClient.postForm(
"/oauth2/v2.1/revoke",
formParams,
);
const result = (await this.parseHTTPResponse(
res,
)) as Types.MessageAPIResponseBase;
return ensureJSON(result);
}
/**
* Verify the validity of short-lived and long-lived channel access tokens
Expand All @@ -246,11 +261,11 @@ export class ChannelAccessTokenClient {
}
});

const res = this.httpClient.postForm<VerifyChannelAccessTokenResponse>(
"/v2/oauth/verify",
formParams,
);
return ensureJSON(res);
const res = await this.httpClient.postForm("/v2/oauth/verify", formParams);
const result = (await this.parseHTTPResponse(
res,
)) as VerifyChannelAccessTokenResponse;
return ensureJSON(result);
}
/**
* You can verify whether a Channel access token with a user-specified expiration (Channel Access Token v2.1) is valid.
Expand All @@ -265,10 +280,10 @@ export class ChannelAccessTokenClient {
accessToken: accessToken,
};

const res = this.httpClient.get<VerifyChannelAccessTokenResponse>(
"/oauth2/v2.1/verify",
queryParams,
);
return ensureJSON(res);
const res = await this.httpClient.get("/oauth2/v2.1/verify", queryParams);
const result = (await this.parseHTTPResponse(
res,
)) as VerifyChannelAccessTokenResponse;
return ensureJSON(result);
}
}

0 comments on commit 47f50c1

Please sign in to comment.