Skip to content

Add line get user profile #12

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Contributions for more APIs are welcome!
| [**Eventpop**](https://mockapis.onrender.com/swagger#tag/eventpop) | A mock API that implements a subset of the [Eventpop Public API](https://docs.eventpop.me/). |
| [**GitHub**](https://mockapis.onrender.com/swagger#tag/github) | A mock API that implements a subset of the [GitHub API](https://docs.github.com/en/rest). |
| [**OpenAI**](https://mockapis.onrender.com/swagger#tag/openai) | A mock API that implements a subset of the [OpenAI API](https://beta.openai.com/docs/api-reference/chat). |
| [**LINE**](https://mockapis.onrender.com/swagger#tag/line) | A mock API that implements a subset of the [LINE Messaging API](https://developers.line.biz/en/reference/messaging-api/). |
| [**LINE**](https://mockapis.onrender.com/swagger#tag/line) | A mock API that implements a subset of the [LINE Messaging API](https://developers.line.biz/en/reference/messaging-api/) and [LINE Login API](https://developers.line.biz/en/reference/line-login/). |
| [**Vonage**](https://mockapis.onrender.com/swagger#tag/vonage) | A mock API that implements a subset of the [Vonage SMS API](https://developer.vonage.com/en/api/sms) for sending SMS messages. |
| [**dtinth/kio**](https://mockapis.onrender.com/swagger#tag/dtinthkio) | A mock API that implements the endpoints expected by [dtinth/kio](https://github.com/dtinth/kio), a geeky self-checkin kiosk. |
| [**OpnPayments**](https://mockapis.onrender.com/swagger#tag/opnpayments) | A mock API that implements a subset of the [OpnPayments API](https://docs.opn.ooo) for receiving payments. |
Expand Down
46 changes: 46 additions & 0 deletions src/apis/line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ test("sends message", async () => {
]);
});

test("get user profile", async () => {
const tester = new LineTester();
const accessToken = await tester.addUserProfile({
userId: "mock_userId",
displayName: "mock_displayName",
pictureUrl: "mock_pictureUrl",
statusMessage: "mock_statusMessage",
});

expect(accessToken).not.toBeNull();

const actual = await tester.getUserProfile(accessToken);

expect(actual).toEqual({
userId: "mock_userId",
displayName: "mock_displayName",
pictureUrl: "mock_pictureUrl",
statusMessage: "mock_statusMessage",
});
});

class LineTester {
generateUserId() {
return `U${crypto.randomUUID().replace(/-/g, "")}`;
Expand All @@ -50,4 +71,29 @@ class LineTester {
});
return data;
}

async addUserProfile(profile: {
userId: string;
displayName: string;
pictureUrl: string;
statusMessage: string;
}) {
const { data } = await api.POST("/line/_test/v2/profile", {
body: {
...profile,
},
});
return data?.access_token;
}

async getUserProfile(accessToken?: string) {
const { data } = await api.GET("/line/v2/profile", {
params: {
header: {
authorization: `Bearer ${accessToken}`,
},
},
});
return data;
}
}
50 changes: 50 additions & 0 deletions src/apis/line.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Elysia, t } from "elysia";
import { defineApi } from "../defineApi";
import { EventStore } from "../EventStore";
import { decodeAccessToken, generateAccessToken } from "./oauth";

interface Events {
push: {
Expand Down Expand Up @@ -78,6 +79,55 @@ const elysia = new Elysia({ prefix: "/line", tags: ["LINE"] })
),
detail: { summary: "[Test] Get messages sent to a user" },
}
)
.post(
"_test/v2/profile",
({ body }) => {
return {
access_token: generateAccessToken(body),
token_type: "Bearer",
expires_in: 3600,
scope: "profile",
};
},
{
body: t.Object({
userId: t.String(),
displayName: t.String(),
pictureUrl: t.String(),
statusMessage: t.String(),
}),
response: t.Object({
access_token: t.String(),
token_type: t.String(),
expires_in: t.Number(),
scope: t.String(),
}),
detail: { summary: "[Test] Add user profile" },
}
)
.get(
"/v2/profile",
({ headers }) => {
const authHeader = headers.authorization;
if (!authHeader || !authHeader.startsWith("Bearer ")) {
return { error: "invalid_token" };
}
const token = authHeader.split(" ")[1];
return decodeAccessToken(token) as any;
},
{
headers: t.Object({
authorization: t.String(),
}),
response: t.Object({
userId: t.String(),
displayName: t.String(),
pictureUrl: t.String(),
statusMessage: t.String(),
}),
detail: { summary: "Get user profile" },
}
);

export const line = defineApi({
Expand Down
Loading