Merge pull request #1606 from HeyPuter/DS/main

feat: allow setting puter origin via globalThis variables
This commit is contained in:
Daniel Salazar
2025-09-23 14:26:17 -07:00
committed by GitHub
5 changed files with 38 additions and 6 deletions

View File

@@ -88,6 +88,21 @@ puter.ai.chat('What color was Napoleon\'s white horse?').then(response => {
<br>
## Setting Custom Origins
By default puter.js uses the official Puter API and GUI origins. You can customize these origins by setting global variables before importing the SDK like so:
```js
// For API origin
globalThis.PUTER_API_ORIGIN = 'https://custom-api.puter.com';
// For GUI origin
globalThis.PUTER_ORIGIN = 'https://custom-gui.puter.com';
import {puter} from '@heyputer/puter.js'; // or however you import it for your env
```
<br>
---
## Documentation & Community
- [Developer Site](https://developer.puter.com)

View File

@@ -1,6 +1,6 @@
{
"name": "@heyputer/puter.js",
"version": "2.0.12",
"version": "2.0.13",
"description": "Puter.js - A JavaScript library for interacting with Puter services.",
"main": "src/index.js",
"types": "index.d.ts",

View File

@@ -45,8 +45,22 @@ const puterInit = (function() {
// 'web' means the SDK is running in a 3rd-party website.
env;
defaultAPIOrigin = globalThis.PUTER_API_ORIGIN ?? 'https://api.puter.com';
defaultGUIOrigin = globalThis.PUTER_ORIGIN ?? 'https://puter.com';
#defaultAPIOrigin = 'https://api.puter.com';
#defaultGUIOrigin = 'https://puter.com';
get defaultAPIOrigin() {
return globalThis.PUTER_API_ORIGIN || globalThis.PUTER_API_ORIGIN_ENV || this.#defaultAPIOrigin;
}
set defaultAPIOrigin(v) {
this.#defaultAPIOrigin = v;
}
get defaultGUIOrigin() {
return globalThis.PUTER_ORIGIN || globalThis.PUTER_ORIGIN_ENV || this.#defaultGUIOrigin;
}
set defaultGUIOrigin(v) {
this.#defaultGUIOrigin = v;
}
// An optional callback when the user is authenticated. This can be set by the app using the SDK.
onAuth;

View File

@@ -7,7 +7,10 @@ const { resolve } = require('node:path');
* @returns {import('../index').puter} The `puter` object from puter.js
*/
const init = (authToken) => {
const goodContext = {};
const goodContext = {
PUTER_API_ORIGIN: globalThis.PUTER_API_ORIGIN,
PUTER_ORIGIN: globalThis.PUTER_ORIGIN,
};
Object.getOwnPropertyNames(globalThis).forEach(name => {
try {
goodContext[name] = globalThis[name];

View File

@@ -18,8 +18,8 @@ export default {
},
plugins: [
new webpack.DefinePlugin({
'globalThis.PUTER_ORIGIN': JSON.stringify(process.env.PUTER_ORIGIN || 'https://puter.com'),
'globalThis.PUTER_API_ORIGIN': JSON.stringify(process.env.PUTER_API_ORIGIN || 'https://api.puter.com'),
'globalThis.PUTER_ORIGIN_ENV': JSON.stringify(process.env.PUTER_ORIGIN || 'https://puter.com'),
'globalThis.PUTER_API_ORIGIN_ENV': JSON.stringify(process.env.PUTER_API_ORIGIN || 'https://api.puter.com'),
}),
],
};