Skip to content

Commit 8eea0ac

Browse files
deal with link or string
1 parent 8f9d33b commit 8eea0ac

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

movies/vanilla/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Solid Data Module for Movies
22

3+
Data schemas supported:
4+
* Media Kraken as-is
5+
* Media Kraken with correctly typed links
6+
37
### Usage
48
Set up a typescript project, install [solid-data-module-tasks](https://www.npmjs.com/package/solid-data-module-tasks) from NPM, and create a `.env` file like this:
59
```env

movies/vanilla/src/jsonld.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
export function getJsonLdFields(
2+
entry: object,
3+
pred: string,
4+
subPred: string,
5+
): string[] {
6+
if (Array.isArray(entry[pred])) {
7+
return entry[pred].map(obj => obj[subPred]);
8+
}
9+
return [];
10+
}
11+
112
export function getJsonLdField(
213
entry: object,
314
pred: string,
415
subPred: string,
516
): string | undefined {
6-
if (Array.isArray(entry[pred]) && entry[pred].length === 1) {
7-
return entry[pred][0][subPred];
17+
const results = getJsonLdFields(entry, pred, subPred);
18+
if (results.length === 1) {
19+
return results[0];
820
}
921
return undefined;
1022
}
@@ -46,6 +58,19 @@ export function getJsonLdStringField(
4658
return getJsonLdField(entry, pred, '@value');
4759
}
4860

61+
// use this function to deal with cases where
62+
// links might be incorrectly stored as strings
63+
export function getJsonLdLinkOrStringField(
64+
entry: object,
65+
pred: string,
66+
): string | undefined {
67+
let ret = getJsonLdLinkField(entry, pred);
68+
if (typeof ret === 'undefined') {
69+
ret = getJsonLdStringField(entry, pred);
70+
}
71+
return ret;
72+
}
73+
4974
export function getJsonLdStringFieldMulti(
5075
entry: object,
5176
pred: string,

movies/vanilla/src/movies.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
getJsonLdDateField,
3-
getJsonLdLinkField,
3+
getJsonLdLinkOrStringField,
44
getJsonLdStringField,
55
} from './jsonld.js';
66

@@ -75,11 +75,11 @@ export async function fetchList(
7575
thing,
7676
'https://schema.org/description',
7777
),
78-
image: getJsonLdStringField(thing, 'https://schema.org/image'), // sic: string instead of link here
78+
image: getJsonLdLinkOrStringField(thing, 'https://schema.org/image'),
7979
name: getJsonLdStringField(thing, 'https://schema.org/name'),
8080
sameAs: thing['https://schema.org/sameAs'].map(
81-
(x) => x['@value'],
82-
) as string[], // sic: string instead of link here
81+
(x) => (x['@id'] || x['@value']),
82+
) as string[],
8383
},
8484
});
8585
} else if (
@@ -95,7 +95,7 @@ export async function fetchList(
9595
'https://schema.org/startTime',
9696
),
9797
endTime: getJsonLdDateField(thing, 'https://schema.org/endTime'),
98-
listingId: getJsonLdLinkField(thing, 'https://schema.org/object'),
98+
listingId: getJsonLdLinkOrStringField(thing, 'https://schema.org/object'),
9999
});
100100
} else {
101101
console.log('thing type not recognised', thing);

tasks/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
To use this Solid Data Module you need an authenticated fetch function. In Node.JS, when working against a CSS or Pivot server such as solidcommnunity.net, you can use [css-authn](https://www.npmjs.com/package/css-authn#usage) for this. In the browser, you can use [Inrupt Solid Client](https://docs.inrupt.com/developer-tools/javascript/client-libraries/authentication/).
44

5+
Data schemas supported:
6+
* Solid OS Issue Pane
57

68
### Usage
79
Set up a typescript project, install [solid-data-module-tasks](https://www.npmjs.com/package/solid-data-module-tasks) from NPM, and create a `.env` file like this:

0 commit comments

Comments
 (0)