mirror of
https://github.com/Terncode/pixel.horse.git
synced 2026-09-24 21:55:52 +02:00
Switch to node-fetch
This commit is contained in:
@@ -8,6 +8,7 @@ import * as Bluebird from 'bluebird';
|
||||
import * as fs from 'fs';
|
||||
import * as yargs from 'yargs';
|
||||
import { hideBin } from 'yargs/helpers';
|
||||
import { noop } from 'lodash';
|
||||
|
||||
const argv = (yargs as any)(hideBin(process.argv))
|
||||
.option('beta', {
|
||||
@@ -29,4 +30,18 @@ const argv = (yargs as any)(hideBin(process.argv))
|
||||
(global as any).TESTS = false;
|
||||
(global as any).performance = Date;
|
||||
|
||||
function PerformanceDate(...args: any[]) {
|
||||
//@ts-ignore
|
||||
return new Date(...args);
|
||||
}
|
||||
|
||||
PerformanceDate.now = Date.now;
|
||||
PerformanceDate.parse = Date.parse;
|
||||
PerformanceDate.UTC = Date.UTC;
|
||||
|
||||
PerformanceDate.markResourceTiming = noop;
|
||||
|
||||
(global as any).performance = PerformanceDate;
|
||||
|
||||
|
||||
Bluebird.promisifyAll(fs);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import * as request from 'request-promise';
|
||||
import { noop, flatMap, uniq } from 'lodash';
|
||||
import {
|
||||
InternalGameServerState, ServerStatus, InternalLoginApi, InternalLoginServerState, InternalApi, HidingStats
|
||||
@@ -82,13 +81,24 @@ export function getServer(id: string) {
|
||||
export function createApi<T extends {}>(host: string, url: string, apiToken: string): T {
|
||||
return new Proxy<T>({} as any, {
|
||||
get: (_, key) =>
|
||||
(...args: any[]) =>
|
||||
Promise.resolve<T>(request(`http://${host}/${url}/api`, {
|
||||
json: true,
|
||||
headers: { 'api-token': apiToken },
|
||||
method: 'post',
|
||||
body: { method: key, args },
|
||||
})),
|
||||
async (...args: any[]) => {
|
||||
const response = await fetch(`http://${host}/${url}/api`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'api-token': apiToken,
|
||||
},
|
||||
body: JSON.stringify({ method: key, args }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`API call failed — ${response.status} ${response.statusText} on method "${String(key)}"`
|
||||
);
|
||||
}
|
||||
|
||||
return response.json() as Promise<T>;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+13
-2
@@ -5,7 +5,7 @@ import * as mongoose from 'mongoose';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { deleteAsync } from 'del';
|
||||
import { once, mapValues } from 'lodash';
|
||||
import { once, mapValues, noop } from 'lodash';
|
||||
import { spawnSync } from 'child_process';
|
||||
import { createStubInstance, SinonStubbedInstance, stub } from 'sinon';
|
||||
import '../server/canvasUtilsNode';
|
||||
@@ -22,7 +22,18 @@ require('chai').use(require('chai-as-promised'));
|
||||
(mongoose as any).modelSchemas = {};
|
||||
(global as any).TESTS = true;
|
||||
(global as any).TOOLS = true;
|
||||
(global as any).performance = Date;
|
||||
function PerformanceDate(...args: any[]) {
|
||||
//@ts-ignore
|
||||
return new Date(...args);
|
||||
}
|
||||
|
||||
PerformanceDate.now = Date.now;
|
||||
PerformanceDate.parse = Date.parse;
|
||||
PerformanceDate.UTC = Date.UTC;
|
||||
|
||||
PerformanceDate.markResourceTiming = noop;
|
||||
|
||||
(global as any).performance = PerformanceDate;
|
||||
|
||||
setPaletteManager(mockPaletteManager);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user