Skip to content

Commit

Permalink
fix(hub-content): content.item.type was layer.type when fetched from …
Browse files Browse the repository at this point in the history
…the API

affects: @esri/hub-content

now it is item.type
  • Loading branch information
tomwayson committed Aug 30, 2021
1 parent f66e08a commit 2e884ac
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
20 changes: 17 additions & 3 deletions packages/content/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { ResourceObject } from "jsonapi-typescript";
import { IItem, getItem } from "@esri/arcgis-rest-portal";
import {
IHubContent,
hubApiRequest,
cloneObject,
getServiceTypeFromUrl,
hubApiRequest,
mergeObjects,
} from "@esri/hub-common";
import { itemToContent } from "./portal";
import { getFamily, itemToContent } from "./portal";
import { isSlug, addContextToSlug, parseDatasetId } from "./slugs";
import { enrichContent, IEnrichContentOptions } from "./enrichments";
import { isExtentCoordinateArray } from "@esri/hub-common";
Expand Down Expand Up @@ -123,6 +124,7 @@ export function datasetToContent(dataset: DatasetResource): IHubContent {

// overwrite hubId
content.hubId = dataset.id;

// overwrite or add enrichments from Hub API
const attributes = dataset.attributes;
const {
Expand Down Expand Up @@ -157,7 +159,15 @@ export function datasetToContent(dataset: DatasetResource): IHubContent {
orgName,
organization,
orgExtent,
// NOTE: for layers and tables the Hub API returns the layer type
// ("Feature Layer", "Table", "Raster Layer") instead of the item type
type,
} = attributes;

// use the type returned by the API (i.e. possibly layer.type)
content.type = type;
content.family = getFamily(type);

// NOTE: we could throw or return if there are errors
// to prevent type errors trying to read properties below
content.errors = errors;
Expand Down Expand Up @@ -292,6 +302,10 @@ export function datasetToItem(dataset: DatasetResource): IItem {
serviceSpatialReference,
} = attributes;

// layer datasets will get their type from the layer
// so we will need to derive the item type from the URL
const serviceType = url && getServiceTypeFromUrl(url);

// build and return an item from properties
// NOTE: we currently do NOT provide default values
// (i.e. null for scalar attributes, [] for arrays, etc)
Expand All @@ -308,7 +322,7 @@ export function datasetToItem(dataset: DatasetResource): IItem {
modified: (itemModified ||
(modifiedProvenance === "item.modified" && modified)) as number,
title: (title || name) as string,
type,
type: serviceType || type,
typeKeywords,
description,
tags,
Expand Down
6 changes: 6 additions & 0 deletions packages/content/src/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function itemToContent(item: IItem): IHubContent {
// we don't store item.name in the Hub API and we use name for title
name: item.title,
family: getFamily(normalizedType),
// TODO: hubType is no longer used, remove it at next breaking change
hubType: getItemHubType(item),
normalizedType,
categories: parseItemCategories(item.categories),
Expand Down Expand Up @@ -117,11 +118,16 @@ export function itemToContent(item: IItem): IHubContent {
}

/**
* DEPRECATED: Use getFamily() instead.
*
* get the HubType for a given item or item type
*
* @param itemOrType an item or item.type
*/
export function getItemHubType(itemOrType: IItem | string): HubType {
console.warn(
"DEPRECATED: Use getFamily() instead. getItemHubType() will be removed at v9.0.0"
);
if (typeof itemOrType === "string") {
itemOrType = { type: itemOrType } as IItem;
}
Expand Down
38 changes: 19 additions & 19 deletions packages/content/test/hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
parseDatasetId,
datasetToItem,
getContentFromHub,
datasetToContent
datasetToContent,
} from "../src/index";
import { IHubRequestOptions, cloneObject, IHubContent } from "@esri/hub-common";
import * as documentsJson from "./mocks/datasets/document.json";
Expand All @@ -15,7 +15,7 @@ import { mockUserSession } from "./test-helpers/fake-user-session";
function validateContentFromDataset(
content: IHubContent,
dataset: DatasetResource,
expectedHubType: string
expectedFamily: string
) {
const { id, attributes } = dataset;
// should have copied these attributes directly
Expand All @@ -37,11 +37,11 @@ function validateContentFromDataset(
"url",
"access",
"size",
"commentsEnabled"
"commentsEnabled",
// TODO: what about the others that will be undefined?
];
// should have set item properties
itemProperties.forEach(key => {
itemProperties.forEach((key) => {
expect(content[key]).toEqual(attributes[key]);
});
// we use attributes.name for both name and title
Expand All @@ -51,13 +51,13 @@ function validateContentFromDataset(
expect(content.hubId).toBe(id);
const extent = attributes.extent;
expect(content.extent).toEqual(extent && extent.coordinates);
expect(content.hubType).toBe(expectedHubType);
expect(content.family).toBe(expectedFamily);
expect(content.summary).toBe(
attributes.searchDescription || attributes.snippet
);
expect(content.publisher).toEqual({
name: attributes.owner,
username: attributes.owner
username: attributes.owner,
});
expect(content.permissions.visibility).toBe(attributes.access);
// no itemControl returned w/ this item, expect default
Expand Down Expand Up @@ -86,7 +86,7 @@ function validateContentFromDataset(
}

describe("hub", () => {
describe("parseDatasetId", function() {
describe("parseDatasetId", function () {
it("returns undefined", () => {
const result = parseDatasetId(undefined);
expect(result).toEqual({ itemId: undefined, layerId: undefined });
Expand All @@ -95,14 +95,14 @@ describe("hub", () => {
const result = parseDatasetId("7a153563b0c74f7eb2b3eae8a66f2fbb");
expect(result).toEqual({
itemId: "7a153563b0c74f7eb2b3eae8a66f2fbb",
layerId: undefined
layerId: undefined,
});
});
it("parse item id and layer id", () => {
const result = parseDatasetId("7a153563b0c74f7eb2b3eae8a66f2fbb_0");
expect(result).toEqual({
itemId: "7a153563b0c74f7eb2b3eae8a66f2fbb",
layerId: "0"
layerId: "0",
});
});
});
Expand Down Expand Up @@ -163,7 +163,7 @@ describe("hub", () => {
orgId: id,
orgExtent: extent,
orgName: name,
organization
organization,
} = dataset.attributes;
let content = datasetToContent(dataset);
expect(content.org).toEqual({ id, extent, name });
Expand Down Expand Up @@ -196,15 +196,15 @@ describe("hub", () => {
user: {},
id: "123",
isPortal: false,
name: "some-portal"
name: "some-portal",
},
isPortal: false,
hubApiUrl: "https://some.url.com/",
authentication: mockUserSession
authentication: mockUserSession,
};
});
afterEach(fetchMock.restore);
it("should fetch a dataset record by id and return content", done => {
it("should fetch a dataset record by id and return content", (done) => {
fetchMock.once(
"https://some.url.com/api/v3/datasets/7a153563b0c74f7eb2b3eae8a66f2fbb_0",
featureLayerJson
Expand All @@ -216,7 +216,7 @@ describe("hub", () => {
const dataset = featureLayerJson.data as DatasetResource;
const datasetId = dataset.id;
const itemId = parseDatasetId(datasetId).itemId;
getContentFromHub(datasetId, requestOpts).then(content => {
getContentFromHub(datasetId, requestOpts).then((content) => {
// verify that we attempted to fetch from the portal API
let [url, opts] = fetchMock.calls()[0];
expect(url).toBe(`https://some.url.com/api/v3/datasets/${datasetId}`);
Expand All @@ -242,15 +242,15 @@ describe("hub", () => {
done();
});
});
it("should fetch a dataset record by id when unauthenticated and return content", done => {
it("should fetch a dataset record by id when unauthenticated and return content", (done) => {
fetchMock.once(
"https://some.url.com/api/v3/datasets/7a153563b0c74f7eb2b3eae8a66f2fbb_0",
featureLayerJson
);
const dataset = featureLayerJson.data as DatasetResource;
const id = dataset.id;
delete requestOpts.authentication;
getContentFromHub(id, requestOpts).then(content => {
getContentFromHub(id, requestOpts).then((content) => {
// verify that we attempted to fetch from the portal API
const [url, opts] = fetchMock.calls()[0];
expect(url).toBe(`https://some.url.com/api/v3/datasets/${id}`);
Expand All @@ -272,11 +272,11 @@ describe("hub", () => {
done();
});
});
it("should fetch a dataset record by slug and return content", done => {
it("should fetch a dataset record by slug and return content", (done) => {
const featureLayersJson = {
// slug requests to datasets w/ filter which returns an array
data: [featureLayerJson.data],
meta: featureLayerJson.meta
meta: featureLayerJson.meta,
};
fetchMock.once(
"https://some.url.com/api/v3/datasets?filter%5Bslug%5D=Wigan%3A%3Aout-of-work-benefit-claims",
Expand All @@ -289,7 +289,7 @@ describe("hub", () => {
const dataset = featureLayersJson.data[0] as DatasetResource;
const ItemId = parseDatasetId(dataset.id).itemId;
const slug = "Wigan::out-of-work-benefit-claims";
getContentFromHub(slug, requestOpts).then(content => {
getContentFromHub(slug, requestOpts).then((content) => {
// verify that we attempted to fetch from the portal API
let [url, opts] = fetchMock.calls()[0];
expect(url).toBe(
Expand Down

0 comments on commit 2e884ac

Please sign in to comment.