Last active
January 8, 2024 08:05
-
-
Save tedyyu/7004d21b4e754f35eee7a08a628299c4 to your computer and use it in GitHub Desktop.
How to call AliCloud's DirectMail JS SDK in your TypeScript ES project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Here is a rescue if you meet "TypeError: Dm20151123 is not a constructor" or similar errors when calling Alicloud DirectMail JS SDK in your TypeScript with ES module project | |
//https://api.alibabacloud.com/api-tools/sdk/Dm?version=2015-11-23&language=nodejs-tea&tab=primer-doc | |
//It seems SDK uses commonjs (CJS) module only | |
//So the fix is to build a wrapper using TypeScript CJS, and call it from your TypeScript ES project | |
//here is an example | |
//如果在TypeScript with ES模块项目中调用阿里云DirectMail JS SDK时遇到“TypeError: Dm20151123 is not a constructor”或类似错误,这里有一个救援方法 | |
//https://api.alibabacloud.com/api-tools/sdk/Dm?version=2015-11-23&language=nodejs-tea&tab=primer-doc | |
//看来SDK只使用了commonjs (CJS)模块 | |
//因此解决方法是使用 TypeScript CJS 构建一个包装器(Wrapper),并从 TypeScript ES 项目中调用它 | |
//这是一个例子 | |
//1. Wrapper CJS Project with '@alicloud/dm20151123' | |
//tsconfig.json | |
{ | |
"compilerOptions": { | |
"target": "es2020", | |
"module": "esnext", | |
"declaration": true, | |
"sourceMap": true, | |
"outDir": "./dist", | |
"esModuleInterop": true | |
}, | |
"include": [ | |
"src/**/*" | |
] | |
} | |
//client.ts, with example code from https://api.aliyun.com/home | |
import Dm20151123, * as $Dm20151123 from '@alicloud/dm20151123'; | |
import OpenApi, * as $OpenApi from '@alicloud/openapi-client'; | |
import Util, * as $Util from '@alicloud/tea-util'; | |
import * as $tea from '@alicloud/tea-typescript'; | |
function createClient(accessKeyId: string, accessKeySecret: string): Dm20151123 { | |
let config = new $OpenApi.Config({ | |
// 必填,您的 AccessKey ID | |
accessKeyId: accessKeyId, | |
// 必填,您的 AccessKey Secret | |
accessKeySecret: accessKeySecret, | |
}); | |
// Endpoint 请参考 https://api.aliyun.com/product/Dm | |
config.endpoint = `dm.aliyuncs.com`; | |
return new Dm20151123(config); | |
} | |
const ALIBABA_CLOUD_ACCESS_KEY_ID = '<request_api_key_from_alicloud_console>'; | |
const ALIBABA_CLOUD_ACCESS_KEY_SECRET = '<request_api_secret_from_alicloud_console>'; | |
let client = createClient(ALIBABA_CLOUD_ACCESS_KEY_ID, ALIBABA_CLOUD_ACCESS_KEY_SECRET); | |
async function sendMailViaAliyun(email: string, content: string, subject: string): Promise<void> { | |
let singleSendMailRequest = new $Dm20151123.SingleSendMailRequest({ | |
accountName: "[email protected]", | |
addressType: 1, | |
replyToAddress: true, | |
toAddress: email, | |
subject: subject, | |
//htmlBody: content, | |
textBody: content, | |
fromAlias: "<your_sender_alias>", | |
}); | |
let runtime = new $Util.RuntimeOptions({ }); | |
try { | |
// 复制代码运行请自行打印 API 的返回值 | |
await client.singleSendMailWithOptions(singleSendMailRequest, runtime); | |
} catch (error) { | |
// 错误 message | |
console.log(error.message); | |
// 诊断地址 | |
console.log(error.data["Recommend"]); | |
Util.assertAsString(error.message); | |
} | |
} | |
//for testing | |
//await sendMail('[email protected]', 'test content', 'test subject'); | |
export default sendMailViaAliyun; | |
//package.json | |
{ | |
"name": "", | |
"version": "1.0.0", | |
"description": "", | |
"main": "dist/client.js", | |
"scripts": { | |
"test": "mocha --reporter spec --timeout 3000 test/*.test.js", | |
"test-cov": "nyc -e .ts -r=html -r=text -r=lcov npm run test", | |
"build": "tsc", | |
"prepublishOnly": "tsc" | |
}, | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"@types/node": "^12.12.26", | |
"nyc": "^15.0.0", | |
"source-map-support": "^0.5.16", | |
"ts-node": "^8.6.2", | |
"typescript": "^3.7.5" | |
}, | |
"dependencies": { | |
"@alicloud/tea-typescript": "^1.7.1", | |
"@alicloud/dm20151123": "1.0.8", | |
"@alicloud/openapi-client": "^0.4.7", | |
"@alicloud/tea-console": "^1.0.0", | |
"@alicloud/tea-util": "^1.4.7" | |
}, | |
"files": [ | |
"dist", | |
"src" | |
] | |
} | |
//Build it in terminal | |
npm run build | |
//2. Your TS project with ES module | |
//tsconfig.json | |
{ | |
"compilerOptions": { | |
"types": ["node"], | |
"lib": ["ES2020"], | |
"target": "ES2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, | |
"module": "esnext" /* esnext - Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, | |
"outDir": "./types" /* Redirect output structure to the directory. */, | |
... | |
} | |
//main.ts | |
//copy client.js (renamed to client.cjs), client.d.ts, client.js.map to your project's /dist sub-folder | |
import { createRequire } from 'module'; | |
const require = createRequire(import.meta.url); | |
const sendMail = require('../dist/client.cjs').default; | |
//specify target email, subject and text content to verify if it works! | |
await sendMail(email, content, subject); | |
//enjoy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment