Skip to content

HTTP Requests

Make HTTP requests during build.

Methods

get(url, options?)

GET request:

javascript
const response = await gemshell.http.get('https://api.example.com/data');
console.log(response.status); // 200
console.log(response.data);   // Response body

post(url, body?, options?)

POST request:

javascript
const response = await gemshell.http.post(
  'https://api.example.com/builds',
  { game: 'My Game', version: '1.0.0' }
);

put(url, body?, options?)

PUT request:

javascript
await gemshell.http.put('https://api.example.com/status', { status: 'building' });

delete(url, options?)

DELETE request:

javascript
await gemshell.http.delete('https://api.example.com/cache/123');

request(options)

Custom request:

javascript
const response = await gemshell.http.request({
  method: 'PATCH',
  url: 'https://api.example.com/resource',
  headers: { 'Authorization': 'Bearer token123' },
  body: { field: 'value' },
  timeout: 10000
});

Options

javascript
{
  headers: { 'Content-Type': 'application/json' },
  timeout: 5000,  // milliseconds
  auth: { username: 'user', password: 'pass' }
}

Response

javascript
{
  status: 200,
  statusText: 'OK',
  headers: { 'content-type': 'application/json' },
  data: { ... }  // Parsed JSON or string
}

Example: Discord Webhook

javascript
module.exports = {
  name: 'Discord Notifier',
  version: '1.0.0',
  
  settings: [
    { id: 'webhookUrl', type: 'text', label: 'Webhook URL' },
    { id: 'notifyOnSuccess', type: 'checkbox', label: 'Notify on success', default: true },
    { id: 'notifyOnError', type: 'checkbox', label: 'Notify on error', default: true }
  ],
  
  async onPostBuild(context, settings) {
    if (!settings.notifyOnSuccess || !settings.webhookUrl) return;
    
    await gemshell.http.post(settings.webhookUrl, {
      embeds: [{
        title: 'Build Complete',
        description: `**${context.config.title}** v${context.config.version}`,
        color: 0x4ade80,
        fields: [
          { name: 'Platform', value: context.platform, inline: true },
          { name: 'Arch', value: context.arch, inline: true }
        ],
        timestamp: new Date().toISOString()
      }]
    });
    
    gemshell.log('Discord notification sent');
  },
  
  async onBuildError(context, settings) {
    if (!settings.notifyOnError || !settings.webhookUrl) return;
    
    await gemshell.http.post(settings.webhookUrl, {
      embeds: [{
        title: 'Build Failed',
        description: context.error,
        color: 0xef4444,
        timestamp: new Date().toISOString()
      }]
    });
  }
};

Example: Upload to Server

javascript
module.exports = {
  name: 'Auto Upload',
  version: '1.0.0',
  
  settings: [
    { id: 'apiKey', type: 'text', label: 'API Key' },
    { id: 'serverUrl', type: 'text', label: 'Server URL' }
  ],
  
  async onPostBuild(context, settings) {
    if (!settings.apiKey || !settings.serverUrl) return;
    
    const buildInfo = {
      game: context.config.title,
      version: context.config.version,
      platform: context.platform,
      arch: context.arch,
      timestamp: new Date().toISOString()
    };
    
    const response = await gemshell.http.post(
      `${settings.serverUrl}/api/builds`,
      buildInfo,
      {
        headers: {
          'Authorization': `Bearer ${settings.apiKey}`,
          'Content-Type': 'application/json'
        }
      }
    );
    
    if (response.status === 200) {
      gemshell.log('Build registered on server');
    } else {
      gemshell.warn('Failed to register build:', response.status);
    }
  }
};

Error Handling

javascript
try {
  const response = await gemshell.http.get('https://api.example.com/data');
  // Handle response
} catch (error) {
  gemshell.error('HTTP request failed:', error.message);
}