pocketbase/ui/src/components/collections/docs/UpdateApiDocs.svelte

369 lines
12 KiB
Svelte
Raw Normal View History

2022-07-07 05:19:05 +08:00
<script>
import ApiClient from "@/utils/ApiClient";
import CommonHelper from "@/utils/CommonHelper";
import CodeBlock from "@/components/base/CodeBlock.svelte";
2022-07-31 02:00:18 +08:00
import SdkTabs from "@/components/collections/docs/SdkTabs.svelte";
import FieldsQueryParam from "@/components/collections/docs/FieldsQueryParam.svelte";
2022-07-07 05:19:05 +08:00
2023-08-15 02:20:49 +08:00
export let collection;
2022-07-07 05:19:05 +08:00
let responseTab = 200;
let responses = [];
2022-10-30 16:28:14 +08:00
let baseData = {};
2022-07-07 05:19:05 +08:00
2023-08-15 02:20:49 +08:00
$: isAuth = collection?.type === "auth";
2022-07-07 05:19:05 +08:00
$: adminsOnly = collection?.updateRule === null;
$: backendAbsUrl = CommonHelper.getApiExampleUrl(ApiClient.baseUrl);
2022-07-31 02:00:18 +08:00
2022-07-07 05:19:05 +08:00
$: responses = [
{
code: 200,
body: JSON.stringify(CommonHelper.dummyCollectionRecord(collection), null, 2),
},
{
code: 400,
body: `
{
"code": 400,
"message": "Failed to update record.",
"data": {
"${collection?.schema?.[0]?.name}": {
"code": "validation_required",
"message": "Missing required value."
}
}
}
`,
},
{
code: 403,
body: `
{
"code": 403,
"message": "You are not allowed to perform this request.",
"data": {}
}
`,
},
{
code: 404,
body: `
{
"code": 404,
"message": "The requested resource wasn't found.",
"data": {}
}
`,
},
];
2023-08-15 02:20:49 +08:00
$: if (collection.type === "auth") {
2022-10-30 16:28:14 +08:00
baseData = {
username: "test_username_update",
emailVisibility: false,
password: "87654321",
passwordConfirm: "87654321",
oldPassword: "12345678",
};
} else {
baseData = {};
}
</script>
2022-07-07 05:19:05 +08:00
2022-10-30 16:28:14 +08:00
<h3 class="m-b-sm">Update ({collection.name})</h3>
<div class="content txt-lg m-b-sm">
2022-07-07 05:19:05 +08:00
<p>Update a single <strong>{collection.name}</strong> record.</p>
<p>
Body parameters could be sent as <code>application/json</code> or
<code>multipart/form-data</code>.
</p>
<p>
File upload is supported only via <code>multipart/form-data</code>.
2022-10-30 16:28:14 +08:00
<br />
For more info and examples you could check the detailed
<a href={import.meta.env.PB_FILE_UPLOAD_DOCS} target="_blank" rel="noopener noreferrer">
Files upload and handling docs
</a>.
2022-07-07 05:19:05 +08:00
</p>
{#if isAuth}
<p>
<em>
Note that in case of a password change all previously issued tokens for the current record
will be automatically invalidated and if you want your user to remain signed in you need to
reauthenticate manually after the update call.
</em>
</p>
{/if}
2022-07-07 05:19:05 +08:00
</div>
2022-10-30 16:28:14 +08:00
<!-- prettier-ignore -->
2022-07-31 02:00:18 +08:00
<SdkTabs
js={`
2022-10-30 16:28:14 +08:00
import PocketBase from 'pocketbase';
2022-07-31 02:00:18 +08:00
2022-10-30 16:28:14 +08:00
const pb = new PocketBase('${backendAbsUrl}');
2022-07-31 02:00:18 +08:00
2022-10-30 16:28:14 +08:00
...
2022-07-31 02:00:18 +08:00
2022-10-30 16:28:14 +08:00
// example update data
const data = ${JSON.stringify(Object.assign({}, baseData, CommonHelper.dummyCollectionSchemaData(collection)), null, 4)};
2022-07-31 02:00:18 +08:00
2022-10-30 16:28:14 +08:00
const record = await pb.collection('${collection?.name}').update('RECORD_ID', data);
2022-07-31 02:00:18 +08:00
`}
dart={`
2022-10-30 16:28:14 +08:00
import 'package:pocketbase/pocketbase.dart';
2022-07-31 02:00:18 +08:00
2022-10-30 16:28:14 +08:00
final pb = PocketBase('${backendAbsUrl}');
2022-07-31 02:00:18 +08:00
2022-10-30 16:28:14 +08:00
...
2022-07-31 02:00:18 +08:00
2022-10-30 16:28:14 +08:00
// example update body
final body = <String, dynamic>${JSON.stringify(Object.assign({}, baseData, CommonHelper.dummyCollectionSchemaData(collection)), null, 2)};
2022-07-31 02:00:18 +08:00
2022-10-30 16:28:14 +08:00
final record = await pb.collection('${collection?.name}').update('RECORD_ID', body: body);
2022-07-31 02:00:18 +08:00
`}
/>
2022-07-07 05:19:05 +08:00
2022-10-30 16:28:14 +08:00
<h6 class="m-b-xs">API details</h6>
<div class="alert alert-warning">
<strong class="label label-primary">PATCH</strong>
<div class="content">
<p>
/api/collections/<strong>{collection.name}</strong>/records/<strong>:id</strong>
</p>
</div>
{#if adminsOnly}
<p class="txt-hint txt-sm txt-right">Requires admin <code>Authorization:TOKEN</code> header</p>
{/if}
</div>
2022-07-07 05:19:05 +08:00
<div class="section-title">Path parameters</div>
2022-10-30 16:28:14 +08:00
<table class="table-compact table-border m-b-base">
2022-07-07 05:19:05 +08:00
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th width="60%">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>
<span class="label">String</span>
</td>
<td>ID of the record to update.</td>
</tr>
</tbody>
</table>
<div class="section-title">Body Parameters</div>
2022-10-30 16:28:14 +08:00
<table class="table-compact table-border m-b-base">
2022-07-07 05:19:05 +08:00
<thead>
<tr>
<th>Param</th>
<th>Type</th>
2022-07-07 05:19:05 +08:00
<th width="50%">Description</th>
</tr>
</thead>
<tbody>
2023-08-15 02:20:49 +08:00
{#if isAuth}
2022-10-30 16:28:14 +08:00
<tr>
<td colspan="3" class="txt-hint">Auth fields</td>
</tr>
<tr>
<td>
<div class="inline-flex">
<span class="label label-warning">Optional</span>
<span>username</span>
</div>
</td>
<td>
<span class="label">String</span>
</td>
<td>The username of the auth record.</td>
</tr>
<tr>
<td>
<div class="inline-flex">
<span class="label label-warning">Optional</span>
<span>email</span>
</div>
</td>
<td>
<span class="label">String</span>
</td>
<td>
The auth record email address.
<br />
This field can be updated only by admins or auth records with "Manage" access.
<br />
Regular accounts can update their email by calling "Request email change".
</td>
</tr>
<tr>
<td>
<div class="inline-flex">
<span class="label label-warning">Optional</span>
<span>emailVisibility</span>
</div>
</td>
<td>
<span class="label">Boolean</span>
</td>
<td>Whether to show/hide the auth record email when fetching the record data.</td>
</tr>
<tr>
<td>
<div class="inline-flex">
<span class="label label-warning">Optional</span>
<span>oldPassword</span>
</div>
</td>
<td>
<span class="label">String</span>
</td>
<td>
Old auth record password.
<br />
This field is required only when changing the record password. Admins and auth records with
"Manage" access can skip this field.
</td>
</tr>
<tr>
<td>
<div class="inline-flex">
<span class="label label-warning">Optional</span>
<span>password</span>
</div>
</td>
<td>
<span class="label">String</span>
</td>
<td>New auth record password.</td>
</tr>
<tr>
<td>
<div class="inline-flex">
<span class="label label-warning">Optional</span>
<span>passwordConfirm</span>
</div>
</td>
<td>
<span class="label">String</span>
</td>
<td>New auth record password confirmation.</td>
</tr>
<tr>
<td>
<div class="inline-flex">
<span class="label label-warning">Optional</span>
<span>verified</span>
</div>
</td>
<td>
<span class="label">Boolean</span>
</td>
<td>
Indicates whether the auth record is verified or not.
<br />
This field can be set only by admins or auth records with "Manage" access.
</td>
</tr>
<tr>
<td colspan="3" class="txt-hint">Schema fields</td>
</tr>
{/if}
2022-07-07 05:19:05 +08:00
{#each collection?.schema as field (field.name)}
<tr>
<td>
<div class="inline-flex">
{#if field.required}
<span class="label label-success">Required</span>
{:else}
<span class="label label-warning">Optional</span>
{/if}
<span>{field.name}</span>
</div>
</td>
<td>
<span class="label">{CommonHelper.getFieldValueType(field)}</span>
</td>
<td>
{#if field.type === "text"}
Plain text value.
{:else if field.type === "number"}
Number value.
{:else if field.type === "json"}
JSON array or object.
{:else if field.type === "email"}
Email address.
{:else if field.type === "url"}
URL address.
{:else if field.type === "file"}
File object.<br />
2022-07-07 05:19:05 +08:00
Set to <code>null</code> to delete already uploaded file(s).
{:else if field.type === "relation"}
Relation record {field.options?.maxSelect > 1 ? "ids" : "id"}.
{:else if field.type === "user"}
User {field.options?.maxSelect > 1 ? "ids" : "id"}.
{/if}
</td>
</tr>
{/each}
</tbody>
</table>
<div class="section-title">Query parameters</div>
<table class="table-compact table-border m-b-lg">
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th width="60%">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>expand</td>
<td>
<span class="label">String</span>
</td>
<td>
Auto expand relations when returning the updated record. Ex.:
2022-10-30 16:28:14 +08:00
<CodeBlock content={`?expand=relField1,relField2.subRelField21`} />
Supports up to 6-levels depth nested relations expansion. <br />
The expanded relations will be appended to the record under the
2022-10-30 16:28:14 +08:00
<code>expand</code> property (eg. <code>{`"expand": {"relField1": {...}, ...}`}</code>). Only
the relations that the user has permissions to <strong>view</strong> will be expanded.
</td>
</tr>
<FieldsQueryParam />
</tbody>
</table>
2022-07-07 05:19:05 +08:00
<div class="section-title">Responses</div>
<div class="tabs">
2023-09-01 17:44:43 +08:00
<div class="tabs-header compact combined left">
2022-07-07 05:19:05 +08:00
{#each responses as response (response.code)}
<button
class="tab-item"
class:active={responseTab === response.code}
on:click={() => (responseTab = response.code)}
>
{response.code}
</button>
{/each}
</div>
<div class="tabs-content">
{#each responses as response (response.code)}
<div class="tab-item" class:active={responseTab === response.code}>
<CodeBlock content={response.body} />
</div>
{/each}
</div>
</div>