Skip to content

Commit 3290d29

Browse files
committed
Import JSON AD string function
1 parent c4c7004 commit 3290d29

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This changelog covers all three packages, as they are (for now) updated as a who
66

77
- Add `Store.parseMetaTags` to load JSON-AD objects stored in the DOM. Speeds up initial page load by allowing server to set JSON-AD objects in the initial HTML response.
88
- Move static assets around, align build with server and fix PWA #292
9+
- `store.createSubject` allows creating nested paths
10+
- Add `useChildren` hook and `Store.getChildren` method
11+
- Add `Store.postToServer` method, add `endpoints`, `import_json_ad_string`
912

1013
## v0.35.0
1114

lib/src/endpoints.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Store } from './index.js';
2+
/** Endpoints are Resources that can respond to query parameters or POST bodies */
3+
4+
/** POSTs a JSON-AD object to the Server */
5+
export function importJsonAdString(
6+
store: Store,
7+
importerUrl: string,
8+
jsonAdString: string,
9+
) {
10+
return store.postToServer(importerUrl, jsonAdString);
11+
}

lib/src/store.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,22 @@ export class Store {
435435
});
436436
}
437437

438+
/** Sends an HTTP POST request to the server to the Subject. Parses the returned Resource and adds it to the store. */
439+
public async postToServer(
440+
parent: string,
441+
data: ArrayBuffer | string,
442+
): Promise<Resource> {
443+
const url = new URL(parent);
444+
url.searchParams.set('parent', parent);
445+
url.pathname = '/import';
446+
447+
return this.fetchResourceFromServer(url.toString(), {
448+
body: data,
449+
noWebSocket: true,
450+
method: 'POST',
451+
});
452+
}
453+
438454
/** Removes (destroys / deletes) resource from this store */
439455
public removeResource(subject: string): void {
440456
const resource = this.resources.get(subject);

react/src/useImporter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { useEffect, useState } from 'react';
2-
import { useResource } from './index.js';
2+
import {
3+
importJsonAdString as importJsonAdString,
4+
useResource,
5+
useStore,
6+
} from './index.js';
37

48
/** Easily send JSON-AD or a URL containing it to your server. */
59
export function useImporter(importerUrl?: string) {

0 commit comments

Comments
 (0)