@kne/fastify-aliyun 1.1.7 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/libs/controllers.js +32 -4
- package/libs/schema/response.js +11 -0
- package/package.json +1 -1
package/libs/controllers.js
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
+
const { str, num, any, success } = require('./schema/response');
|
|
3
|
+
|
|
4
|
+
const TAG = '阿里云-智能语音';
|
|
5
|
+
|
|
2
6
|
module.exports = fp(async (fastify, options) => {
|
|
3
7
|
fastify.post(
|
|
4
8
|
`${options.prefix}/nls/getToken`,
|
|
5
9
|
{
|
|
6
|
-
onRequest: options.createAuthenticate('nls:getToken')
|
|
10
|
+
onRequest: options.createAuthenticate('nls:getToken'),
|
|
11
|
+
schema: {
|
|
12
|
+
tags: [TAG],
|
|
13
|
+
summary: '获取智能语音访问令牌',
|
|
14
|
+
description: '用配置的 AccessKey 调用阿里云 CreateToken 获取 NLS 令牌,令牌在过期前缓存复用;供前端直连阿里云语音服务',
|
|
15
|
+
response: success('NLS 令牌', {
|
|
16
|
+
token: str('NLS 访问令牌'),
|
|
17
|
+
appKey: str('配置的 NLS 项目 appKey')
|
|
18
|
+
})
|
|
19
|
+
}
|
|
7
20
|
},
|
|
8
21
|
async () => {
|
|
9
22
|
const { services } = fastify.aliyun;
|
|
@@ -16,14 +29,29 @@ module.exports = fp(async (fastify, options) => {
|
|
|
16
29
|
{
|
|
17
30
|
onRequest: options.createAuthenticate('nls:tts'),
|
|
18
31
|
schema: {
|
|
32
|
+
tags: [TAG],
|
|
33
|
+
summary: '语音合成',
|
|
34
|
+
description: '调用阿里云语音合成把文本转为 wav 音频,并通过 fastify-file-manager 保存为文件记录;合成失败时返回阿里云的错误信息',
|
|
19
35
|
body: {
|
|
20
36
|
type: 'object',
|
|
21
37
|
required: ['text'],
|
|
22
38
|
properties: {
|
|
23
|
-
text: { type: 'string' },
|
|
24
|
-
audioName: { type: 'string' }
|
|
39
|
+
text: { type: 'string', description: '要合成的文本' },
|
|
40
|
+
audioName: { type: 'string', description: '保存的音频文件名(不含扩展名),默认 audio' }
|
|
25
41
|
}
|
|
26
|
-
}
|
|
42
|
+
},
|
|
43
|
+
response: success('保存的音频文件记录(fastify-file-manager 的 uploadToFileSystem 返回值)', {
|
|
44
|
+
id: any('文件 ID(即文件记录的 uuid),用于下载或获取文件地址'),
|
|
45
|
+
filename: str('文件名,如 audio.wav'),
|
|
46
|
+
hash: str('文件内容 md5'),
|
|
47
|
+
namespace: str('文件命名空间'),
|
|
48
|
+
size: num('文件大小(字节)'),
|
|
49
|
+
encoding: str('编码'),
|
|
50
|
+
mimetype: str('MIME 类型'),
|
|
51
|
+
storageType: str('存储类型:local 本地文件系统,oss 远程 OSS'),
|
|
52
|
+
createdAt: any('创建时间'),
|
|
53
|
+
updatedAt: any('更新时间')
|
|
54
|
+
})
|
|
27
55
|
}
|
|
28
56
|
},
|
|
29
57
|
async request => {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// 响应 schema 会参与 fast-json-stringify 序列化:对象一律 additionalProperties: true 以免裁掉未声明字段;
|
|
2
|
+
// 用 nullable 而非类型数组,类型不确定的字段(如 id、日期)只写说明不声明类型
|
|
3
|
+
const str = description => ({ type: 'string', nullable: true, description });
|
|
4
|
+
const num = description => ({ type: 'number', nullable: true, description });
|
|
5
|
+
const any = description => ({ description });
|
|
6
|
+
|
|
7
|
+
const success = (description, properties) => ({
|
|
8
|
+
200: { type: 'object', additionalProperties: true, description, properties: properties || {} }
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
module.exports = { str, num, any, success };
|