rest-client-vue 1.0.0-a1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ import { Method } from 'axios';
2
+ import { Ref } from 'vue';
3
+ import { Resource, ResourceId, ResourceType } from './rest-collection-resources.js';
4
+ import { RestResourceStatus } from './stores/rest-resource-store.js';
5
+ /** Options that define a REST resource client. */
6
+ export interface RestResourceOptions {
7
+ /**
8
+ * The REST resource client's unique identifier, which will be used as the ID of its Pinia store.
9
+ *
10
+ * This is normally generated, and the generated ID may be accessed using the resource client's
11
+ * {@link resourceClientId} property.
12
+ *
13
+ * There are two circumstances where you may want to specify a resource client ID:
14
+ * 1. You have already created a resource client and want to give another Vue component access to it. In this case,
15
+ * pass the value of the collection's {@link resourceClientId} property in the options to the second component's
16
+ * {@link useRestResource} call.
17
+ * 2. You want to assign a canonical ID to the client, so that components can call {@link useRestResrouce} to create
18
+ * it or to access it if it already exists. In this case, you can assign the ID manually, but you must ensure that
19
+ * it does not conflict with any other Pinia store IDs used in the Vue application.
20
+ *
21
+ * - If not specified, an unique ID will be generated.
22
+ * - If specified, the REST resource client will check whether a Pinia store with this ID already exists.
23
+ * - If one exists, that store will be used, and other options will be ignored. This is a way to have multiple
24
+ * components' {@link RestResource}s share a single data store.
25
+ * - If a Pinia store does not exist, then when creating a new resource client, the ID will be used instead of
26
+ * generating a new unique ID.
27
+ */
28
+ resourceClientId?: string;
29
+ draftBatchId?: string;
30
+ enabled?: boolean;
31
+ /** The resource type. */
32
+ resourceType?: Ref<ResourceType | undefined> | ResourceType;
33
+ /** Options that govern the behavior of the REST resource's data store module. */
34
+ options?: RestResourceStoreOptionsInput;
35
+ }
36
+ export interface ConcreteRestResourceStoreOptionsInput {
37
+ referencePathsToExpand?: string[];
38
+ resourceId?: ResourceId;
39
+ resourceUrl?: string;
40
+ }
41
+ export interface RestResourceStoreOptionsInput {
42
+ referencePathsToExpand?: string[] | ((resourceType: ResourceType) => string[]);
43
+ resourceId?: ResourceId;
44
+ resourceUrl?: string;
45
+ }
46
+ export interface MakeCustomApiRequestParams {
47
+ method?: Method;
48
+ subpath?: string;
49
+ url?: string;
50
+ data?: any;
51
+ }
52
+ /**
53
+ * A REST resource client.
54
+ *
55
+ * This represents one resource fetched from a REST API. More commonly, clients will use a {@link RestCollection}, but
56
+ * {@link RestResourceClient} is sometimes useful for consuming individual responses from APIs that are not really
57
+ * RESTful.
58
+ */
59
+ export interface RestResourceClient {
60
+ /**
61
+ * The REST resource client's unique identifier, which is the ID of its Pinia store.
62
+ *
63
+ * If you want to share the store with another component, you can pass this ID when creating the second component's
64
+ * {@link RestResourceClient}.
65
+ */
66
+ resourceClientId: Ref<string | null>;
67
+ /** The draft batch ID, if one was passed in {@link RestCollectionOptions.draftBatchId}. */
68
+ draftBatchId: Ref<string | undefined>;
69
+ /** A flag indicating whether accessing the local collection will fetch data from the remote collection. */
70
+ enabled: Ref<boolean>;
71
+ /**
72
+ * The resource type for this collection.
73
+ *
74
+ * To allow for asynchronous loading of resource types, this may be undefined.
75
+ */
76
+ resourceType: Ref<ResourceType | undefined>;
77
+ /** A status indicating whether resources have been loaded yet from the REST API. */
78
+ status: Ref<RestResourceStatus>;
79
+ /** The local version of the resource loaded from the REST API. */
80
+ resource: Ref<Resource>;
81
+ /** The ID of the REST resource. */
82
+ resourceId: Ref<ResourceId | null>;
83
+ /** The URL of the REST resource. */
84
+ resourceUrl: Ref<string | null>;
85
+ /**
86
+ * Apply new configuration options.
87
+ *
88
+ * The result is much like creating a new RestCollection, except that the client can continue using the same
89
+ * properties.
90
+ *
91
+ * @param options New options to apply.
92
+ */
93
+ reconfigureClient: (options: RestResourceOptions) => void;
94
+ setEnabled: (enabled: boolean) => void;
95
+ setResourceId: (resourceId: ResourceId) => Promise<void>;
96
+ setResourceUrl: (resourceUrl: string) => Promise<void>;
97
+ /**
98
+ * Ensure that a Pinia store has been created for this REST resource.
99
+ *
100
+ * This does not normally need to be called, because it will happen automatically when the {@link RestResourceClient}
101
+ * is created and when {@link resourceClientId}, {@link resourceType}, or options change.
102
+ *
103
+ * @return true if the store has been created or replaced. (Note that false does not indicate that the store does not
104
+ * exist.)
105
+ */
106
+ ensureStore: () => boolean;
107
+ /** Return the {@link RestResourceClient} to the NotLoaded state, where no resource has been fetched yet. */
108
+ clear: () => void;
109
+ /**
110
+ * Fetch the resource from the REST API, if this has not happened yet.
111
+ *
112
+ * A client that observes changes to the reactive property {@link resource} should never need to call this.
113
+ *
114
+ * @return A promise that returns when the resource has loaded or loading has failed.
115
+ */
116
+ ensureResourceLoaded: () => Promise<void>;
117
+ /**
118
+ * Load the resource from the REST API.
119
+ *
120
+ * A client that observes changes to the reactive property {@link resource} should never need to call this.
121
+ *
122
+ * @return A promise that returns when the resource has loaded or loading has failed.
123
+ */
124
+ loadResource: () => Promise<void>;
125
+ /**
126
+ * Start editing a new resource, whose properties are all undefined except for any passed in the parameter.
127
+ *
128
+ * @param resourceDefaults An object containing initial property values for the new resource.
129
+ */
130
+ checkForDeletedResource: () => Promise<boolean>;
131
+ recordDeletion: () => void;
132
+ recordInsertion: (resource: Resource) => void;
133
+ recordUpdate: (resource: Resource) => void;
134
+ refreshResource: () => Promise<Resource | null | undefined>;
135
+ deleteResource: () => Promise<void>;
136
+ saveResource: (resource: Resource) => Promise<Resource | null | undefined>;
137
+ makeCustomApiRequest: (params: MakeCustomApiRequestParams) => Promise<any>;
138
+ }
139
+ declare const _default: (params?: RestResourceOptions) => RestResourceClient;
140
+ export default _default;
@@ -0,0 +1,9 @@
1
+ import { RestCollectionStore } from './stores/rest-collection-store.js';
2
+ import { RestResourceStore } from './stores/rest-resource-store.js';
3
+ export declare class StoreRegistry<T extends RestCollectionStore | RestResourceStore> {
4
+ #private;
5
+ clearAllStores(): void;
6
+ destroyStore(id: string): void;
7
+ getStore(id: string): T;
8
+ registerStore(id: string, store: T): void;
9
+ }