Skip to content
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

Add worktop, AWS, and callback adapters & fix webhook reply #53

Merged
merged 13 commits into from
Oct 18, 2021
3 changes: 2 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@
"profile": "https://github.com/amberlionk",
"contributions": [
"review",
"bug"
"bug",
"code"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center"><a href="https://github.com/dzek69"><img src="https://avatars.githubusercontent.com/u/4936805?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jacek Nowacki</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/commits?author=dzek69" title="Documentation">📖</a> <a href="https://github.com/grammyjs/grammY/commits?author=dzek69" title="Code">💻</a> <a href="https://github.com/grammyjs/grammY/issues?q=author%3Adzek69" title="Bug reports">🐛</a></td>
<td align="center"><a href="https://blog.outv.im"><img src="https://avatars.githubusercontent.com/u/19144373?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Outvi V</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/commits?author=outloudvi" title="Code">💻</a></td>
<td align="center"><a href="https://bandism.net/"><img src="https://avatars.githubusercontent.com/u/22633385?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ikko Ashimine</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/commits?author=eltociear" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/amberlionk"><img src="https://avatars.githubusercontent.com/u/29119723?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Yevhen Denesiuk</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3Aamberlionk" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/grammyjs/grammY/issues?q=author%3Aamberlionk" title="Bug reports">🐛</a></td>
<td align="center"><a href="https://github.com/amberlionk"><img src="https://avatars.githubusercontent.com/u/29119723?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Yevhen Denesiuk</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3Aamberlionk" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/grammyjs/grammY/issues?q=author%3Aamberlionk" title="Bug reports">🐛</a> <a href="https://github.com/grammyjs/grammY/commits?author=amberlionk" title="Code">💻</a></td>
</tr>
</table>

Expand Down
17 changes: 16 additions & 1 deletion src/convenience/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ type SupportedFrameworks =
| "koa"
| "oak"
| "fastify"
| "worktop";
| "worktop"
| "callback"
| "aws-lambda";

/**
* Abstraction over a request-response cycle, provding access to the update, as
Expand Down Expand Up @@ -79,6 +81,19 @@ const frameworkAdapters: Record<SupportedFrameworks, FrameworkAdapter> = {
end: () => res.end(),
respond: (json) => res.send(200, json),
}),
callback: (update, callback) => ({
update: update,
respond: callback,
}),
"aws-lambda": (event, _context, callback) => ({
update: JSON.parse(event.body),
end: () => callback(null, { statusCode: 200 }),
respond: (json) =>
callback(null, {
statusCode: 200,
body: json,
}),
}),
// please open a PR if you want to add another
};

Expand Down
15 changes: 12 additions & 3 deletions src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
Telegram,
} from "../platform.deno.ts";
import { GrammyError, HttpError } from "./error.ts";
import { createRequestConfig } from "./payload.ts";
import {
createFormDataPayload,
createJsonPayload,
requiresFormDataUpload,
} from "./payload.ts";
const debug = d("grammy:core");

export type Methods<R extends RawApi> = string & keyof R;
Expand Down Expand Up @@ -224,17 +228,22 @@ class ApiClient<R extends RawApi> {
this.token,
method,
);
const config = createRequestConfig(payload ?? {});
const formDataRequired = requiresFormDataUpload(payload);
if (
this.webhookReplyEnvelope.send !== undefined &&
!this.hasUsedWebhookReply &&
typeof config.body === "string" &&
!formDataRequired &&
this.options.canUseWebhookReply(method)
) {
this.hasUsedWebhookReply = true;
const config = createJsonPayload({ ...payload, method });
await this.webhookReplyEnvelope.send(config.body);
return { ok: true, result: true };
} else {
const p = payload ?? {};
const config = formDataRequired
? createFormDataPayload(p)
: createJsonPayload(p);
let res: Await<ReturnType<typeof fetch>>;
try {
res = await fetch(url, {
Expand Down
18 changes: 3 additions & 15 deletions src/core/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,14 @@ import {
} from "../platform.deno.ts";

// === Payload types (JSON vs. form data)
/**
* Turns a payload into an options object that can be passed to a `fetch` call.
* Automatically switches between JSON payloads and form-data payloads as
* needed.
*
* @param payload The payload to use
*/
export function createRequestConfig(payload: Record<string, unknown>) {
return requiresFormDataUpload(payload)
? createFormDataPayload(payload)
: createJsonPayload(payload);
}
/**
* Determines for a given payload if it may be sent as JSON, or if it has to be
* uploaded via multipart/form-data. Returns `true` in the latter case and
* `false` in the former.
*
* @param payload The payload to analyse
*/
function requiresFormDataUpload(payload: unknown): boolean {
export function requiresFormDataUpload(payload: unknown): boolean {
return (
typeof payload === "object" &&
payload !== null &&
Expand Down Expand Up @@ -53,7 +41,7 @@ function str(value: unknown) {
*
* @param payload The payload to wrap
*/
function createJsonPayload(payload: Record<string, unknown>) {
export function createJsonPayload(payload: Record<string, unknown>) {
return {
method: "POST",
headers: {
Expand All @@ -71,7 +59,7 @@ function createJsonPayload(payload: Record<string, unknown>) {
*
* @param payload The payload to wrap
*/
function createFormDataPayload(payload: Record<string, unknown>) {
export function createFormDataPayload(payload: Record<string, unknown>) {
const boundary = createBoundary();

return {
Expand Down