From c9bfdbe23a120b8a5c574307a6fa8c7a3c50bb36 Mon Sep 17 00:00:00 2001 From: neet Date: Sun, 20 Jan 2019 13:03:41 +0900 Subject: [PATCH 1/2] Add create user API --- src/client/Mastodon.ts | 12 +++++++++++- src/client/options.ts | 14 ++++++++++++++ src/entities/AccessToken.ts | 3 +++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/entities/AccessToken.ts diff --git a/src/client/Mastodon.ts b/src/client/Mastodon.ts index 81c2de7c7..623d8a6fe 100644 --- a/src/client/Mastodon.ts +++ b/src/client/Mastodon.ts @@ -3,6 +3,7 @@ import { EventHandler } from './EventHandler'; import { getNextUrl } from './linkHeader'; import * as Options from './options'; +import { AccessToken } from '../entities/AccessToken'; import { Account, AccountCredentials } from '../entities/Account'; import { Application, OAuth } from '../entities/Application'; import { Attachment } from '../entities/Attachment'; @@ -123,7 +124,7 @@ export class Mastodon extends Gateway { * @see https://docs.joinmastodon.org/api/authentication/ */ public async fetchAccessToken (code: string, client_id: string, client_secret: string, redirect_uri: string, grant_type = 'authorization_code') { - return (await this.post<{ access_token: string }>(`${this.url}/oauth/token`, { code, client_id, client_secret, redirect_uri, grant_type })).data; + return (await this.post(`${this.url}/oauth/token`, { code, client_id, client_secret, redirect_uri, grant_type })).data; } /** @@ -136,6 +137,15 @@ export class Mastodon extends Gateway { return (await this.get(`${this.url}/api/v1/accounts/${id}`)).data; } + /** + * Create an account + * @param options Data of the user to create + * @return Access token + */ + public async createAccount (options: Options.CreateAccount) { + return (await this.post(`${this.url}/api/v1/accounts`, options)); + } + /** * User’s own account. * @return Returns Account with an extra source attribute. diff --git a/src/client/options.ts b/src/client/options.ts index 9dcaba800..42329c8d2 100644 --- a/src/client/options.ts +++ b/src/client/options.ts @@ -152,3 +152,17 @@ export interface AddPushSubscription { } export type UpdatePushSubscription = Pick; + +export interface CreateAccount { + /** Username to create */ + username: string; + + /** Password of the user */ + password: string; + + /** Email of the user */ + email: string; + + /** Whether the user has been agreed to the agreement of the Mastodon instance */ + agreement: boolean; +} diff --git a/src/entities/AccessToken.ts b/src/entities/AccessToken.ts new file mode 100644 index 000000000..efd6bd549 --- /dev/null +++ b/src/entities/AccessToken.ts @@ -0,0 +1,3 @@ +export interface AccessToken { + access_token: string; +} From 28fa538b3dd4a9099e9944abdde39637c1db1fb8 Mon Sep 17 00:00:00 2001 From: neet Date: Mon, 21 Jan 2019 17:05:33 +0900 Subject: [PATCH 2/2] Rename Token entity based on tootsuite's document --- src/client/Mastodon.ts | 7 +++---- src/entities/AccessToken.ts | 3 --- src/entities/Account.ts | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) delete mode 100644 src/entities/AccessToken.ts diff --git a/src/client/Mastodon.ts b/src/client/Mastodon.ts index 623d8a6fe..f62c8a910 100644 --- a/src/client/Mastodon.ts +++ b/src/client/Mastodon.ts @@ -3,8 +3,7 @@ import { EventHandler } from './EventHandler'; import { getNextUrl } from './linkHeader'; import * as Options from './options'; -import { AccessToken } from '../entities/AccessToken'; -import { Account, AccountCredentials } from '../entities/Account'; +import { Account, AccountCredentials, AccountToken } from '../entities/Account'; import { Application, OAuth } from '../entities/Application'; import { Attachment } from '../entities/Attachment'; import { Card } from '../entities/Card'; @@ -124,7 +123,7 @@ export class Mastodon extends Gateway { * @see https://docs.joinmastodon.org/api/authentication/ */ public async fetchAccessToken (code: string, client_id: string, client_secret: string, redirect_uri: string, grant_type = 'authorization_code') { - return (await this.post(`${this.url}/oauth/token`, { code, client_id, client_secret, redirect_uri, grant_type })).data; + return (await this.post(`${this.url}/oauth/token`, { code, client_id, client_secret, redirect_uri, grant_type })).data; } /** @@ -143,7 +142,7 @@ export class Mastodon extends Gateway { * @return Access token */ public async createAccount (options: Options.CreateAccount) { - return (await this.post(`${this.url}/api/v1/accounts`, options)); + return (await this.post(`${this.url}/api/v1/accounts`, options)); } /** diff --git a/src/entities/AccessToken.ts b/src/entities/AccessToken.ts deleted file mode 100644 index efd6bd549..000000000 --- a/src/entities/AccessToken.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface AccessToken { - access_token: string; -} diff --git a/src/entities/Account.ts b/src/entities/Account.ts index 64fe77f0f..bfcee277e 100644 --- a/src/entities/Account.ts +++ b/src/entities/Account.ts @@ -69,10 +69,10 @@ export interface AccountField { } export interface AccountCredentials extends Account { - source: AccountCredentialsSource; + source: AccountSource; } -export interface AccountCredentialsSource { +export interface AccountSource { /** Selected preference: Default privacy of new toots */ privacy?: StatusVisibility | null; @@ -89,3 +89,16 @@ export interface AccountCredentialsSource { fields: AccountField; } +export interface AccountToken { + /** Access token of the account */ + access_token: string; + + /** Type of the token */ + token_type: string; + + /** Scope of the token */ + scope: string; + + /** Created date of the token */ + created_at: string; +}