-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from WordPress/model
Define model and refactor ApiClient
- Loading branch information
Showing
12 changed files
with
256 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* eslint-disable camelcase */ | ||
import { WP_REST_API_Post } from 'wp-types'; | ||
type ApiPost = WP_REST_API_Post; | ||
/* eslint-enable camelcase */ | ||
|
||
import { Post, PostContent, PostDate, PostTitle } from '@/model/Post'; | ||
import { ApiClient } from '@/api/ApiClient'; | ||
|
||
interface CreateBody { | ||
guid: string; | ||
} | ||
|
||
interface UpdateBody { | ||
date?: PostDate; | ||
title?: PostTitle; | ||
content?: PostContent; | ||
} | ||
|
||
interface PostMeta { | ||
guid: string; | ||
raw_title: string; | ||
raw_date: string; | ||
raw_content: string; | ||
} | ||
|
||
export class PostsApi { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor( private readonly client: ApiClient ) {} | ||
|
||
async create( body: CreateBody ): Promise< Post > { | ||
const response = ( await this.client.post( '/liberated_posts', { | ||
meta: { | ||
guid: body.guid, | ||
}, | ||
} ) ) as ApiPost; | ||
return makePostFromApiResponse( response ); | ||
} | ||
|
||
async update( id: number, body: UpdateBody ): Promise< Post > { | ||
const actualBody: any = {}; | ||
if ( body.date ) { | ||
actualBody.date = body.date.parsed; | ||
} | ||
if ( body.title ) { | ||
actualBody.title = body.title.parsed; | ||
} | ||
if ( body.content ) { | ||
actualBody.content = body.content.parsed; | ||
actualBody.meta = { | ||
raw_content: body.content.original, | ||
}; | ||
} | ||
if ( Object.keys( actualBody ).length === 0 ) { | ||
throw Error( 'attempting to update zero fields' ); | ||
} | ||
const response = ( await this.client.post( | ||
`/liberated_posts/${ id }`, | ||
actualBody | ||
) ) as ApiPost; | ||
return makePostFromApiResponse( response ); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async getByGuid( guid: string ): Promise< Post | null > { | ||
return null; | ||
} | ||
} | ||
|
||
function makePostFromApiResponse( response: ApiPost ): Post { | ||
const meta = response.meta as unknown as PostMeta; | ||
const date = new PostDate( response.date_gmt, meta.raw_date ); | ||
const title = new PostTitle( response.title.raw ?? '', meta.raw_title ); | ||
const content = new PostContent( | ||
response.content.raw ?? '', | ||
meta.raw_content | ||
); | ||
|
||
return { | ||
guid: meta.guid, | ||
id: response.id, | ||
url: response.link, | ||
date, | ||
content, | ||
title, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,39 @@ | ||
/* eslint-disable camelcase */ | ||
|
||
import { WP_REST_API_Settings } from 'wp-types'; | ||
type ApiSettings = WP_REST_API_Settings; | ||
/* eslint-enable camelcase */ | ||
|
||
export interface Settings extends WP_REST_API_Settings {} | ||
import { ApiClient } from '@/api/ApiClient'; | ||
import { SiteSettings } from '@/model/SiteSettings'; | ||
|
||
/* eslint-enable camelcase */ | ||
interface UpdateBody { | ||
title?: string; | ||
} | ||
|
||
export class SettingsApi { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor( private readonly client: ApiClient ) {} | ||
|
||
async update( body: UpdateBody ): Promise< SiteSettings > { | ||
const actualBody: any = {}; | ||
if ( body.title ) { | ||
actualBody.title = body.title; | ||
} | ||
if ( Object.keys( actualBody ).length === 0 ) { | ||
throw Error( 'attempting to update zero fields' ); | ||
} | ||
const response = ( await this.client.post( | ||
`/settings`, | ||
actualBody | ||
) ) as ApiSettings; | ||
return makeSiteSettingsFromApiResponse( response ); | ||
} | ||
} | ||
|
||
function makeSiteSettingsFromApiResponse( | ||
response: ApiSettings | ||
): SiteSettings { | ||
return { | ||
title: response.title, | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* eslint-disable camelcase */ | ||
import { WP_REST_API_User } from 'wp-types'; | ||
type ApiUser = WP_REST_API_User; | ||
/* eslint-enable camelcase */ | ||
|
||
import { ApiClient } from '@/api/ApiClient'; | ||
import { User } from '@/model/User'; | ||
|
||
interface CreateBody { | ||
username: string; | ||
email: string; | ||
password: string; | ||
role?: string; // default roles: administrator, editor, author, subscriber (default) | ||
firstName?: string; | ||
lastName?: string; | ||
} | ||
|
||
export class UsersApi { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor( private readonly client: ApiClient ) {} | ||
|
||
async create( body: CreateBody ): 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; | ||
} | ||
const response = ( await this.client.post( | ||
`/users`, | ||
actualBody | ||
) ) as ApiUser; | ||
return makeUserFromApiResponse( response ); | ||
} | ||
} | ||
|
||
function makeUserFromApiResponse( response: ApiUser ): User { | ||
return { | ||
username: response.username ?? '', | ||
email: response.email ?? '', | ||
role: response.roles ? response.roles[ 0 ] : '', | ||
firstName: response.first_name ?? '', | ||
lastName: response.last_name ?? '', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export interface Post { | ||
id: number; | ||
guid: string; | ||
url: string; | ||
date: PostDate; | ||
title: PostTitle; | ||
content: PostContent; | ||
} | ||
|
||
abstract class PostSection< T > { | ||
original: string; | ||
parsed: string; | ||
constructor( original: string, parsed: string ) { | ||
this.original = original; | ||
this.parsed = parsed; | ||
} | ||
abstract value(): T; | ||
} | ||
|
||
export class PostDate extends PostSection< Date > { | ||
value(): Date { | ||
return new Date( this.parsed ); | ||
} | ||
} | ||
|
||
export class PostTitle extends PostSection< string > { | ||
value(): string { | ||
return this.parsed; | ||
} | ||
} | ||
|
||
export class PostContent extends PostSection< string > { | ||
value(): string { | ||
return this.parsed; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface SiteSettings { | ||
title: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface User { | ||
username: string; | ||
email: string; | ||
role: string; | ||
firstName: string; | ||
lastName: string; | ||
} |
Oops, something went wrong.