Skip to content

Instantly share code, notes, and snippets.

@strass
Created June 21, 2021 23:45
Show Gist options
  • Save strass/02b2fb32e3d790361606944694e1750e to your computer and use it in GitHub Desktop.
Save strass/02b2fb32e3d790361606944694e1750e to your computer and use it in GitHub Desktop.
fetchAppDataRegistration
export const fetchAppDataRegistration = async (
typeIndexWebIds: string[],
fetch: typeof window.fetch
) =>
await Promise.all(
typeIndexWebIds.map(async (url) => {
const typeIndexDataset = await getSolidDataset(url, { fetch });
const allThings = getThingAll(typeIndexDataset);
// I'm pretty sure we don't expect multiple TypeIndexes, but is there a better practice?
const typeIndex = allThings.find((t) => {
const thingTypes = getUrlAll(t, TYPE_PREDICATE);
return thingTypes.includes(
"http://www.w3.org/ns/solid/terms#TypeIndex"
);
});
if (!typeIndex) {
throw new Error(`Could not find TypeIndex in '${typeIndex}'`);
}
const isListed = getUrlAll(typeIndex, TYPE_PREDICATE).includes(
"http://www.w3.org/ns/solid/terms#ListedDocument"
);
const isUnlisted = getUrlAll(typeIndex, TYPE_PREDICATE).includes(
"http://www.w3.org/ns/solid/terms#UnlistedDocument"
);
const typeRegistrations = allThings
.filter((t) => {
const thingTypes = getUrlAll(t, TYPE_PREDICATE);
return thingTypes.includes(
"http://www.w3.org/ns/solid/terms#TypeRegistration"
);
})
.filter((t) => {
const forClass = getStringNoLocale(
t,
"http://www.w3.org/ns/solid/terms#forClass"
);
return forClass === APP_ID;
});
return {
url: getSourceUrl(typeIndexDataset),
dataset: typeIndexDataset,
type: (isListed && "listed") || (isUnlisted && "unlisted") || undefined,
instances: typeRegistrations.flatMap((tr) =>
getUrlAll(tr, "http://www.w3.org/ns/solid/terms#instance")
),
instanceContainers: typeRegistrations.flatMap((tr) =>
getUrlAll(tr, "http://www.w3.org/ns/solid/terms#instanceContainer")
),
};
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment