This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
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.
feat(blueprints): add a basic HTTP client factory blueprint
closes #27
- Loading branch information
1 parent
ce760e1
commit 06ef473
Showing
7 changed files
with
126 additions
and
23 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 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,30 @@ | ||
import { context, makeActionClass } from '@/core'; | ||
import { HttpAdapter } from '@/http'; | ||
import { makeActionFactoryMockable } from '@/test'; | ||
|
||
/** | ||
* Create the dependencies and action factory to interact with any | ||
* HTTP backend using a simple client. | ||
* | ||
* @param config | ||
*/ | ||
export default function makeHttpClient< | ||
Extension extends {} = {}, | ||
>(config: { | ||
baseURL?: string; | ||
extensions?: Extension; | ||
} = {}) { | ||
const adapter = new HttpAdapter({ | ||
baseURL: config.baseURL ?? '/api/v1', | ||
}); | ||
|
||
const Action = makeActionClass(config.extensions); | ||
const withDependencies = context({ adapter }); | ||
|
||
return { | ||
adapter, | ||
withDependencies, | ||
Action, | ||
action: makeActionFactoryMockable(() => new Action().use(withDependencies)), | ||
}; | ||
} |
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,64 @@ | ||
--- | ||
sidebar_position: 10 | ||
description: Using Foscia as a simple HTTP client. | ||
--- | ||
|
||
import Link from '@docusaurus/Link'; | ||
|
||
# HTTP client | ||
|
||
:::tip What you'll learn | ||
|
||
- Configuring your action factory to only make HTTP request | ||
- Using Foscia as an HTTP client | ||
|
||
::: | ||
|
||
## Creating the action factory | ||
|
||
**If you are using a REST or JSON:API blueprints, you can already use your | ||
action as an HTTP client.** | ||
|
||
You can use Foscia as a simple HTTP client (just like Axios). | ||
For this, you can configure an HTTP client action factory. | ||
|
||
|
||
```javascript title="action.js" | ||
import { makeHttpClient } from 'foscia/blueprints'; | ||
|
||
const { action } = makeHttpClient({ | ||
baseURL: 'https://example.com', | ||
}); | ||
|
||
export default action; | ||
``` | ||
|
||
## Using the HTTP client features | ||
|
||
Once your action factory is ready, sending HTTP request is pretty easy: | ||
|
||
```javascript | ||
import { makeGet, makePost } from 'foscia/http'; | ||
import { action } from './action'; | ||
|
||
// GET https://example.com/ | ||
const response = await action() | ||
.use(makeGet('/')) | ||
.run(raw()); | ||
|
||
// GET https://example.com/api/posts (and get JSON payload) | ||
const data = await action() | ||
.use(makePost('/api/posts', { | ||
data: { title: 'Hello World!' }, | ||
})) | ||
.run(raw((response) => response.json())); | ||
``` | ||
|
||
<Link className="button bg--primary" to="/docs/reference/actions-enhancers#http"> | ||
Read the HTTP' enhancers API guide | ||
</Link> | ||
|
||
## Implementation notes | ||
|
||
You may learn more about the HTTP adapter capabilities and implementation | ||
on [the HTTP implementation guide](/docs/guides/implementations/http). |
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