Skip to content

Commit

Permalink
[fix] several detail bugs
Browse files Browse the repository at this point in the history
[optimize] update Upstream packages
  • Loading branch information
TechQuery committed Apr 15, 2022
1 parent be76e89 commit 915d2c6
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 391 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ on:
tags:
- v*
jobs:
Build-and-Deploy:
Test-Build-Publish-Deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
registry-url: https://registry.npmjs.org
cache: yarn
- name: Install, Build & Publish
run: yarn && yarn publish
- name: Install packages
run: yarn
- name: Test, Build & Publish
run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
APP_ID: ${{ secrets.APP_ID }}
Expand All @@ -25,7 +25,7 @@ jobs:
BITABLE_ID: ${{ secrets.BITABLE_ID }}
BITABLE_TABLE_ID: ${{ secrets.BITABLE_TABLE_ID }}
MEDIA_ID: ${{ secrets.MEDIA_ID }}
- name: Deploy
- name: Deploy document
uses: peaceiris/actions-gh-pages@v3
with:
publish_dir: ./docs
Expand Down
4 changes: 4 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Unofficial Node.js [TypeScript][1] SDK for [FeiShu/Lark API][2]

1. [China Open Source map](https://kaiyuanshe.vercel.app/organization)

## Related with

- Lark client API: https://github.com/idea2app/Lark-client-API

[1]: https://www.typescriptlang.org/
[2]: https://open.feishu.cn/
[3]: https://github.com/idea2app/Lark-TS-SDK/actions/workflows/main.yml
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lark-ts-sdk",
"version": "0.7.2",
"version": "0.7.7",
"license": "LGPL-3.0",
"author": "[email protected]",
"description": "Unofficial Node.js TypeScript SDK for FeiShu/Lark API",
Expand All @@ -25,22 +25,23 @@
},
"dependencies": {
"@swc/helpers": "^0.3.8",
"jsdom": "^19.0.0",
"koajax": "^0.8.0-rc.2",
"koajax": "^0.8.0-rc.3",
"node-fetch": "^2.6.7",
"regenerator-runtime": "^0.13.9",
"ts-toolbelt": "^9.6.0",
"tslib": "^2.3.1",
"web-utility": "^3.6.1"
"web-utility": "^3.6.2"
},
"devDependencies": {
"@types/node": "^14.18.12",
"@types/node": "^14.18.13",
"@types/node-fetch": "^2.6.1",
"dotenv": "^16.0.0",
"husky": "^7.0.4",
"lint-staged": "^12.3.7",
"lint-staged": "^12.3.8",
"prettier": "^2.6.2",
"ts-node": "^10.7.0",
"typedoc": "^0.22.14",
"typedoc-plugin-mdn-links": "^1.0.5",
"typedoc": "^0.22.15",
"typedoc-plugin-mdn-links": "^1.0.6",
"typescript": "^4.6.3"
},
"prettier": {
Expand Down
31 changes: 20 additions & 11 deletions src/Lark.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { buildURLData, cache } from 'web-utility';
import { HTTPClient, polyfill } from 'koajax';
import { HTTPClient } from 'koajax';

import { TenantAccessToken, JSTicket, UserMeta } from './type';
import { isLarkError, TenantAccessToken, JSTicket, UserMeta } from './type';
import { InstantMessenger } from './module/InstantMessenger';
import { SpreadSheet } from './module/SpreadSheet';
import { BITable } from './module/BITable';
Expand Down Expand Up @@ -34,20 +34,22 @@ export class Lark implements LarkOptions {
...request.headers,
Authorization: `Bearer ${accessToken}`
};

try {
await next();
} catch (error) {

const { body } = response;

console.error(body);
if (isLarkError(body)) {
console.error(body);
throw new URIError(body.msg);
}
} catch (error) {
console.error(response.body);
throw error;
}
});

async getAccessToken() {
await polyfill(new URL(this.client.baseURI).origin);

return (this.accessToken = await this.getTenantAccessToken());
}

Expand All @@ -71,7 +73,7 @@ export class Lark implements LarkOptions {
* @see https://open.feishu.cn/document/ukTMukTMukTM/ukzN4UjL5cDO14SO3gTN
*/
getWebSignInURL(redirect_uri: string, state?: string) {
return `${this.client.baseURI}/authen/v1/index?${buildURLData({
return `${this.client.baseURI}authen/v1/index?${buildURLData({
app_id: this.appId,
redirect_uri,
state
Expand All @@ -82,6 +84,8 @@ export class Lark implements LarkOptions {
* @see https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/authen/access_token
*/
async getUserMeta(code: string) {
await this.getAccessToken();

const { body } = await this.client.post<UserMeta>(
'authen/v1/access_token',
{ grant_type: 'authorization_code', code }
Expand All @@ -92,11 +96,16 @@ export class Lark implements LarkOptions {
/**
* @see https://open.feishu.cn/document/ukTMukTMukTM/uYTM5UjL2ETO14iNxkTN/h5_js_sdk/authorization
*/
async getJSTicket() {
getJSTicket = cache(async clean => {
await this.getAccessToken();

const { body } = await this.client.post<JSTicket>('jssdk/ticket/get');
const { expire_in, ticket } = body!.data;

return body!.data;
}
setTimeout(clean, expire_in * 1000);

return ticket;
}, 'JS ticket');

async getSpreadSheet(id: string) {
await this.getAccessToken();
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import fetch from 'node-fetch';
// @ts-ignore
globalThis.fetch ||= fetch;

export * from './type';
export * from './Lark';
export * from './module';
6 changes: 5 additions & 1 deletion src/module/InstantMessenger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class InstantMessenger extends LarkModule {
* @see https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/chat/create
*/
async createGroup(meta?: Partial<CreateChatMeta>) {
await this.core.getAccessToken();

const { body } = await this.core.client.post<LarkData<ChatMeta>>(
`${this.baseURI}/chats?${new URLSearchParams({
set_bot_manager: true + ''
Expand All @@ -36,10 +38,12 @@ export class InstantMessenger extends LarkModule {

if (old) return old;

await this.core.getAccessToken();

const { body } = await this.core.client.get<LarkData<ChatMeta>>(
`${this.baseURI}/chats/${id}`
);
return this.cacheChat(body!.data);
return this.cacheChat({ ...body!.data, chat_id: id });
}
}

Expand Down
Loading

0 comments on commit 915d2c6

Please sign in to comment.