From f382e65da7595557852bfa0be7116d425b46a943 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sun, 4 Apr 2021 23:16:07 +0900 Subject: [PATCH] refs #714 Add headers for pleroma api_clients --- package.json | 2 ++ src/mastodon.ts | 1 + src/misskey.ts | 2 ++ src/pleroma.ts | 1 + src/pleroma/api_client.ts | 46 ++++++++++++++++++++++----------------- yarn.lock | 10 +++++++++ 6 files changed, 42 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index f1f586d..5ec843c 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "form-data": "^4.0.0", "https-proxy-agent": "^5.0.0", "oauth": "^0.9.15", + "object-assign-deep": "^0.4.0", "socks-proxy-agent": "^5.0.0", "typescript": "4.2.3", "uuid": "^8.0.0", @@ -68,6 +69,7 @@ "devDependencies": { "@types/core-js": "^2.5.0", "@types/jest": "^26.0.20", + "@types/object-assign-deep": "^0.4.0", "@types/uuid": "^8.0.0", "@typescript-eslint/eslint-plugin": "^2.19.2", "@typescript-eslint/parser": "^2.19.2", diff --git a/src/mastodon.ts b/src/mastodon.ts index 3229dc9..9ab22ea 100644 --- a/src/mastodon.ts +++ b/src/mastodon.ts @@ -1,4 +1,5 @@ import { OAuth2 } from 'oauth' +import FormData from 'form-data' import MastodonAPI from './mastodon/api_client' import WebSocket from './mastodon/web_socket' diff --git a/src/misskey.ts b/src/misskey.ts index c046689..28802e6 100644 --- a/src/misskey.ts +++ b/src/misskey.ts @@ -1,3 +1,5 @@ +import FormData from 'form-data' + import MisskeyAPI from './misskey/api_client' import { DEFAULT_UA } from './default' import { ProxyConfig } from './proxy_config' diff --git a/src/pleroma.ts b/src/pleroma.ts index 44dbfe4..8269bf7 100644 --- a/src/pleroma.ts +++ b/src/pleroma.ts @@ -1,4 +1,5 @@ import { OAuth2 } from 'oauth' +import FormData from 'form-data' import PleromaAPI from './pleroma/api_client' import WebSocket from './pleroma/web_socket' diff --git a/src/pleroma/api_client.ts b/src/pleroma/api_client.ts index 2649856..4e1da40 100644 --- a/src/pleroma/api_client.ts +++ b/src/pleroma/api_client.ts @@ -1,4 +1,5 @@ import axios, { AxiosResponse, CancelTokenSource, AxiosRequestConfig } from 'axios' +import objectAssignDeep from 'object-assign-deep' import MegalodonEntity from '../entity' import PleromaEntity from './entity' @@ -217,11 +218,11 @@ namespace PleromaAPI { * Interface */ export interface Interface { - get(path: string, params?: any): Promise> - put(path: string, params?: any): Promise> - patch(path: string, params?: any): Promise> - post(path: string, params?: any): Promise> - del(path: string, params?: any): Promise> + get(path: string, params?: any, headers?: { [key: string]: string }): Promise> + put(path: string, params?: any, headers?: { [key: string]: string }): Promise> + patch(path: string, params?: any, headers?: { [key: string]: string }): Promise> + post(path: string, params?: any, headers?: { [key: string]: string }): Promise> + del(path: string, params?: any, headers?: { [key: string]: string }): Promise> cancel(): void socket(path: string, stream: string, params?: string): WebSocket } @@ -266,13 +267,14 @@ namespace PleromaAPI { * @param path relative path from baseUrl * @param params Query parameters */ - public async get(path: string, params = {}): Promise> { + public async get(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { cancelToken: this.cancelTokenSource.token, - params: params + params: params, + headers: headers } if (this.accessToken) { - options = Object.assign(options, { + options = objectAssignDeep({}, options, { headers: { Authorization: `Bearer ${this.accessToken}` } @@ -309,12 +311,13 @@ namespace PleromaAPI { * @param path relative path from baseUrl * @param params Form data. If you want to post file, please use FormData() */ - public async put(path: string, params = {}): Promise> { + public async put(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - cancelToken: this.cancelTokenSource.token + cancelToken: this.cancelTokenSource.token, + headers: headers } if (this.accessToken) { - options = Object.assign(options, { + options = objectAssignDeep({}, options, { headers: { Authorization: `Bearer ${this.accessToken}` } @@ -351,12 +354,13 @@ namespace PleromaAPI { * @param path relative path from baseUrl * @param params Form data. If you want to post file, please use FormData() */ - public async patch(path: string, params = {}): Promise> { + public async patch(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - cancelToken: this.cancelTokenSource.token + cancelToken: this.cancelTokenSource.token, + headers: headers } if (this.accessToken) { - options = Object.assign(options, { + options = objectAssignDeep({}, options, { headers: { Authorization: `Bearer ${this.accessToken}` } @@ -393,12 +397,13 @@ namespace PleromaAPI { * @param path relative path from baseUrl * @param params Form data */ - public async post(path: string, params = {}): Promise> { + public async post(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - cancelToken: this.cancelTokenSource.token + cancelToken: this.cancelTokenSource.token, + headers: headers } if (this.accessToken) { - options = Object.assign(options, { + options = objectAssignDeep({}, options, { headers: { Authorization: `Bearer ${this.accessToken}` } @@ -426,13 +431,14 @@ namespace PleromaAPI { * @param path relative path from baseUrl * @param params Form data */ - public async del(path: string, params = {}): Promise> { + public async del(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { cancelToken: this.cancelTokenSource.token, - data: params + data: params, + headers: headers } if (this.accessToken) { - options = Object.assign(options, { + options = objectAssignDeep({}, options, { headers: { Authorization: `Bearer ${this.accessToken}` } diff --git a/yarn.lock b/yarn.lock index 831619e..730a672 100644 --- a/yarn.lock +++ b/yarn.lock @@ -703,6 +703,11 @@ dependencies: "@types/node" "*" +"@types/object-assign-deep@^0.4.0": + version "0.4.0" + resolved "https://registry.yarnpkg.com/@types/object-assign-deep/-/object-assign-deep-0.4.0.tgz#23c66300f2d04676b927ee700308d0d1b5f8df89" + integrity sha512-3D0F3rHRNDc8cQSXNzwF1jBrJi28Mdrhc10ZLlqbJWDPYRWTTWB9Tc8JoKrgBvLKioXoPoHT6Uzf3s2F7akCUg== + "@types/prettier@^2.0.0": version "2.1.6" resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.1.6.tgz#f4b1efa784e8db479cdb8b14403e2144b1e9ff03" @@ -3473,6 +3478,11 @@ oauth@^0.9.15: resolved "https://registry.yarnpkg.com/oauth/-/oauth-0.9.15.tgz#bd1fefaf686c96b75475aed5196412ff60cfb9c1" integrity sha1-vR/vr2hslrdUda7VGWQS/2DPucE= +object-assign-deep@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/object-assign-deep/-/object-assign-deep-0.4.0.tgz#43505d3679abb9686ab359b97ac14cc837a9d143" + integrity sha512-54Uvn3s+4A/cMWx9tlRez1qtc7pN7pbQ+Yi7mjLjcBpWLlP+XbSHiHbQW6CElDiV4OvuzqnMrBdkgxI1mT8V/Q== + object-copy@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/object-copy/-/object-copy-0.1.0.tgz#7e7d858b781bd7c991a41ba975ed3812754e998c"