Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit ea7e69a

Browse files
authored
Add Vitest (#2)
* Add vitest Add vitest for project testing, ensuring stable quality. Modify new initialization method. * update package version
1 parent 8fa090e commit ea7e69a

11 files changed

+1069
-82
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ node_modules
1111
dist
1212
dist-ssr
1313
*.local
14+
coverage
1415

1516
# Editor directories and files
1617
.vscode/*

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ npm install prodia-ai
2727
## Usage
2828
### Initialization
2929
```js
30-
import ProdiaAI from 'prodia-ai'
30+
import createProdiaAI from 'prodia-ai'
3131

32-
const prodiai = new ProdiaAI({
33-
key: 'prodia-key'
34-
})
32+
const prodiai = createProdiaAI('prodia-key')
3533

3634
export default prodiai
3735
```

main.d.ts

+50-43
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,52 @@
1-
declare module 'prodia-ai' {
1+
/**
2+
* ProdiaAI is a class for handling ProdiaAI images.
3+
*/
4+
declare class ProdiaAI {
5+
private readonly key: string
6+
27
/**
3-
* ProdiaAI is a class for handling ProdiaAI images.
8+
* Initializes a ProdiaAI instance.
9+
* @param {Object} obj - Initialization parameters object.
10+
* @param {String} obj.key - Your API key.
411
*/
5-
export default class ProdiaAI {
6-
#API_DOMAIN: string;
7-
#API_VERSION: string;
8-
9-
/**
10-
* Initializes a ProdiaAI instance.
11-
* @param {Object} obj - Initialization parameters object.
12-
* @param {String} obj.key - Your API key.
13-
*/
14-
constructor({ key }: { key: string });
15-
16-
models: string[];
17-
18-
/**
19-
* Creates an AI image job.
20-
* @param {Object} config - Image job configuration.
21-
* @param {String} config.prompt - Image prompt.
22-
* @param {String} config.negative_prompt - Negative image prompt.
23-
* @param {Number} config.steps - Number of image generation steps.
24-
* @param {Number} config.cfg_scale - Image generation scale.
25-
* @param {String} config.sampler - Image generation sampler.
26-
* @param {String} config.model - Image generation model.
27-
* @returns {Promise<String>} - Promise with the API response content.
28-
*/
29-
createJob(config: {
30-
prompt: string;
31-
negative_prompt: string;
32-
steps: number;
33-
cfg_scale: number;
34-
sampler: string;
35-
model: string;
36-
}): Promise<string>;
37-
38-
/**
39-
* Retrieves information about a specific job ID.
40-
* @param {String} jobId - Job ID.
41-
* @returns {Promise<String>} - Promise with the API response content.
42-
*/
43-
getJob(jobId: string): Promise<string>;
44-
}
45-
}
12+
constructor(key: string)
13+
14+
models: string[]
15+
16+
/**
17+
* Creates an AI image job.
18+
* @param {Object} config - Image job configuration.
19+
* @param {String} config.prompt - Image prompt.
20+
* @param {String} config.negative_prompt - Negative image prompt.
21+
* @param {Number} config.steps - Number of image generation steps.
22+
* @param {Number} config.cfg_scale - Image generation scale.
23+
* @param {String} config.sampler - Image generation sampler.
24+
* @param {String} config.model - Image generation model.
25+
* @returns {Promise<String>} - Promise with the API response content.
26+
*/
27+
createJob(config: {
28+
prompt: string
29+
negative_prompt: string
30+
steps: number
31+
cfg_scale: number
32+
sampler: string
33+
model: string
34+
}): Promise<string>
35+
36+
/**
37+
* Retrieves information about a specific job ID.
38+
* @param {String} jobId - Job ID.
39+
* @returns {Promise<String>} - Promise with the API response content.
40+
*/
41+
getJob(jobId: string): Promise<string>
42+
}
43+
44+
/**
45+
* Creates an instance of ProdiaAI.
46+
* @param {string} key - The API key used for authentication.
47+
* @returns {ProdiaAI} The ProdiaAI instance.
48+
* @throws {Error} If the API key is missing.
49+
*/
50+
declare function createProdiaAI(key: string) : ProdiaAI
51+
52+
export default createProdiaAI

main.js

+33-17
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@
44
class ProdiaAI {
55
#API_DOMAIN = 'https://api.prodia.com'
66
#API_VERSION = 'v1'
7-
8-
/**
9-
* Initializes a ProdiaAI instance.
10-
* @param {Object} obj - Initialization parameters object.
11-
* @param {String} obj.key - Your API key.
12-
*/
13-
constructor({ key }) {
14-
this.key = key
15-
}
16-
7+
178
models = [
189
'analog-diffusion-1.0.ckpt [9ca13f02]',
1910
'anythingv3_0-pruned.ckpt [2700c435]',
@@ -43,6 +34,15 @@ class ProdiaAI {
4334
'timeless-1.0.ckpt [7c4971d4]'
4435
]
4536

37+
/**
38+
* Initializes a ProdiaAI instance.
39+
* @param {Object} obj - Initialization parameters object.
40+
* @param {String} obj.key - Your API key.
41+
*/
42+
constructor (key) {
43+
this.key = key
44+
}
45+
4646
/**
4747
* Creates an AI image job.
4848
* @param {Object} config - Image job configuration.
@@ -54,13 +54,15 @@ class ProdiaAI {
5454
* @param {String} config.model - Image generation model.
5555
* @returns {Promise<String>} - Promise with the API response content.
5656
*/
57-
async createJob(config){
58-
if (!config.prompt) {
57+
async createJob(config) {
58+
if (!config?.prompt && config.prompt === '') {
5959
throw new Error('Prompt is required!')
6060
}
61-
const model = this.models.includes(config.model)
62-
if (!model) {
63-
throw new Error('Model not accepted')
61+
if (config?.model) {
62+
const model = this.models.includes(config.model)
63+
if (!model) {
64+
throw new Error('Model not accepted')
65+
}
6466
}
6567
const fetch_response = await fetch(`${this.#API_DOMAIN}/${this.#API_VERSION}/job`, {
6668
method: 'POST',
@@ -79,7 +81,7 @@ class ProdiaAI {
7981
* @param {String} jobId - Job ID.
8082
* @returns {Promise<String>} - Promise with the API response content.
8183
*/
82-
async getJob(jobId){
84+
async getJob (jobId) {
8385
if (!jobId) {
8486
throw new Error('JobID is required!')
8587
}
@@ -93,4 +95,18 @@ class ProdiaAI {
9395
}
9496
}
9597

96-
export default ProdiaAI
98+
99+
/**
100+
* Creates an instance of ProdiaAI.
101+
* @param {string} key - The API key used for authentication.
102+
* @returns {ProdiaAI} The ProdiaAI instance.
103+
* @throws {Error} If the API key is missing.
104+
*/
105+
const createProdiaAI = (key) => {
106+
if (!key) {
107+
throw new Error('API Key is required')
108+
}
109+
return new ProdiaAI(key)
110+
}
111+
112+
export default createProdiaAI

0 commit comments

Comments
 (0)