-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
90 lines (83 loc) · 2.59 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* ProdiaAI is a class for handling ProdiaAI images.
*/
class ProdiaAI {
#API_DOMAIN = 'https://api.prodia.com'
#API_VERSION = 'v1'
/**
* Initializes a ProdiaAI instance.
* @param {String} key - Your API key.
*/
constructor (key) {
this.key = key
}
/**
* Creates an AI image job.
* @param {Object} config - Image job configuration.
* @param {String} config.prompt - Image prompt.
* @param {String} config.negative_prompt - Negative image prompt.
* @param {Number} config.steps - Number of image generation steps.
* @param {Number} config.cfg_scale - Image generation scale.
* @param {String} config.sampler - Image generation sampler.
* @param {String} config.model - Image generation model.
* @returns {Promise<{ job: String, status: String }>} object - Promise with the API response content.
*/
async createJob(config) {
if (!config?.prompt && config.prompt === '') {
throw new Error('Prompt is required!')
}
const fetch_response = await fetch(`${this.#API_DOMAIN}/${this.#API_VERSION}/job`, {
method: 'POST',
headers: {
'X-Prodia-Key': this.key,
accept: 'application/json',
'content-type': 'application/json',
},
body: JSON.stringify(config),
})
return await fetch_response.json()
}
/**
* Retrieves information about a specific job ID.
* @param {String} jobId - Job ID.
* @returns {Promise<{ job: string, status: string, imageUrl: string }>} - Promise with the API response content.
*/
async getJob (jobId) {
if (!jobId) {
throw new Error('JobID is required!')
}
const fetch_response = await fetch(`${this.#API_DOMAIN}/${this.#API_VERSION}/job/${jobId}`, {
headers: {
'X-Prodia-Key': this.key,
'content-type': 'application/json',
}
})
return await fetch_response.json()
}
/**
* Get a list of current available models.
* @returns {Promise<string[]>} - Promise with the API response content.
*/
async getModels () {
const fetch_response = await fetch(`${this.#API_DOMAIN}/${this.#API_VERSION}/models/list`, {
headers: {
'X-Prodia-Key': this.key,
'content-type': 'application/json',
}
})
return await fetch_response.json()
}
}
/**
* Creates an instance of ProdiaAI.
* @param {string} key - The API key used for authentication.
* @returns {ProdiaAI} The ProdiaAI instance.
* @throws {Error} If the API key is missing.
*/
const createProdiaAI = (key) => {
if (!key) {
throw new Error('API Key is required')
}
return new ProdiaAI(key)
}
export default createProdiaAI