Skip to content

Commit

Permalink
Make getDynamicProperties async
Browse files Browse the repository at this point in the history
This is so implementors can utilize await/async code inside the generator
  • Loading branch information
chrisrohr committed Jun 13, 2024
1 parent 0d2086e commit 9557a08
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
20 changes: 9 additions & 11 deletions __tests__/property-extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ describe("Property Extractor", () => {
foo: "boom",
};

expect(() => {
PropertyExtractor.getPropertyListForObject(badObj);
}).toThrowError(
new Error(
expect(async () => {
await PropertyExtractor.getPropertyListForObject(badObj);
}).rejects.toEqual(
'The given object does not have a "getDynamicProperties" method. Can not process property list.',
),
);
});

it("should return the properties for the obj", () => {
it("should return the properties for the obj", async () => {
const prop = Property.newProperty().setName("foo").setType("string");

const goodObj = {
Expand All @@ -32,7 +30,7 @@ describe("Property Extractor", () => {
},
};

const propList = PropertyExtractor.getPropertyListForObject(goodObj);
const propList = await PropertyExtractor.getPropertyListForObject(goodObj);

expect(propList).toEqual([prop]);
});
Expand All @@ -59,16 +57,16 @@ describe("Property Extractor", () => {
const studentObject = {
foo: "Yeah",

getDynamicProperties: () => {
return [studentProp];
getDynamicProperties: async () => {
return Promise.resolve([studentProp]);
},
};

const teacherObject = {
foo: "Yeah",

getDynamicProperties: () => {
return [teacherProp];
getDynamicProperties: async () => {
return Promise.resolve([teacherProp]);
},
};

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kiwiproject/dynamic-properties-provider",
"version": "0.1.1",
"version": "0.2.0",
"description": "Dynamic Properties Provider is a utility library to assist in the generation of field definitions based on model classes. This process is helpful if a UI needs to build dynamic forms based on models in a service but don't want to hardcode all the fields of the models in the UI. This is a port of the Java library with the same name.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
20 changes: 9 additions & 11 deletions src/property-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import { Property } from "./property";
* @param obj The object that has a getDynamicProperties method
* @throws error if given object does not have a getDynamicProperties method
*/
function getPropertyListForObject(obj: any): Array<Property> {
if (!obj.getDynamicProperties) {
throw new Error(
'The given object does not have a "getDynamicProperties" method. Can not process property list.',
);
async function getPropertyListForObject(obj: any): Promise<Array<Property>> {
if (!obj.hasOwnProperty("getDynamicProperties")) {
await Promise.reject('The given object does not have a "getDynamicProperties" method. Can not process property list.');
}

return obj.getDynamicProperties();
return await obj.getDynamicProperties();
}

/**
Expand All @@ -26,19 +24,19 @@ function setupDynamicPropertiesEndpoints(
): express.Router {
const router = express.Router();

router.get("/kiwi/dynamic-properties", (_req, res) => {
router.get("/kiwi/dynamic-properties", async (_req, res, next) => {
const props: object = {};
for (const key of Object.keys(objects)) {
props[key] = getPropertyListForObject(objects[key]);
props[key] = await getPropertyListForObject(objects[key]).catch(next);
}

res.json(props);
});

router.get("/kiwi/dynamic-properties/:identifier", (req, res) => {
const objectProps = getPropertyListForObject(
router.get("/kiwi/dynamic-properties/:identifier", async (req, res, next) => {
const objectProps = await getPropertyListForObject(
objects[req.params.identifier],
);
).catch(next);
res.json(objectProps);
});

Expand Down

0 comments on commit 9557a08

Please sign in to comment.