Repository: neet/masto.js
Pull request #244 (2020-04-05 14:30:42)

Use v2 media API


Patch:
diff --git a/src/clients/masto/masto.ts b/src/clients/masto/masto.ts
index be9d0284..09f1ae8f 100644
--- a/src/clients/masto/masto.ts
+++ b/src/clients/masto/masto.ts
@@ -1,3 +1,5 @@
+import semver from 'semver';
+
 import {
   Account,
   AccountCredentials,
@@ -759,7 +761,8 @@ export class Masto extends GatewayImpl {
    */
   @available({ since: '0.0.0' })
   createMediaAttachment(params: CreateMediaAttachmentParams) {
-    return this.post<Attachment>('/api/v1/media', params, {
+    const v = semver.gt(this.version, '3.1.3') ? 'v2' : 'v1';
+    return this.post<Attachment>(`/api/${v}/media`, params, {
       headers: { 'Content-Type': 'multipart/form-data' },
     });
   }
@@ -1013,11 +1016,12 @@ export class Masto extends GatewayImpl {
    * Search results
    * @param params Parameters
    * @return Results
-   * @see https://docs.joinmastodon.org/api/rest/search/#get-api-v2-search
+   * @see https://docs.joinmastodon.org/methods/search/
    */
-  @available({ since: '2.4.1' })
+  @available({ since: '0.0.0' })
   search(params: SearchParams) {
-    return this.paginate<Results, typeof params>('/api/v2/search', params);
+    const v = semver.gt(this.version, '2.4.1') ? 'v2' : 'v1';
+    return this.paginate<Results, typeof params>(`/api/${v}/search`, params);
   }
 
   /**
diff --git a/src/gateway/gateway-impl.ts b/src/gateway/gateway-impl.ts
index 1ea94ebf..1eb3a2e8 100644
--- a/src/gateway/gateway-impl.ts
+++ b/src/gateway/gateway-impl.ts
@@ -37,7 +37,7 @@ export class GatewayImpl implements Gateway<AxiosRequestConfig> {
   /** Streaming API URL of the instance */
   private _streamingApiUrl = '';
   /** Version of the current instance */
-  version?: string;
+  version: string;
   /** API token of the user */
   accessToken?: string;
 
@@ -46,6 +46,7 @@ export class GatewayImpl implements Gateway<AxiosRequestConfig> {
    */
   constructor(params: GatewayConstructorParams<AxiosRequestConfig>) {
     this.uri = params.uri;
+    this.version = params.version;
 
     if (params.accessToken) {
       this.accessToken = params.accessToken;
@@ -55,10 +56,6 @@ export class GatewayImpl implements Gateway<AxiosRequestConfig> {
       this.streamingApiUrl = params.streamingApiUrl;
     }
 
-    if (params.version) {
-      this.version = params.version;
-    }
-
     this.axios = axios.create({
       baseURL: this.uri,
       transformResponse: this.transformResponse,
@@ -93,8 +90,9 @@ export class GatewayImpl implements Gateway<AxiosRequestConfig> {
    */
   static async login<T extends typeof GatewayImpl>(
     this: T,
-    params: LoginParams,
+    _params: LoginParams,
   ) {
+    const params = { ..._params, version: '0.0.0' };
     const gateway = new this(params) as InstanceType<T>;
     const instance = await gateway.get<Instance>('/api/v1/instance');
 
diff --git a/src/gateway/gateway.ts b/src/gateway/gateway.ts
index 9abdad0b..a3b6a880 100644
--- a/src/gateway/gateway.ts
+++ b/src/gateway/gateway.ts
@@ -41,7 +41,7 @@ export interface GatewayConstructorParams<T = undefined> {
   /** Streaming API URL */
   streamingApiUrl?: string;
   /** Version of the instance */
-  version?: string;
+  version: string;
   /** Access token of the user */
   accessToken?: string;
   /** Axios configurations. See [Axios'](https://github.com/axios/axios#request-config) docs */


Test patch:
diff --git a/src/clients/masto/__tests__/__snapshots__/masto.spec.ts.snap b/src/clients/masto/__tests__/__snapshots__/masto.spec.ts.snap
index 6d1a270b..cc9921d5 100644
--- a/src/clients/masto/__tests__/__snapshots__/masto.spec.ts.snap
+++ b/src/clients/masto/__tests__/__snapshots__/masto.spec.ts.snap
@@ -257,6 +257,56 @@ exports[`Masto createMarkers 1`] = `
 }
 `;
 
+exports[`Masto createMediaAttachment 1`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [
+      "/api/v2/media",
+      Object {
+        "description": "Nice image",
+        "file": "...",
+      },
+      Object {
+        "headers": Object {
+          "Content-Type": "multipart/form-data",
+        },
+      },
+    ],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": undefined,
+    },
+  ],
+}
+`;
+
+exports[`Masto createMediaAttachment v1 1`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [
+      "/api/v1/media",
+      Object {
+        "description": "Nice image",
+        "file": "...",
+      },
+      Object {
+        "headers": Object {
+          "Content-Type": "multipart/form-data",
+        },
+      },
+    ],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": undefined,
+    },
+  ],
+}
+`;
+
 exports[`Masto createPushSubscription 1`] = `
 [MockFunction] {
   "calls": Array [
@@ -1744,6 +1794,25 @@ exports[`Masto search 1`] = `
 }
 `;
 
+exports[`Masto search v1 1`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [
+      "/api/v1/search",
+      Object {
+        "q": "query",
+      },
+    ],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": undefined,
+    },
+  ],
+}
+`;
+
 exports[`Masto searchAccounts 1`] = `
 [MockFunction] {
   "calls": Array [
diff --git a/src/clients/masto/__tests__/masto.spec.ts b/src/clients/masto/__tests__/masto.spec.ts
index 5232e915..b19ac083 100644
--- a/src/clients/masto/__tests__/masto.spec.ts
+++ b/src/clients/masto/__tests__/masto.spec.ts
@@ -495,13 +495,21 @@ describe('Masto', () => {
       description: 'Nice image',
     });
     expect(mockPost).toBeCalledTimes(1);
-    expect(mockPost).toBeCalledWith(
-      expect.any(String),
-      expect.anything(),
-      expect.any(Object),
-    );
+    expect(mockPost).toMatchSnapshot();
+    expect(mockPost).toBeCalledWith('/api/v2/media', expect.anything(), expect.anything());
   });
 
+  test('createMediaAttachment v1', async () => {
+    masto.version = '0.0.0';
+    await masto.createMediaAttachment({
+      file: '...',
+      description: 'Nice image',
+    });
+    expect(mockPost).toBeCalledTimes(1);
+    expect(mockPost).toMatchSnapshot();
+    expect(mockPost).toBeCalledWith('/api/v1/media', expect.anything(), expect.anything());
+  })
+
   test('updateMediaAttachment', async () => {
     await masto.updateMediaAttachment('123123', {
       file: '',
@@ -684,6 +692,17 @@ describe('Masto', () => {
     });
     expect(mockPaginate).toBeCalledTimes(1);
     expect(mockPaginate).toMatchSnapshot();
+    expect(mockPaginate).toBeCalledWith('/api/v2/search', expect.anything());
+  });
+
+  test('search v1', async () => {
+    masto.version = '0.0.0';
+    masto.search({
+      q: 'query',
+    });
+    expect(mockPaginate).toBeCalledTimes(1);
+    expect(mockPaginate).toMatchSnapshot();
+    expect(mockPaginate).toBeCalledWith('/api/v1/search', expect.anything());
   });
 
   test('fetchStatus', async () => {
diff --git a/src/gateway/__tests__/gateway-impl.spec.ts b/src/gateway/__tests__/gateway-impl.spec.ts
index d12908b5..425fd6f0 100644
--- a/src/gateway/__tests__/gateway-impl.spec.ts
+++ b/src/gateway/__tests__/gateway-impl.spec.ts
@@ -62,6 +62,7 @@ describe('GatewayImpl', () => {
     const customGateway = new GatewayImpl({
       uri: 'https://example.com',
       streamingApiUrl: 'wss://example.com',
+      version: '0.0.0',
     });
     expect(customGateway.streamingApiUrl).toBe('wss://example.com');
   });
@@ -78,6 +79,7 @@ describe('GatewayImpl', () => {
     const customGateway = new GatewayImpl({
       uri: 'https://example.com',
       accessToken: 'token token',
+      version: '0.0.0',
     });
     expect(customGateway.accessToken).toBe('token token');
   });
@@ -85,6 +87,7 @@ describe('GatewayImpl', () => {
   test('this._uri accessor works', () => {
     const customGateway = new GatewayImpl({
       uri: 'https://example.com/aaa',
+      version: '0.0.0',
     });
     customGateway.uri = 'https://example.com/bbb';
     expect(customGateway.uri).toEqual('https://example.com/bbb');
@@ -93,6 +96,7 @@ describe('GatewayImpl', () => {
   test('this._streamingApiUrl accessor works', () => {
     const customGateway = new GatewayImpl({
       uri: 'wss://example.com/aaa',
+      version: '0.0.0',
     });
     customGateway.streamingApiUrl = 'wss://example.com/bbb';
     expect(customGateway.streamingApiUrl).toEqual('wss://example.com/bbb');