Skip to content

Commit

Permalink
feat(hub-common): add composeHubContent() for creating an IHubEditabl…
Browse files Browse the repository at this point in the history
…eContent from pre-fetched parts (#1630)
  • Loading branch information
sonofflynn89 authored Aug 23, 2024
1 parent 1dd9a1f commit 2626a26
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
34 changes: 34 additions & 0 deletions packages/common/src/content/composeHubContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { IHubEditableContentEnrichments } from "../items/_enrichments";
import { IHubEditableContent } from "../core/types/IHubEditableContent";
import { normalizeItemType } from "./compose";
import { setProp } from "../objects/set-prop";
import { modelToHubEditableContent } from "./modelToHubEditableContent";
import { cloneObject } from "../util";
import { IItem } from "@esri/arcgis-rest-portal";
import { IHubRequestOptions } from "../types";

/**
* composes a Hub content entity from an item and optional enrichments
* @param item item to compose content from
* @param requestOptions request options (needed to determine certain urls)
* @param enrichments enrichments to use during composition
* @returns content entity
*/
export const composeHubContent = (
item: IItem,
requestOptions: IHubRequestOptions,
enrichments: IHubEditableContentEnrichments
): IHubEditableContent => {
// we must normalize the underlying item type to account
// for older items (e.g. sites that are type "Web Mapping
// Application") before we map the model to a Hub Entity
const normalizedItem = cloneObject(item);
const type = normalizeItemType(item);
setProp("type", type, normalizedItem);

return modelToHubEditableContent(
{ item: normalizedItem },
requestOptions,
enrichments
);
};
2 changes: 1 addition & 1 deletion packages/common/src/content/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export * from "./compose";
export * from "./get-family";
export * from "./compose";
export * from "./contentUtils";
export * from "./edit";
export * from "./fetchContent";
export * from "./fetchHubContent";
export * from "./composeHubContent";
export * from "./modelToHubEditableContent";
export * from "./get-family";
export * from "./HubContent";
Expand Down
62 changes: 62 additions & 0 deletions packages/common/test/content/composeHubContent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
HOSTED_FEATURE_SERVICE_DEFINITION,
HOSTED_FEATURE_SERVICE_GUID,
HOSTED_FEATURE_SERVICE_ITEM,
PDF_GUID,
PDF_ITEM,
} from "./fixtures";
import { MOCK_AUTH } from "../mocks/mock-auth";
import {
composeHubContent,
IHubRequestOptions,
IServiceExtendedProps,
} from "../../src";
import { IHubEditableContentEnrichments } from "../../src/items/_enrichments";
import { IItem } from "@esri/arcgis-rest-portal";

describe("composeHubContent", () => {
it("composes feature service content", () => {
const requestOptions = {
portal: MOCK_AUTH.portal,
authentication: MOCK_AUTH,
} as IHubRequestOptions;

const enrichments: IHubEditableContentEnrichments = {
server: HOSTED_FEATURE_SERVICE_DEFINITION,
};

const chk = composeHubContent(
HOSTED_FEATURE_SERVICE_ITEM,
requestOptions,
enrichments
);
const extendedProps = chk.extendedProps as IServiceExtendedProps;
expect(chk.id).toBe(HOSTED_FEATURE_SERVICE_GUID);
expect(chk.owner).toBe(HOSTED_FEATURE_SERVICE_ITEM.owner);
expect(extendedProps.serverExtractCapability).toBeTruthy();
expect(extendedProps.serverQueryCapability).toBeTruthy();
});

it("composes non-service content", () => {
const requestOptions = { authentication: MOCK_AUTH } as IHubRequestOptions;
const enrichments: IHubEditableContentEnrichments = { metadata: null };

const chk = composeHubContent(PDF_ITEM, requestOptions, enrichments);
expect(chk.id).toBe(PDF_GUID);
expect(chk.owner).toBe(PDF_ITEM.owner);
});

it("normalizes the item type", () => {
const item = {
id: "ae3",
type: "Web Mapping Application",
typeKeywords: ["hubSite"],
} as IItem;
const requestOptions = { authentication: MOCK_AUTH } as IHubRequestOptions;
const enrichments: IHubEditableContentEnrichments = { metadata: null };

const chk = composeHubContent(item, requestOptions, enrichments);

expect(chk.type).toBe("Hub Site Application");
});
});

0 comments on commit 2626a26

Please sign in to comment.