mirror of
https://github.com/samuelthomas2774/nxapi.git
synced 2026-09-07 02:45:09 -05:00
Add HTTP server
This commit is contained in:
81
src/api/znc-proxy.ts
Normal file
81
src/api/znc-proxy.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import fetch from 'node-fetch';
|
||||
import createDebug from 'debug';
|
||||
import { ActiveEvent, Announcement, CurrentUser, Friend, WebService, WebServiceToken } from './znc-types.js';
|
||||
import { ErrorResponse } from './util.js';
|
||||
import ZncApi from './znc.js';
|
||||
import { SavedToken } from '../util.js';
|
||||
|
||||
const debug = createDebug('api:znc-proxy');
|
||||
|
||||
export default class ZncProxyApi implements ZncApi {
|
||||
constructor(
|
||||
private url: string,
|
||||
// ZncApi uses the NSO token (valid for a few hours)
|
||||
// ZncProxyApi uses the Nintendo Account session token (valid for two years)
|
||||
public token: string
|
||||
) {}
|
||||
|
||||
async fetch<T = unknown>(url: string, method = 'GET', body?: string, headers?: object) {
|
||||
const response = await fetch(this.url + url, {
|
||||
method: method,
|
||||
headers: Object.assign({
|
||||
'Authorization': 'na ' + this.token,
|
||||
}, headers),
|
||||
body: body,
|
||||
});
|
||||
|
||||
debug('fetch %s %s, response %s', method, url, response.status);
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new ErrorResponse('[zncproxy] Unknown error', response);
|
||||
}
|
||||
|
||||
const data = await response.json() as T;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
async getAnnouncements() {
|
||||
const response = await this.fetch<{announcements: Announcement[]}>('/announcements');
|
||||
return {status: 0 as const, result: response.announcements, correlationId: ''};
|
||||
}
|
||||
|
||||
async getFriendList() {
|
||||
const response = await this.fetch<{friends: Friend[]}>('/friends');
|
||||
return {status: 0 as const, result: response, correlationId: ''};
|
||||
}
|
||||
|
||||
async getWebServices() {
|
||||
const response = await this.fetch<{webservices: WebService[]}>('/webservices');
|
||||
return {status: 0 as const, result: response.webservices, correlationId: ''};
|
||||
}
|
||||
|
||||
async getActiveEvent() {
|
||||
const response = await this.fetch<{activeevent: ActiveEvent}>('/activeevent');
|
||||
return {status: 0 as const, result: response.activeevent, correlationId: ''};
|
||||
}
|
||||
|
||||
async getCurrentUser() {
|
||||
const response = await this.fetch<{user: CurrentUser}>('/user');
|
||||
return {status: 0 as const, result: response.user, correlationId: ''};
|
||||
}
|
||||
|
||||
async getWebServiceToken(id: string) {
|
||||
const response = await this.fetch<{token: WebServiceToken}>('/webservice/' + id + '/token');
|
||||
return {status: 0 as const, result: response.token, correlationId: ''};
|
||||
}
|
||||
|
||||
async renewToken() {
|
||||
const data = await this.fetch<SavedToken>('/auth');
|
||||
data.proxy_url = this.url;
|
||||
return data;
|
||||
}
|
||||
|
||||
static async createWithSessionToken(url: string, token: string) {
|
||||
const nso = new this(url, token);
|
||||
const data = await nso.fetch<SavedToken>('/auth');
|
||||
data.proxy_url = url;
|
||||
|
||||
return {nso, data};
|
||||
}
|
||||
}
|
||||
@@ -106,10 +106,9 @@ export interface CurrentUser {
|
||||
links: {
|
||||
nintendoAccount: {
|
||||
membership: {
|
||||
// active: {
|
||||
// active: boolean;
|
||||
// };
|
||||
active: boolean;
|
||||
active: {
|
||||
active: boolean;
|
||||
} | boolean;
|
||||
};
|
||||
};
|
||||
friendCode: {
|
||||
|
||||
@@ -17,10 +17,10 @@ const ZNC_URL = 'https://api-lp1.znc.srv.nintendo.net';
|
||||
|
||||
export default class ZncApi {
|
||||
constructor(
|
||||
private token: string
|
||||
public token: string
|
||||
) {}
|
||||
|
||||
protected async fetch<T = unknown>(url: string, method = 'GET', body?: string, headers?: object) {
|
||||
async fetch<T = unknown>(url: string, method = 'GET', body?: string, headers?: object) {
|
||||
const response = await fetch(ZNC_URL + url, {
|
||||
method: method,
|
||||
headers: Object.assign({
|
||||
@@ -33,6 +33,8 @@ export default class ZncApi {
|
||||
body: body,
|
||||
});
|
||||
|
||||
debug('fetch %s %s, response %s', method, url, response.status);
|
||||
|
||||
const data = await response.json() as ZncResponse<T>;
|
||||
|
||||
if ('errorMessage' in data) {
|
||||
|
||||
Reference in New Issue
Block a user