forked from replicate/replicate-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (68 loc) · 1.91 KB
/
index.js
File metadata and controls
72 lines (68 loc) · 1.91 KB
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
const Replicate = require("./lib/replicate");
const ApiError = require("./lib/error");
/**
* Placeholder class used to warn of deprecated constructor.
* @deprecated use exported Replicate class instead
*/
class DeprecatedReplicate extends Replicate {
/** @deprecated Use `import { Replicate } from "replicate";` instead */
// biome-ignore lint/complexity/noUselessConstructor: exists for the tsdoc comment
constructor(...args) {
super(...args);
}
}
const named = { ApiError, Replicate };
const singleton = new Replicate();
/**
* Default instance of the Replicate class that gets the access token
* from the REPLICATE_API_TOKEN environment variable.
*
* Create a new Replicate API client instance.
*
* @example
*
* import replicate from "replicate";
*
* // Run a model and await the result:
* const model = 'owner/model:version-id'
* const input = {text: 'Hello, world!'}
* const output = await replicate.run(model, { input });
*
* @remarks
*
* NOTE: Use of this object as a constructor is deprecated and will
* be removed in a future version. Import the Replicate constructor
* instead:
*
* ```
* const Replicate = require("replicate").Replicate;
* ```
*
* Or in commonjs:
*
* ```
* import { Replicate } from "replicate";
* const client = new Replicate({...});
* ```
*
* @type { Replicate & typeof DeprecatedReplicate & {ApiError: ApiError, Replicate: Replicate} }
*/
const replicate = new Proxy(DeprecatedReplicate, {
get(target, prop, receiver) {
// Should mostly behave like the singleton.
if (named[prop]) {
return named[prop];
}
// Provide Replicate & ApiError constructors.
if (singleton[prop]) {
return singleton[prop];
}
// Fallback to Replicate constructor properties.
return Reflect.get(target, prop, receiver);
},
set(_target, prop, newValue, _receiver) {
singleton[prop] = newValue;
return true;
}
});
module.exports = replicate;