@jwerre/vellum 0.0.1 → 1.0.1
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.
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { Model } from './Model.svelte';
|
|
2
|
+
import { type VellumConfig } from './config.svelte';
|
|
3
|
+
export interface FetchOptions extends Partial<VellumConfig> {
|
|
4
|
+
endpoint?: string;
|
|
5
|
+
search?: Record<string, string | number | boolean>;
|
|
6
|
+
}
|
|
2
7
|
/**
|
|
3
8
|
* Abstract base class for managing collections of Model instances.
|
|
4
9
|
*
|
|
@@ -103,7 +108,8 @@ export declare abstract class Collection<M extends Model<T>, T extends object> {
|
|
|
103
108
|
* Fetches data from the server and populates the collection.
|
|
104
109
|
*
|
|
105
110
|
* @param options - Configuration options for the fetch request
|
|
106
|
-
* @param options.
|
|
111
|
+
* @param [options.endpoint] - Optional endpoint to use if different than this.endpoint()
|
|
112
|
+
* @param [options.search] - Optional search parameters to include in the query string.
|
|
107
113
|
* Keys and values will be converted to strings and URL-encoded.
|
|
108
114
|
*
|
|
109
115
|
* @throws {Error} Throws an error if the HTTP request fails or returns a non-ok status
|
|
@@ -119,7 +125,5 @@ export declare abstract class Collection<M extends Model<T>, T extends object> {
|
|
|
119
125
|
* });
|
|
120
126
|
* ```
|
|
121
127
|
*/
|
|
122
|
-
fetch(options?:
|
|
123
|
-
search?: Record<string, string | number | boolean>;
|
|
124
|
-
}): Promise<void>;
|
|
128
|
+
fetch(options?: FetchOptions): Promise<void>;
|
|
125
129
|
}
|
|
@@ -117,7 +117,8 @@ export class Collection {
|
|
|
117
117
|
* Fetches data from the server and populates the collection.
|
|
118
118
|
*
|
|
119
119
|
* @param options - Configuration options for the fetch request
|
|
120
|
-
* @param options.
|
|
120
|
+
* @param [options.endpoint] - Optional endpoint to use if different than this.endpoint()
|
|
121
|
+
* @param [options.search] - Optional search parameters to include in the query string.
|
|
121
122
|
* Keys and values will be converted to strings and URL-encoded.
|
|
122
123
|
*
|
|
123
124
|
* @throws {Error} Throws an error if the HTTP request fails or returns a non-ok status
|
|
@@ -142,7 +143,8 @@ export class Collection {
|
|
|
142
143
|
}
|
|
143
144
|
query = `?${params.toString()}`;
|
|
144
145
|
}
|
|
145
|
-
const
|
|
146
|
+
const endpoint = options?.endpoint?.length ? options.endpoint : this.endpoint();
|
|
147
|
+
const fullUrl = `${vellumConfig.origin}${endpoint}${query}`;
|
|
146
148
|
const response = await fetch(fullUrl, {
|
|
147
149
|
headers: { ...vellumConfig.headers }
|
|
148
150
|
});
|
package/dist/Model.svelte.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { type VellumConfig } from './config.svelte';
|
|
2
|
+
export interface SyncOptions extends Partial<VellumConfig> {
|
|
3
|
+
endpoint?: string;
|
|
4
|
+
}
|
|
2
5
|
export declare abstract class Model<T extends object> {
|
|
3
6
|
#private;
|
|
4
7
|
/**
|
|
@@ -97,7 +100,7 @@ export declare abstract class Model<T extends object> {
|
|
|
97
100
|
* // Delete a user (DELETE request)
|
|
98
101
|
* await user.sync('DELETE'); // Returns null for 204 responses
|
|
99
102
|
*/
|
|
100
|
-
sync<R = T>(method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE', body?: Record<string, unknown> | T, options?:
|
|
103
|
+
sync<R = T>(method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE', body?: Record<string, unknown> | T, options?: SyncOptions): Promise<R | null>;
|
|
101
104
|
/**
|
|
102
105
|
* Fetches data from the server and updates the model's attributes.
|
|
103
106
|
*
|
package/dist/Model.svelte.js
CHANGED
|
@@ -100,9 +100,10 @@ export class Model {
|
|
|
100
100
|
* // Delete a user (DELETE request)
|
|
101
101
|
* await user.sync('DELETE'); // Returns null for 204 responses
|
|
102
102
|
*/
|
|
103
|
-
async sync(method = 'GET', body, options) {
|
|
103
|
+
async sync(method = 'GET', body, options = {}) {
|
|
104
104
|
const id = this.#getId();
|
|
105
|
-
const
|
|
105
|
+
const endpoint = options?.endpoint?.length ? options.endpoint : this.endpoint();
|
|
106
|
+
const fullUrl = `${vellumConfig.origin}${endpoint}`;
|
|
106
107
|
const url = id ? `${fullUrl}/${id}` : fullUrl;
|
|
107
108
|
const fetchOpts = {
|
|
108
109
|
method,
|