@ruiapp/ding-talk-plugin
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -17,2 +17,3 @@ /** | ||
configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>; | ||
registerCronJobs(server: IRpdServer): Promise<void>; | ||
onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<void>; | ||
@@ -19,0 +20,0 @@ get dingTalkService(): DingTalkService; |
@@ -148,2 +148,16 @@ 'use strict'; | ||
var refreshAccessToken = { | ||
code: "dingTalk.refreshAccessToken", | ||
cronTime: "0 0 */1 * * *", | ||
async handler(ctx) { | ||
const { server, logger } = ctx; | ||
logger.info("Executing dingTalk.refreshAccessToken job..."); | ||
const dingTalkService = server.getService("dingTalkService"); | ||
await dingTalkService.refreshAccessTokens(); | ||
logger.info("Finished dingTalk.refreshAccessToken job..."); | ||
}, | ||
}; | ||
var pluginCronJobs = [refreshAccessToken]; | ||
class AuthApi { | ||
@@ -272,4 +286,3 @@ #manager; | ||
async getAccessToken() { | ||
const cacheProvider = await this.#server.getFacility("cache"); | ||
const cache = await cacheProvider.createCache(this.#server); | ||
const cache = await this.#server.getFacility("cache"); | ||
const cacheKey = `dingTalk.suite-${this.#config.appKey}.agent-${this.#config.agentId}.accessToken`; | ||
@@ -301,4 +314,3 @@ let accessToken = await cache.get(cacheKey); | ||
async getAccessTokenOfInternalApp() { | ||
const cacheProvider = await this.#server.getFacility("cache"); | ||
const cache = await cacheProvider.createCache(this.#server); | ||
const cache = await this.#server.getFacility("cache"); | ||
const cacheKey = `dingTalk.app-${this.#config.appKey}.agent-${this.#config.agentId}.accessToken`; | ||
@@ -345,2 +357,6 @@ let accessToken = await cache.get(cacheKey); | ||
} | ||
async refreshAccessTokens() { | ||
const accessToken = await this.#serverApi.getAccessToken(); | ||
this.#server.getLogger().info(`DingTalk access token refreshed.`, { accessToken }); | ||
} | ||
async getDingTalkUserInfoByAuthCode(routeContext, authCode) { | ||
@@ -505,11 +521,11 @@ if (!this.#serverApi) { | ||
} | ||
async registerCronJobs(server) { | ||
for (const cronJob of pluginCronJobs) { | ||
server.registerCronJob(cronJob); | ||
} | ||
} | ||
async onApplicationLoaded(server, applicationConfig) { | ||
const settingService = server.getService("settingService"); | ||
const settingValues = await settingService.getSystemSettingValues("dingTalk"); | ||
const apiConfig = lodash.pick(settingValues, [ | ||
"corpId", | ||
"agentId", | ||
"appKey", | ||
"appSecret", | ||
]); | ||
const apiConfig = lodash.pick(settingValues, ["corpId", "agentId", "appKey", "appSecret"]); | ||
this.#dingTalkService.initService(apiConfig); | ||
@@ -516,0 +532,0 @@ } |
@@ -8,2 +8,3 @@ import type { IRpdServer, RouteContext } from "@ruiapp/rapid-core"; | ||
initService(apiConfig: DingTalkServerApiConfig): void; | ||
refreshAccessTokens(): Promise<void>; | ||
getDingTalkUserInfoByAuthCode(routeContext: RouteContext, authCode: string): Promise<import("../server-sdk/dingTalkSdkTypes").GetUserByUnionIdResult>; | ||
@@ -10,0 +11,0 @@ sendWorkMessage(routeContext: RouteContext, userIds: number[], message: DingTalkMessage): Promise<{}>; |
{ | ||
"name": "@ruiapp/ding-talk-plugin", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -13,2 +13,3 @@ "main": "dist/index.js", | ||
"@types/node": "^20.11.16", | ||
"prettier": "^3.4.2", | ||
"rimraf": "^3.0.2", | ||
@@ -24,3 +25,3 @@ "rollup": "^2.79.1", | ||
"peerDependencies": { | ||
"@ruiapp/rapid-core": "^0.6.5", | ||
"@ruiapp/rapid-core": "^0.7.0", | ||
"axios": "^1.7.9", | ||
@@ -33,4 +34,5 @@ "cron": "^3.1.7", | ||
"dev": "rollup --config", | ||
"fmt": "prettier . --write", | ||
"test": "echo \"Error: no test specified\"" | ||
} | ||
} |
@@ -0,0 +0,0 @@ import typescript from "rollup-plugin-typescript2"; |
@@ -8,3 +8,7 @@ import type { ActionHandlerContext } from "@ruiapp/rapid-core"; | ||
export async function handler(plugin: DingTalkPlugin, ctx: ActionHandlerContext, config: BindDingTalkAccountForUsersWithMobileActionHandlerConfig) { | ||
export async function handler( | ||
plugin: DingTalkPlugin, | ||
ctx: ActionHandlerContext, | ||
config: BindDingTalkAccountForUsersWithMobileActionHandlerConfig, | ||
) { | ||
const { routerContext: routeContext } = ctx; | ||
@@ -11,0 +15,0 @@ |
@@ -0,0 +0,0 @@ import type { IPluginActionHandler } from "@ruiapp/rapid-core"; |
@@ -14,3 +14,7 @@ import type { ActionHandlerContext } from "@ruiapp/rapid-core"; | ||
export async function handler(plugin: DingTalkPlugin, ctx: ActionHandlerContext, config: SendDingTalkWorkMessageActionHandlerConfig) { | ||
export async function handler( | ||
plugin: DingTalkPlugin, | ||
ctx: ActionHandlerContext, | ||
config: SendDingTalkWorkMessageActionHandlerConfig, | ||
) { | ||
const { routerContext: routeContext } = ctx; | ||
@@ -17,0 +21,0 @@ const input: SendDingTalkWorkMessageInput = ctx.input; |
@@ -18,3 +18,3 @@ import type { ActionHandlerContext } from "@ruiapp/rapid-core"; | ||
ctx: ActionHandlerContext, | ||
config: SigninWithAuthCodeActionHandlerConfig | ||
config: SigninWithAuthCodeActionHandlerConfig, | ||
) { | ||
@@ -24,7 +24,6 @@ const { routerContext: routeContext, server } = ctx; | ||
const getDingTalkUserInfoResult = | ||
await plugin.dingTalkService.getDingTalkUserInfoByAuthCode( | ||
routeContext, | ||
input.authCode | ||
); | ||
const getDingTalkUserInfoResult = await plugin.dingTalkService.getDingTalkUserInfoByAuthCode( | ||
routeContext, | ||
input.authCode, | ||
); | ||
const dingTalkUserId = getDingTalkUserInfoResult?.userid; | ||
@@ -31,0 +30,0 @@ if (!dingTalkUserId) { |
@@ -16,2 +16,3 @@ /** | ||
import pluginRoutes from "./routes"; | ||
import pluginCronJobs from "./cronJobs"; | ||
import DingTalkService from "./services/DingTalkService"; | ||
@@ -50,13 +51,7 @@ import type SettingService from "@ruiapp/rapid-core/dist/plugins/setting/SettingService"; | ||
async configureModels( | ||
server: IRpdServer, | ||
applicationConfig: RpdApplicationConfig | ||
): Promise<any> { | ||
async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> { | ||
server.appendApplicationConfig({ models: pluginModels }); | ||
} | ||
async configureServices( | ||
server: IRpdServer, | ||
applicationConfig: RpdApplicationConfig | ||
): Promise<any> { | ||
async configureServices(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> { | ||
this.#dingTalkService = new DingTalkService(server); | ||
@@ -66,23 +61,16 @@ server.registerService("dingTalkService", this.#dingTalkService); | ||
async configureRoutes( | ||
server: IRpdServer, | ||
applicationConfig: RpdApplicationConfig | ||
): Promise<any> { | ||
async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> { | ||
server.appendApplicationConfig({ routes: pluginRoutes }); | ||
} | ||
async onApplicationLoaded( | ||
server: IRpdServer, | ||
applicationConfig: RpdApplicationConfig | ||
) { | ||
async registerCronJobs(server: IRpdServer) { | ||
for (const cronJob of pluginCronJobs) { | ||
server.registerCronJob(cronJob); | ||
} | ||
} | ||
async onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig) { | ||
const settingService = server.getService<SettingService>("settingService"); | ||
const settingValues = await settingService.getSystemSettingValues( | ||
"dingTalk" | ||
); | ||
const apiConfig = pick(settingValues, [ | ||
"corpId", | ||
"agentId", | ||
"appKey", | ||
"appSecret", | ||
]); | ||
const settingValues = await settingService.getSystemSettingValues("dingTalk"); | ||
const apiConfig = pick(settingValues, ["corpId", "agentId", "appKey", "appSecret"]); | ||
this.#dingTalkService.initService(apiConfig); | ||
@@ -89,0 +77,0 @@ } |
@@ -0,0 +0,0 @@ export { default as DingTalkPlugin } from "./DingTalkPlugin"; |
@@ -1,3 +0,1 @@ | ||
export default [ | ||
] | ||
export default []; |
@@ -0,0 +0,0 @@ import type { RpdRoute } from "@ruiapp/rapid-core"; |
@@ -0,0 +0,0 @@ import bindDingTalkAccountForUsersWithMobile from "./bindDingTalkAccountForUsersWithMobile"; |
@@ -0,0 +0,0 @@ import type { RpdRoute } from "@ruiapp/rapid-core"; |
@@ -0,0 +0,0 @@ import type { RpdRoute } from "@ruiapp/rapid-core"; |
@@ -24,7 +24,10 @@ import DingTalkServerApi from "../dingTalkServerApi"; | ||
async getAccessToken(options: GetAccessTokenOptions) { | ||
const response = await this.#manager.newApiRequest.post<GetAccessTokenResult>(`/v1.0/oauth2/${options.corpId}/token`, { | ||
client_id: options.appKey, | ||
client_secret: options.appSecret, | ||
grant_type: "client_credentials", | ||
}); | ||
const response = await this.#manager.newApiRequest.post<GetAccessTokenResult>( | ||
`/v1.0/oauth2/${options.corpId}/token`, | ||
{ | ||
client_id: options.appKey, | ||
client_secret: options.appSecret, | ||
grant_type: "client_credentials", | ||
}, | ||
); | ||
return response.data; | ||
@@ -39,3 +42,6 @@ } | ||
async getAccessTokenOfInternalApp(options: GetAccessTokenOfInternalAppOptions) { | ||
const response = await this.#manager.newApiRequest.post<GetAccessTokenOfInternalAppResult>("/v1.0/oauth2/accessToken", options); | ||
const response = await this.#manager.newApiRequest.post<GetAccessTokenOfInternalAppResult>( | ||
"/v1.0/oauth2/accessToken", | ||
options, | ||
); | ||
return response.data; | ||
@@ -42,0 +48,0 @@ } |
@@ -0,0 +0,0 @@ import type { DingTalkMessage } from "../dingTalkSdkTypes"; |
import type DingTalkServerApi from "../dingTalkServerApi"; | ||
import type { ApiResponseBody } from "../types/common-types"; | ||
import type { GetUserByMobileResult, GetUserByUnionIdResult, GetUserDetailsOptions, GetUserDetailsResult } from "../types/user-types"; | ||
import type { | ||
GetUserByMobileResult, | ||
GetUserByUnionIdResult, | ||
GetUserDetailsOptions, | ||
GetUserDetailsResult, | ||
} from "../types/user-types"; | ||
@@ -23,5 +28,8 @@ export default class UserApi { | ||
const accessToken = await this.#manager.getAccessToken(); | ||
const response = await this.#manager.oldApiRequest.post<ApiResponseBody<GetUserByMobileResult>>(`/topapi/v2/user/getbymobile?access_token=${accessToken}`, { | ||
mobile, | ||
}); | ||
const response = await this.#manager.oldApiRequest.post<ApiResponseBody<GetUserByMobileResult>>( | ||
`/topapi/v2/user/getbymobile?access_token=${accessToken}`, | ||
{ | ||
mobile, | ||
}, | ||
); | ||
return response.data; | ||
@@ -51,7 +59,10 @@ } | ||
const accessToken = await this.#manager.getAccessToken(); | ||
const response = await this.#manager.oldApiRequest.post<ApiResponseBody<GetUserByUnionIdResult>>(`/topapi/user/getbyunionid?access_token=${accessToken}`, { | ||
unionid, | ||
}); | ||
const response = await this.#manager.oldApiRequest.post<ApiResponseBody<GetUserByUnionIdResult>>( | ||
`/topapi/user/getbyunionid?access_token=${accessToken}`, | ||
{ | ||
unionid, | ||
}, | ||
); | ||
return response.data; | ||
} | ||
} |
export * from "./types/im-types"; | ||
export * from "./types/approval-types"; | ||
export * from "./types/user-types"; |
@@ -5,3 +5,3 @@ import axios, { AxiosInstance } from "axios"; | ||
import UserApi from "./apis/user"; | ||
import { CacheProvider, IRpdServer } from "@ruiapp/rapid-core"; | ||
import { CacheProvider, Cache, IRpdServer } from "@ruiapp/rapid-core"; | ||
@@ -70,9 +70,4 @@ export type DingTalkServerApiConfig = { | ||
async getAccessToken() { | ||
const cacheProvider = await this.#server.getFacility<CacheProvider>( | ||
"cache" | ||
); | ||
const cache = await cacheProvider.createCache(this.#server); | ||
const cacheKey = `dingTalk.suite-${this.#config.appKey}.agent-${ | ||
this.#config.agentId | ||
}.accessToken`; | ||
const cache = await this.#server.getFacility<Cache>("cache"); | ||
const cacheKey = `dingTalk.suite-${this.#config.appKey}.agent-${this.#config.agentId}.accessToken`; | ||
let accessToken: string | null = await cache.get(cacheKey); | ||
@@ -111,9 +106,4 @@ | ||
async getAccessTokenOfInternalApp() { | ||
const cacheProvider = await this.#server.getFacility<CacheProvider>( | ||
"cache" | ||
); | ||
const cache = await cacheProvider.createCache(this.#server); | ||
const cacheKey = `dingTalk.app-${this.#config.appKey}.agent-${ | ||
this.#config.agentId | ||
}.accessToken`; | ||
const cache = await this.#server.getFacility<Cache>("cache"); | ||
const cacheKey = `dingTalk.app-${this.#config.appKey}.agent-${this.#config.agentId}.accessToken`; | ||
let accessToken: string | null = await cache.get(cacheKey); | ||
@@ -120,0 +110,0 @@ |
@@ -0,0 +0,0 @@ export type GetWorkflowFormSchemaResult = { |
@@ -0,0 +0,0 @@ export type GetAccessTokenOptions = { |
@@ -0,0 +0,0 @@ export type ApiResponseBody<TResult> = { |
@@ -84,3 +84,5 @@ export type DingTalkMessage = | ||
export type DingTalkActionCardMessage = DingTalkActionCardMessageWithSingleAction | DingTalkActionCardMessageWithMultipleAction; | ||
export type DingTalkActionCardMessage = | ||
| DingTalkActionCardMessageWithSingleAction | ||
| DingTalkActionCardMessageWithMultipleAction; | ||
@@ -87,0 +89,0 @@ export type DingTalkActionCardMessageWithSingleAction = { |
@@ -0,0 +0,0 @@ export type GetUserByMobileResult = { |
@@ -10,3 +10,3 @@ import type { IRpdServer, RouteContext } from "@ruiapp/rapid-core"; | ||
return new Error( | ||
"DingTalk server api was not initialized. Please check if the settings of ding talk intergration were set properly." | ||
"DingTalk server api was not initialized. Please check if the settings of ding talk intergration were set properly.", | ||
); | ||
@@ -35,6 +35,8 @@ } | ||
async getDingTalkUserInfoByAuthCode( | ||
routeContext: RouteContext, | ||
authCode: string | ||
) { | ||
async refreshAccessTokens() { | ||
const accessToken = await this.#serverApi.getAccessToken(); | ||
this.#server.getLogger().info(`DingTalk access token refreshed.`, { accessToken }); | ||
} | ||
async getDingTalkUserInfoByAuthCode(routeContext: RouteContext, authCode: string) { | ||
if (!this.#serverApi) { | ||
@@ -48,4 +50,3 @@ throw newDingTalkServerApiNotInitializedError(); | ||
const getUserInfoByAuthCodeResult = | ||
await this.#serverApi.auth.getUserInfoByAuthCode(authCode); | ||
const getUserInfoByAuthCodeResult = await this.#serverApi.auth.getUserInfoByAuthCode(authCode); | ||
if (getUserInfoByAuthCodeResult.errcode != 0) { | ||
@@ -56,3 +57,3 @@ throw new Error(getUserInfoByAuthCodeResult.errmsg); | ||
const getUserByUnionIdResult = await this.#serverApi.user.getUserByUnionId( | ||
getUserInfoByAuthCodeResult.result.unionid | ||
getUserInfoByAuthCodeResult.result.unionid, | ||
); | ||
@@ -65,7 +66,3 @@ if (getUserByUnionIdResult.errcode != 0) { | ||
async sendWorkMessage( | ||
routeContext: RouteContext, | ||
userIds: number[], | ||
message: DingTalkMessage | ||
) { | ||
async sendWorkMessage(routeContext: RouteContext, userIds: number[], message: DingTalkMessage) { | ||
if (!this.#serverApi) { | ||
@@ -156,4 +153,3 @@ throw newDingTalkServerApiNotInitializedError(); | ||
const accountManager = | ||
this.#server.getEntityManager<AuthAccount>("auth_account"); | ||
const accountManager = this.#server.getEntityManager<AuthAccount>("auth_account"); | ||
for (const user of usersWithMobile) { | ||
@@ -172,4 +168,3 @@ const dingTalkAccount = first(user.accounts); | ||
} | ||
const getDingTalkUserByMobileResult = | ||
await this.#serverApi.user.getUserByMobile(mobile); | ||
const getDingTalkUserByMobileResult = await this.#serverApi.user.getUserByMobile(mobile); | ||
const dingTalkUserId = getDingTalkUserByMobileResult.result?.userid; | ||
@@ -176,0 +171,0 @@ if (dingTalkUserId) { |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ import { EnabledDisabledState } from "./data-dictionary-types"; |
@@ -0,0 +0,0 @@ { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78674
58
2747
8