-
Notifications
You must be signed in to change notification settings - Fork 0
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 #25 from akirk/api-client
API Client
- Loading branch information
Showing
8 changed files
with
146 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,49 @@ | ||
/* eslint-disable camelcase */ | ||
|
||
import { PlaygroundClient } from '@wp-playground/client'; | ||
import { WP_REST_API_Post } from 'wp-types'; | ||
|
||
export interface Post { | ||
id: number; | ||
title: string; | ||
} | ||
|
||
export class ApiClient { | ||
private readonly playgroundClient: PlaygroundClient; | ||
private readonly _siteUrl: string; | ||
|
||
constructor( playgroundClient: PlaygroundClient, siteUrl: string ) { | ||
this.playgroundClient = playgroundClient; | ||
this._siteUrl = siteUrl; | ||
} | ||
|
||
get siteUrl(): string { | ||
return this._siteUrl; | ||
} | ||
|
||
async getPosts(): Promise< Post[] > { | ||
const response = ( await this.get( | ||
'/wp/v2/posts' | ||
) ) as WP_REST_API_Post[]; | ||
|
||
return response.map( ( post ) => { | ||
return { | ||
id: post.id, | ||
title: post.title.rendered, | ||
}; | ||
} ); | ||
} | ||
|
||
private async get( route: string ): Promise< object > { | ||
const response = await this.playgroundClient.request( { | ||
url: `/index.php?rest_route=${ route }`, | ||
method: 'GET', | ||
} ); | ||
if ( response.httpStatusCode !== 200 ) { | ||
throw Error( response.json.message ); | ||
} | ||
return response.json; | ||
} | ||
} | ||
|
||
/* eslint-enable camelcase */ |
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 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 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 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,22 @@ | ||
import { createContext, useContext } from 'react'; | ||
import { Session } from '@/storage/session'; | ||
import { ApiClient } from '@/api/ApiClient'; | ||
|
||
export interface SessionContext { | ||
session: Session; | ||
apiClient?: ApiClient; | ||
} | ||
|
||
const sessionContext = createContext< SessionContext >( { | ||
session: { | ||
id: '', | ||
url: '', | ||
title: '', | ||
}, | ||
} ); | ||
|
||
export const SessionProvider = sessionContext.Provider; | ||
|
||
export function useSessionContext() { | ||
return useContext( sessionContext ); | ||
} |
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,11 +1,32 @@ | ||
import { useOutletContext } from 'react-router-dom'; | ||
import { Session } from '@/storage/session'; | ||
import { useSessionContext } from '@/ui/session/SessionProvider'; | ||
import { Post } from '@/api/ApiClient'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
export function ViewSession() { | ||
const session = useOutletContext() as Session; | ||
const { session, apiClient } = useSessionContext(); | ||
|
||
const [ posts, setPosts ] = useState< Post[] >( [] ); | ||
useEffect( () => { | ||
if ( ! apiClient ) { | ||
return; | ||
} | ||
const getPosts = async () => { | ||
setPosts( await apiClient.getPosts() ); | ||
}; | ||
void getPosts(); | ||
}, [ apiClient?.siteUrl ] ); | ||
|
||
return ( | ||
<> | ||
<span>{ `view session: ${ session.id }` }</span> | ||
<div>view session: { session.id }</div> | ||
{ apiClient?.siteUrl ? ( | ||
<div>url: { apiClient.siteUrl }</div> | ||
) : null } | ||
<ul> | ||
{ posts.map( ( post ) => { | ||
return <li key={ post.id }>{ post.title }</li>; | ||
} ) } | ||
</ul> | ||
</> | ||
); | ||
} |