Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #30 from FelixWaweru/endpoint-add
Browse files Browse the repository at this point in the history
Feature Updates
  • Loading branch information
FelixWaweru authored Sep 7, 2023
2 parents 754faf8 + 1be359a commit f6f6a4a
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 10 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/pre-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This workflow will run tests using Jest
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: NPM Package Test

on:
push:
branches:
- test

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm install
- run: npm test
env:
ELEVENLABS_API_KEY: ${{ secrets.ELEVENLABS_API_KEY }}
ELEVENLABS_VOICE_ID: ${{ secrets.ELEVENLABS_VOICE_ID }}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ This is an open source Eleven Labs NodeJS package for converting text to speech
| `getVoice` | (apiKey, voiceID) | `/v1/voices/{voice_id}` |
| `deleteVoice` | (apiKey, voiceID) | `/v1/voices/{voice_id}` |
| `editVoiceSettings` | (apiKey, voiceID, stability, similarityBoost) | `/v1/voices/{voice_id}/settings/edit` |
| `getModels` | (apiKey) | `/v1/models` |
| `getUserInfo` | (apiKey) | `/v1/user` |
| `getUserSubscription` | (apiKey) | `/v1/user/subscription` |

## Requirements

Expand Down
112 changes: 107 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ const textToSpeech = async (

response.data.pipe(fs.createWriteStream(fileName));

return {
status: "ok",
};
const writeStream = fs.createWriteStream(fileName);
response.data.pipe(writeStream);

return new Promise((resolve, reject) => {
const responseJson = { status: "ok", fileName: fileName };
writeStream.on('finish', () => resolve(responseJson));

writeStream.on('error', reject);
});
} catch (error) {
console.log(error);
}
Expand All @@ -85,6 +91,8 @@ Function that converts text to speech and returns a readable stream of the audio
@param {string} modelId - The model to use for the text-to-speech conversion. If null, it will use elevenlab's default model.
@param {string} responseType - The response type for the text-to-speech function (arrayBuffer, stream, etc). If null, it will use 'stream' by default.
@returns {Object} - A readable stream of the audio data.
*/
const textToSpeechStream = async (
Expand All @@ -93,7 +101,8 @@ const textToSpeechStream = async (
textInput,
stability,
similarityBoost,
modelId
modelId,
responseType
) => {
try {
if (!apiKey || !voiceID || !textInput) {
Expand All @@ -120,7 +129,7 @@ const textToSpeechStream = async (
"xi-api-key": apiKey,
"Content-Type": "application/json",
},
responseType: "stream",
responseType: responseType ? responseType : "stream"
});

return response.data;
Expand Down Expand Up @@ -323,6 +332,96 @@ const editVoiceSettings = async (
}
};

/**
Function that returns an object containing the list of voice models.
@param {string} apiKey - The API key to authenticate the request.
@returns {Object} - An object containing the list of voice models and their details.
*/
const getModels = async (apiKey) => {
try {
if (!apiKey) {
console.log("ERR: Missing parameter");
}

const voiceURL = `${elevenLabsAPI}/models`;

const response = await axios({
method: "GET",
url: voiceURL,
headers: {
"xi-api-key": apiKey,
},
});

return response.data;
} catch (error) {
console.log(error);
}
};

/**
Function that returns the user details.
@param {string} apiKey - The API key to authenticate the request.
@returns {Object} - An object containing the user details.
*/
const getUserInfo = async (apiKey) => {
try {
if (!apiKey) {
console.log("ERR: Missing parameter");
}

const voiceURL = `${elevenLabsAPI}/user`;

const response = await axios({
method: "GET",
url: voiceURL,
headers: {
"xi-api-key": apiKey,
},
});

return response.data;
} catch (error) {
console.log(error);
}
};

/**
Function that returns the user subscription details.
@param {string} apiKey - The API key to authenticate the request.
@returns {Object} - An object containing the user subscription details.
*/
const getUserSubscription = async (apiKey) => {
try {
if (!apiKey) {
console.log("ERR: Missing parameter");
}

const voiceURL = `${elevenLabsAPI}/user/subscription`;

const response = await axios({
method: "GET",
url: voiceURL,
headers: {
"xi-api-key": apiKey,
},
});

return response.data;
} catch (error) {
console.log(error);
}
};

module.exports = {
textToSpeech: textToSpeech,
textToSpeechStream: textToSpeechStream,
Expand All @@ -332,4 +431,7 @@ module.exports = {
getVoice: getVoice,
deleteVoice: deleteVoice,
editVoiceSettings: editVoiceSettings,
getModels: getModels,
getUserInfo: getUserInfo,
getUserSubscription: getUserSubscription,
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "elevenlabs-node",
"version": "1.1.3",
"version": "1.2.0",
"description": "This is an open source Eleven Labs NodeJS package for converting text to speech using the Eleven Labs API",
"main": "index.js",
"scripts": {
"test": "jest --testPathPattern=test"
"test": "jest --testPathPattern=test --detectOpenHandles --forceExit"
},
"repository": {
"type": "git",
Expand Down
44 changes: 41 additions & 3 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const textInput = 'mozzy is cool';
const stability = '0.5';
const similarityBoost = '0.5';
const modelId = 'eleven_multilingual_v1';
const responseType = 'stream';

describe("Eleven Labs Node Unit Test", () => {

// textToSpeech test
test("Test textToSpeech", async () => {
// Execute test
await process.nextTick(() => {});
const response = await script.textToSpeech(apiKey, voiceID, fileName, textInput, stability, similarityBoost, modelId);

// Response check
Expand All @@ -22,7 +24,8 @@ describe("Eleven Labs Node Unit Test", () => {
// textToSpeechStream test
test("Test textToSpeechStream", async () => {
// Execute test
const response = await script.textToSpeechStream(apiKey, voiceID, textInput, stability, similarityBoost, modelId);
await process.nextTick(() => {});
const response = await script.textToSpeechStream(apiKey, voiceID, textInput, stability, similarityBoost, modelId, responseType);

// Response check
expect(!response).toBeFalsy();
Expand All @@ -31,6 +34,7 @@ describe("Eleven Labs Node Unit Test", () => {
// getVoices test
test("Test getVoices", async () => {
// Execute test
await process.nextTick(() => {});
const response = await script.getVoices(apiKey);

// Response check
Expand All @@ -40,6 +44,7 @@ describe("Eleven Labs Node Unit Test", () => {
// getDefaultVoiceSettings test
test("Test getDefaultVoiceSettings", async () => {
// Execute test
await process.nextTick(() => {});
const response = await script.getDefaultVoiceSettings();

// Response check
Expand All @@ -50,6 +55,7 @@ describe("Eleven Labs Node Unit Test", () => {
// getVoiceSettings test
test("Test getVoiceSettings", async () => {
// Execute test
await process.nextTick(() => {});
const response = await script.getVoiceSettings(apiKey, voiceID);

// Response check
Expand All @@ -60,6 +66,7 @@ describe("Eleven Labs Node Unit Test", () => {
// getVoice test
test("Test getVoice", async () => {
// Execute test
await process.nextTick(() => {});
const response = await script.getVoice(apiKey, voiceID);

// Response check
Expand All @@ -69,14 +76,45 @@ describe("Eleven Labs Node Unit Test", () => {
// editVoiceSettings test
test("Test editVoiceSettings", async () => {
// Execute test
await process.nextTick(() => {});
const response = await script.editVoiceSettings(apiKey, voiceID, stability, similarityBoost);

// Response check
expect(response.status).toEqual('ok');
});

// deleteVoice test
// TODO: Add create voice test first
// getModels test
test("Test getModels", async () => {
// Execute test
await process.nextTick(() => {});
const response = await script.getModels(apiKey);

// Response check
expect(response).toBeTruthy();
});

// getUserInfo test
test("Test getUserInfo", async () => {
// Execute test
await process.nextTick(() => {});
const response = await script.getUserInfo(apiKey);

// Response check
expect(response.xi_api_key).toEqual(apiKey);
});

// getUserSubscription test
test("Test getUserSubscription", async () => {
// Execute test
await process.nextTick(() => {});
const response = await script.getUserSubscription(apiKey);

// Response check
expect(response.status).toBeTruthy();
});

// deleteVoice test
// TODO: Add create voice test first
// test("Test deleteVoice", async () => {
// // Execute test
// const response = await script.deleteVoice(apiKey, voiceID);
Expand Down

0 comments on commit f6f6a4a

Please sign in to comment.