Skip to content

Commit

Permalink
feat(hub-discussions): add exportPosts fn to export discussion posts …
Browse files Browse the repository at this point in the history
…as CSV string (#1745)
  • Loading branch information
rweber-esri authored Nov 22, 2024
1 parent 603ac70 commit 5dba4b4
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 35 deletions.
96 changes: 76 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/discussions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"peerDependencies": {
"@esri/arcgis-rest-auth": "^2.14.0 || 3",
"@esri/arcgis-rest-request": "^2.14.0 || 3",
"@esri/hub-common": "^15.7.1"
"@esri/hub-common": "^15.8.0"
},
"devDependencies": {
"@esri/hub-common": "^15.7.1",
"@esri/hub-common": "^15.8.0",
"@types/geojson": "^7946.0.7",
"typescript": "^3.8.1"
},
Expand Down
56 changes: 43 additions & 13 deletions packages/discussions/src/posts/posts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/* tslint:disable unified-signatures */
import { discussionsApiRequest } from "@esri/hub-common";
import {
discussionsApiRequest,
ISearchPosts,
SearchPostsFormat,
} from "@esri/hub-common";
import {
ICreatePostParams,
ICreateReplyParams,
IPost,
ISearchPostsParams,
IExportPostsParams,
IFetchPostParams,
IRemovePostParams,
IRemovePostResponse,
Expand All @@ -13,6 +18,21 @@ import {
IPagedResponse,
} from "../types";

const stringifySearchParams = (data: ISearchPosts): any => {
// need to serialize geometry and featureGeometry since this
// is a GET request. we should consider requiring this to be
// a base64 string to safeguard against large geometries that
// will exceed URL character limits
const paramsToStringify = ["geometry", "featureGeometry"];
return Object.entries(data ?? {}).reduce(
(acc, [key, val]) => ({
...acc,
[key]: paramsToStringify.includes(key) ? JSON.stringify(val) : val,
}),
{}
);
};

/**
* search posts
*
Expand All @@ -24,24 +44,34 @@ export function searchPosts(
options: ISearchPostsParams
): Promise<IPagedResponse<IPost>> {
const url = `/posts`;
// need to serialize geometry and featureGeometry since this
// is a GET request. we should consider requiring this to be
// a base64 string to safeguard against large geometries that
// will exceed URL character limits
const data = ["geometry", "featureGeometry"].reduce(
(acc, property) =>
acc[property]
? { ...acc, [property]: JSON.stringify(acc[property]) }
: acc,
{ ...(options.data ?? {}) } as any
);
return discussionsApiRequest(url, {
const data = stringifySearchParams(options.data);
return discussionsApiRequest<IPagedResponse<IPost>>(url, {
...options,
data,
httpMethod: "GET",
});
}

/**
* searches for posts and resolves a promise with CSV string representing the results
*
* @export
* @param {IExportPostsParams} options
* @return {*} {Promise<string>}
*/
export function exportPosts(options: IExportPostsParams): Promise<string> {
const url = `/posts`;
const data = stringifySearchParams(options.data);
return discussionsApiRequest<string>(url, {
...options,
data: {
...data,
f: SearchPostsFormat.CSV,
},
httpMethod: "GET",
});
}

/**
* create post
*
Expand Down
1 change: 1 addition & 0 deletions packages/discussions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export {
IUpdatePostStatus,
IUpdatePost,
ISearchPostsParams,
IExportPostsParams,
IFetchPostParams,
IUpdatePostParams,
IUpdatePostStatusParams,
Expand Down
27 changes: 27 additions & 0 deletions packages/discussions/test/posts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
removePost,
createReply,
updatePostStatus,
exportPosts,
} from "../src/posts";
import * as hubCommon from "@esri/hub-common";
import {
Expand All @@ -16,6 +17,7 @@ import {
IFetchPostParams,
PostStatus,
Role,
SearchPostsFormat,
SharingAccess,
} from "../src/types";

Expand Down Expand Up @@ -145,6 +147,31 @@ describe("posts", () => {
.catch(() => fail());
});

it("exports posts as a CSV string", (done) => {
const query = {
access: [SharingAccess.PUBLIC],
groups: ["foo"],
};

const options = { ...baseOpts, data: query };
exportPosts(options)
.then(() => {
expect(requestSpy.calls.count()).toEqual(1);
const [url, opts] = requestSpy.calls.argsFor(0);
expect(url).toEqual(`/posts`);
expect(opts).toEqual({
...options,
data: {
...options.data,
f: SearchPostsFormat.CSV,
},
httpMethod: "GET",
});
done();
})
.catch(() => fail());
});

it("creates post on unknown or non-existent channel", (done) => {
const body = {
access: SharingAccess.PRIVATE,
Expand Down

0 comments on commit 5dba4b4

Please sign in to comment.