Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/client/Mastodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EventHandler } from './EventHandler';
import { getNextUrl } from './linkHeader';
import * as Options from './options';

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';
Expand Down Expand Up @@ -123,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<{ access_token: string }>(`${this.url}/oauth/token`, { code, client_id, client_secret, redirect_uri, grant_type })).data;
return (await this.post<AccountToken>(`${this.url}/oauth/token`, { code, client_id, client_secret, redirect_uri, grant_type })).data;
}

/**
Expand All @@ -136,6 +136,15 @@ export class Mastodon extends Gateway {
return (await this.get<Account>(`${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<AccountToken>(`${this.url}/api/v1/accounts`, options));
}

/**
* User’s own account.
* @return Returns Account with an extra source attribute.
Expand Down
14 changes: 14 additions & 0 deletions src/client/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,17 @@ export interface AddPushSubscription {
}

export type UpdatePushSubscription = Pick<AddPushSubscription, 'data'>;

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;
}
17 changes: 15 additions & 2 deletions src/entities/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}