Skip to content

Commit 886fc77

Browse files
authored
Merge pull request #210 from proofgeist/fix/layoutmetadata-optional-args
feat: Allow layoutMetadata without args when layout configured on client
2 parents ceb1637 + 50895ad commit 886fc77

3 files changed

Lines changed: 85 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@proofgeist/fmdapi": patch
3+
---
4+
5+
Allow `layoutMetadata` to be called without arguments when layout is pre-configured on the client.

src/client.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,16 +419,19 @@ function DataApi<
419419
}
420420

421421
async function _layoutMetadata(
422-
args: Opts["layout"] extends string
422+
args?: Opts["layout"] extends string
423423
? { timeout?: number } & Partial<WithLayout> & FetchOptions
424424
: { timeout?: number } & WithLayout & FetchOptions,
425425
) {
426-
const { layout, ...params } = args;
426+
const { layout = options.layout, ...restArgs } = args ?? {};
427+
// Explicitly define the type for params based on FetchOptions
428+
const params: FetchOptions & { timeout?: number } = restArgs;
429+
427430
if (layout === undefined) throw new Error("Layout is required");
428431
return await layoutMetadata({
429432
layout,
430-
fetch: params.fetch,
431-
timeout: params.timeout,
433+
fetch: params.fetch, // Now should correctly resolve to undefined if not present
434+
timeout: params.timeout, // Now should correctly resolve to undefined if not present
432435
});
433436
}
434437

test/client-methods.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,79 @@ describe("other methods", () => {
212212
expect(resp.scripts[1] as ScriptOrFolder).toHaveProperty("isFolder");
213213
});
214214

215+
it("should retrieve layout metadata with only the layout parameter", async () => {
216+
const client = DataApi({
217+
adapter: new OttoAdapter({
218+
auth: config.auth,
219+
db: config.db,
220+
server: config.server,
221+
}),
222+
});
223+
const layoutName = "layout"; // Assuming "layout" is a valid layout in the test DB
224+
225+
// Call the method with only the required layout parameter
226+
const response = await client.layoutMetadata({ layout: layoutName });
227+
228+
// Assertion 1: Ensure the call succeeded and returned a response object
229+
expect(response).toBeDefined();
230+
expect(response).toBeTypeOf("object");
231+
232+
// Assertion 2: Check for the presence of core metadata properties
233+
expect(response).toHaveProperty("fieldMetaData");
234+
expect(response).toHaveProperty("portalMetaData");
235+
// valueLists is optional, check type if present
236+
if (response.valueLists) {
237+
expect(Array.isArray(response.valueLists)).toBe(true);
238+
}
239+
240+
// Assertion 3: Verify the types of the core properties
241+
expect(Array.isArray(response.fieldMetaData)).toBe(true);
242+
expect(typeof response.portalMetaData).toBe("object");
243+
244+
// Assertion 4 (Optional but recommended): Check structure of metadata
245+
if (response.fieldMetaData.length > 0) {
246+
expect(response.fieldMetaData[0]).toHaveProperty("name");
247+
expect(response.fieldMetaData[0]).toHaveProperty("type");
248+
}
249+
});
250+
251+
it("should retrieve layout metadata when layout is configured on the client", async () => {
252+
const client = DataApi({
253+
adapter: new OttoAdapter({
254+
auth: config.auth,
255+
db: config.db,
256+
server: config.server,
257+
}),
258+
layout: "layout", // Configure layout on the client
259+
});
260+
261+
// Call the method without the layout parameter (expecting it to use the client's layout)
262+
// No arguments should be needed when layout is configured on the client.
263+
const response = await client.layoutMetadata();
264+
265+
// Assertion 1: Ensure the call succeeded and returned a response object
266+
expect(response).toBeDefined();
267+
expect(response).toBeTypeOf("object");
268+
269+
// Assertion 2: Check for the presence of core metadata properties
270+
expect(response).toHaveProperty("fieldMetaData");
271+
expect(response).toHaveProperty("portalMetaData");
272+
// valueLists is optional, check type if present
273+
if (response.valueLists) {
274+
expect(Array.isArray(response.valueLists)).toBe(true);
275+
}
276+
277+
// Assertion 3: Verify the types of the core properties
278+
expect(Array.isArray(response.fieldMetaData)).toBe(true);
279+
expect(typeof response.portalMetaData).toBe("object");
280+
281+
// Assertion 4 (Optional but recommended): Check structure of metadata
282+
if (response.fieldMetaData.length > 0) {
283+
expect(response.fieldMetaData[0]).toHaveProperty("name");
284+
expect(response.fieldMetaData[0]).toHaveProperty("type");
285+
}
286+
});
287+
215288
it("should paginate through all records", async () => {
216289
const client = DataApi({
217290
adapter: new OttoAdapter({

0 commit comments

Comments
 (0)