From ebb4084c589e587b35c30b6ffff5457ccd7cc115 Mon Sep 17 00:00:00 2001 From: ashfame Date: Tue, 24 Sep 2024 20:39:54 +0400 Subject: [PATCH 1/2] add createUser method on ApiClient --- src/api/ApiClient.ts | 28 ++++++++++++++++++++++++++++ src/api/User.ts | 7 +++++++ 2 files changed, 35 insertions(+) create mode 100644 src/api/User.ts diff --git a/src/api/ApiClient.ts b/src/api/ApiClient.ts index f4e401bb..cc8319ca 100644 --- a/src/api/ApiClient.ts +++ b/src/api/ApiClient.ts @@ -3,12 +3,22 @@ import { PlaygroundClient } from '@wp-playground/client'; import { Post } from '@/api/Post'; import { Settings } from '@/api/Settings'; +import { User } from '@/api/User'; import { PostContent, PostDate, PostTitle } from '@/parser/post'; export interface CreatePostBody { guid: string; } +export interface CreateUserBody { + username: string; + email: string; + password: string; + roles?: [ string ]; // default roles: administrator, editor, author, subscriber (default) + firstname?: string; + lastname?: string; +} + export interface UpdatePostBody { date?: PostDate; title?: PostTitle; @@ -67,6 +77,24 @@ export class ApiClient { } ) ) as Settings; } + async createUser( body: CreateUserBody ): Promise< User > { + const actualBody: any = { + username: body.username, + email: body.email, + password: body.password, + }; + if ( body.roles ) { + actualBody.roles = body.roles; + } + if ( body.firstname ) { + actualBody.first_name = body.firstname; + } + if ( body.lastname ) { + actualBody.last_name = body.lastname; + } + return ( await this.post( `/users`, actualBody ) ) as User; + } + private async get( route: string ): Promise< object > { const response = await this.playgroundClient.request( { url: `/index.php?rest_route=/wp/v2${ route }`, diff --git a/src/api/User.ts b/src/api/User.ts new file mode 100644 index 00000000..468c91e3 --- /dev/null +++ b/src/api/User.ts @@ -0,0 +1,7 @@ +/* eslint-disable camelcase */ + +import { WP_REST_API_User } from 'wp-types'; + +export interface User extends WP_REST_API_User {} + +/* eslint-enable camelcase */ From 269ea81022cd2ea124c5b1447fa01881669eb448 Mon Sep 17 00:00:00 2001 From: ashfame Date: Wed, 25 Sep 2024 18:08:39 +0400 Subject: [PATCH 2/2] abstract roles being plural and an array, for our purpose it should just be a string --- src/api/ApiClient.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/ApiClient.ts b/src/api/ApiClient.ts index cc8319ca..c37fe222 100644 --- a/src/api/ApiClient.ts +++ b/src/api/ApiClient.ts @@ -14,7 +14,7 @@ export interface CreateUserBody { username: string; email: string; password: string; - roles?: [ string ]; // default roles: administrator, editor, author, subscriber (default) + role?: string; // default roles: administrator, editor, author, subscriber (default) firstname?: string; lastname?: string; } @@ -83,8 +83,8 @@ export class ApiClient { email: body.email, password: body.password, }; - if ( body.roles ) { - actualBody.roles = body.roles; + if ( body.role ) { + actualBody.roles = [ body.role ]; } if ( body.firstname ) { actualBody.first_name = body.firstname;