Skip to content

Commit eb78ae4

Browse files
committed
Importer uses Post
1 parent 617d5e8 commit eb78ae4

File tree

5 files changed

+63
-21
lines changed

5 files changed

+63
-21
lines changed

data-browser/src/routes/AboutRoute.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const About: React.FunctionComponent = () => {
4343
</p>
4444
<p>
4545
The back-end of this app is{' '}
46-
<AtomicLink href='https://github.com/joepio/atomic'>
46+
<AtomicLink href='https://github.com/atomicdata-dev/atomic-data-rust'>
4747
atomic-server
4848
</AtomicLink>
4949
, which you can think of as an open source, web-native database.
@@ -63,7 +63,7 @@ export const About: React.FunctionComponent = () => {
6363
<h2>Run your own server</h2>
6464
<p>
6565
The easiest way to run an{' '}
66-
<AtomicLink href='https://github.com/joepio/atomic'>
66+
<AtomicLink href='https://github.com/atomicdata-dev/atomic-data-rust'>
6767
atomic-server
6868
</AtomicLink>{' '}
6969
is by using Docker:

lib/src/client.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ interface FetchResourceOptions {
4141
* fetch through that server.
4242
*/
4343
from?: string;
44+
method?: 'GET' | 'POST';
45+
/** The body is only used combined with the `POST` method */
46+
body?: ArrayBuffer | string;
4447
}
4548

46-
interface HTTPResult {
49+
/** Contains one or more Resources */
50+
interface HTTPResourceResult {
4751
resource: Resource;
4852
createdResources: Resource[];
4953
}
5054

55+
/** Contains a `fetch` instance, provides methods to GET and POST several types */
5156
export class Client {
5257
private __fetchOverride?: typeof fetch;
5358

@@ -110,8 +115,8 @@ export class Client {
110115
public async fetchResourceHTTP(
111116
subject: string,
112117
opts: FetchResourceOptions = {},
113-
): Promise<HTTPResult> {
114-
const { signInfo, from } = opts;
118+
): Promise<HTTPResourceResult> {
119+
const { signInfo, from, body: bodyReq } = opts;
115120
let createdResources: Resource[] = [];
116121
const parser = new JSONADParser();
117122
let resource = new Resource(subject);
@@ -143,6 +148,8 @@ export class Client {
143148

144149
const response = await this.fetch(url, {
145150
headers: requestHeaders,
151+
method: bodyReq ? 'POST' : 'GET',
152+
body: bodyReq,
146153
});
147154
const body = await response.text();
148155

@@ -256,16 +263,30 @@ export class Client {
256263
return resources;
257264
}
258265

259-
/** Instructs an Atomic Server to fetch a URL and get its JSON-AD */
260-
public async importJsonAdUrl(
261-
/** The URL of the JSON-AD to import */
262-
jsonAdUrl: string,
263-
/** Importer URL. Servers tend to have one at `example.com/import` */
264-
importerUrl: string,
265-
): Promise<HTTPResult> {
266-
const url = new URL(importerUrl);
267-
url.searchParams.set('url', jsonAdUrl);
268-
269-
return this.fetchResourceHTTP(url.toString());
270-
}
266+
// /** Instructs an Atomic Server to fetch a URL and get its JSON-AD */
267+
// public async importJsonAdUrl(
268+
// /** The URL of the JSON-AD to import */
269+
// jsonAdUrl: string,
270+
// /** Importer URL. Servers tend to have one at `example.com/import` */
271+
// importerUrl: string,
272+
// ): Promise<HTTPResourceResult> {
273+
// const url = new URL(importerUrl);
274+
// url.searchParams.set('url', jsonAdUrl);
275+
276+
// return this.fetchResourceHTTP(url.toString());
277+
// }
278+
279+
// /** Instructs an Atomic Server to fetch a URL and get its JSON-AD */
280+
// public async importJsonAdString(
281+
// /** The JSON-AD to import */
282+
// jsonAdString: string,
283+
// /** Importer URL. Servers tend to have one at `example.com/import` */
284+
// importerUrl: string,
285+
// ): Promise<HTTPResourceResult> {
286+
// const url = new URL(importerUrl);
287+
288+
// return this.fetchResourceHTTP(url.toString(), {
289+
// body: jsonAdString,
290+
// });
291+
// }
271292
}

lib/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export * from './class.js';
3535
export * from './client.js';
3636
export * from './commit.js';
3737
export * from './error.js';
38+
export * from './endpoints.js';
3839
export * from './datatypes.js';
3940
export * from './parse.js';
4041
export * from './resource.js';

lib/src/store.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ export class Store {
189189
setLoading?: boolean;
190190
/** Do not use WebSockets, use HTTP(S) */
191191
noWebSocket?: boolean;
192+
/** HTTP Method, defaults to GET */
193+
method?: 'GET' | 'POST';
194+
/** HTTP Body for POSTing */
195+
body?: ArrayBuffer | string;
192196
} = {},
193197
): Promise<Resource> {
194198
if (opts.setLoading) {
@@ -206,8 +210,10 @@ export class Store {
206210
supportsWebSockets() &&
207211
ws?.readyState === WebSocket.OPEN
208212
) {
213+
// Use WebSocket
209214
await fetchWebSocket(ws, subject);
210215
} else {
216+
// Use HTTPS
211217
const signInfo = this.agent
212218
? { agent: this.agent, serverURL: this.getServerUrl() }
213219
: undefined;
@@ -216,6 +222,8 @@ export class Store {
216222
subject,
217223
{
218224
from: opts.fromProxy ? this.getServerUrl() : undefined,
225+
method: opts.method,
226+
body: opts.body,
219227
signInfo,
220228
},
221229
);

react/src/useImporter.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export function useImporter(importerUrl?: string) {
1010
const [url, setUrl] = useState(importerUrl);
1111
const [success, setSuccess] = useState(false);
1212
const resource = useResource(url);
13+
const store = useStore();
1314

1415
// Get the error from the resource
1516
useEffect(() => {
@@ -29,10 +30,21 @@ export function useImporter(importerUrl?: string) {
2930
setUrl(parsed.toString());
3031
}
3132

32-
function importJsonAd(jsonAdString: string) {
33-
const parsed = new URL(importerUrl!);
34-
parsed.searchParams.set('json', jsonAdString);
35-
setUrl(parsed.toString());
33+
async function importJsonAd(jsonAdString: string) {
34+
if (!importerUrl) {
35+
throw Error('No importer URL given');
36+
}
37+
38+
try {
39+
const resp = await importJsonAdString(store, importerUrl, jsonAdString);
40+
41+
if (resp.error) {
42+
throw resp.error;
43+
}
44+
} catch (e) {
45+
store.notifyError(e);
46+
setSuccess(false);
47+
}
3648
}
3749

3850
return { importJsonAd, importURL, resource, success };

0 commit comments

Comments
 (0)