Skip to content

Commit

Permalink
feat: url du serveur d'API en paramètre
Browse files Browse the repository at this point in the history
  • Loading branch information
jillro committed Jun 24, 2024
1 parent 691a413 commit b7fbbfd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ import ResinFormulaire from '@medialab/resin-formulaire';
<!-- mypage.html -->
<script src="mybundle.js"></script>

<resin-formulaire />
<resin-formulaire apiServer="http://locahost:8000" />
```

`apiServer` doit contenir la base d'URL [du serveur
de l'API de Résin](https://github.com/medialab/resin-api),
sous la forme `protocol://domain[:port]`.

Par défaut, le formulaire affiché est le formulaire d'inscription.
Si les paramètres `uid` et `token` sont passés en querystring,
le formulaire affiché est le formulaire de modification de profil.
Expand Down
4 changes: 3 additions & 1 deletion src/FormComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<script lang="ts">
import SubscribeForm from "./components/Form.svelte";
export let apiServer = "http://localhost:8000";
// Get uid and token params
const urlParams = new URLSearchParams(window.location.search);
const uid = urlParams.get("uid");
Expand All @@ -20,7 +22,7 @@
let res;
try {
res = await fetch(`http://localhost:8000/api/members/${uid}/`, {
res = await fetch(`${apiServer}/api/members/${uid}/`, {
headers: {
"Content-Type": "application/json",
...(token ? { Authorization: `Token ${token}` } : {}),
Expand Down
7 changes: 4 additions & 3 deletions src/components/Form.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import Autocomplete from "./Autocomplete.svelte";
export let apiServer = "http://localhost:8000";
export let success = false;
export let initialData: Record<string, any> = {};
export let token: string | null;
Expand All @@ -27,7 +28,7 @@
const getFormFields = async (): Promise<Map<string, FormField>> =>
(
await (
await fetch("http://localhost:8000/api/members/", {
await fetch(`${apiServer}/api/members/`, {
headers: {
"Content-Type": "application/json",
},
Expand All @@ -43,7 +44,7 @@
].map(
(endpoint) => async () =>
await (
await fetch("http://localhost:8000/api/" + endpoint + "/", {
await fetch(`${apiServer}/api/` + endpoint + "/", {
headers: {
"Content-Type": "application/json",
},
Expand Down Expand Up @@ -86,7 +87,7 @@
}
const response = await fetch(
`http://localhost:8000/api/members/${isUpdate ? `${initialData.id}/` : ""}`,
`${apiServer}/api/members/${isUpdate ? `${initialData.id}/` : ""}`,
{
method: isUpdate ? "PATCH" : "POST",
body: formData,
Expand Down

0 comments on commit b7fbbfd

Please sign in to comment.