UNPKG

@marceloclp/monzojs

Version:

Unofficial wrapper for the Monzo API written in TypeScript.

63 lines (62 loc) 1.95 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // eslint-disable-next-line simple-import-sort/imports const query_string_1 = __importDefault(require("query-string")); const constants_1 = require("../config/constants"); class Fetcher { constructor(accessToken) { this.headers = { Authorization: '', 'Content-Type': 'application/json', }; this.headers.Authorization = `Bearer ${accessToken}`; } static buildUrl(path, query) { return query_string_1.default.stringifyUrl({ url: `${constants_1.MONZO_API_BASE_URL}/${path}`, query }); } withFormData(data) { this.headers['Content-Type'] = 'application/x-www-form-urlencoded'; this.body = new URLSearchParams(data); return this; } withJSON(data) { this.headers['Content-Type'] = 'application/json'; this.body = JSON.stringify(data); return this; } withQuery(query) { this.query = query; return this; } async fetch(method, path) { const url = query_string_1.default.stringifyUrl({ url: `${constants_1.MONZO_API_BASE_URL}/${path}`, query: this.query, }); const request = { method, headers: this.headers, body: this.body, }; return fetch(url, request).then((response) => response.json()); } async get(path) { return this.fetch('GET', path); } async post(path) { return this.fetch('POST', path); } async put(path) { return this.fetch('PUT', path); } async patch(path) { return this.fetch('PATCH', path); } async delete(path) { return this.fetch('DELETE', path); } } exports.default = Fetcher;