Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add createUser method on ApiClient #55

Merged
merged 2 commits into from
Sep 25, 2024
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
28 changes: 28 additions & 0 deletions src/api/ApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
/* eslint-disable react/no-is-mounted */

import { PlaygroundClient } from '@wp-playground/client';

Check warning on line 3 in src/api/ApiClient.ts

View workflow job for this annotation

GitHub Actions / lint

PlaygroundClient not found in '@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;
role?: string; // default roles: administrator, editor, author, subscriber (default)
firstname?: string;
lastname?: string;
}

export interface UpdatePostBody {
date?: PostDate;
title?: PostTitle;
Expand Down Expand Up @@ -67,6 +77,24 @@
} ) ) as Settings;
}

async createUser( body: CreateUserBody ): Promise< User > {
const actualBody: any = {
username: body.username,
email: body.email,
password: body.password,
};
if ( body.role ) {
actualBody.roles = [ body.role ];
}
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 }`,
Expand Down
7 changes: 7 additions & 0 deletions src/api/User.ts
Original file line number Diff line number Diff line change
@@ -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 */
Loading