Skip to content

Commit

Permalink
Add header=colId option for the table-schema API #719 (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent authored Nov 17, 2023
1 parent d8b224b commit 7bc862f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
17 changes: 4 additions & 13 deletions app/server/lib/ExportTableSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function collectTableSchemaInFrictionlessFormat(
req: express.Request,
options: DownloadOptions
): Promise<FrictionlessFormat> {
const {tableId} = options;
const {tableId, header} = options;
if (!activeDoc.docData) {
throw new Error('No docData in active document');
}
Expand All @@ -50,24 +50,15 @@ export async function collectTableSchemaInFrictionlessFormat(
throw new ApiError(`Table ${tableId} not found.`, 404);
}

const data = await exportTable(activeDoc, tableRef, req);
const tableSchema = columnsToTableSchema(tableId, data, settings.locale);
return tableSchema;
}

function columnsToTableSchema(
tableId: string,
{tableName, columns}: {tableName: string, columns: ExportColumn[]},
locale: string,
): FrictionlessFormat {
const {tableName, columns} = await exportTable(activeDoc, tableRef, req);
return {
name: tableId.toLowerCase().replace(/_/g, '-'),
title: tableName,
schema: {
fields: columns.map(col => ({
name: col.label,
name: col[header || "label"],
...(col.description ? {description: col.description} : {}),
...buildTypeField(col, locale),
...buildTypeField(col, settings.locale),
})),
}
};
Expand Down
37 changes: 37 additions & 0 deletions test/server/lib/DocApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2695,6 +2695,43 @@ function testDocApi() {
assert.equal(resp2.data, 'A,B\nSanta,1\nBob,11\nAlice,2\nFelix,22\n');
});

it("GET /docs/{did}/download/table-schema serves table-schema-encoded document with header=colId", async function () {
const { docUrl, tableUrl } = await generateDocAndUrl('tableSchemaWithColIdAsHeader');
const columns = [
{
id: 'Some_ID',
fields: {
label: 'Some Label',
type: 'Text',
}
},
];
const setupColResp = await axios.put(`${tableUrl}/columns`, { columns }, {...chimpy, params: { replaceall: true }});
assert.equal(setupColResp.status, 200);

const resp = await axios.get(`${docUrl}/download/table-schema?tableId=Table1&header=colId`, chimpy);
assert.equal(resp.status, 200);
const expected = {
format: "csv",
mediatype: "text/csv",
encoding: "utf-8",
dialect: {
delimiter: ",",
doubleQuote: true,
},
name: 'table1',
title: 'Table1',
schema: {
fields: [{
name: 'Some_ID',
type: 'string',
format: 'default',
}]
}
};
assert.deepInclude(resp.data, expected);
});

it("GET /docs/{did}/download/table-schema respects permissions", async function () {
// kiwi has no access to TestDoc
const resp = await axios.get(`${serverUrl}/api/docs/${docIds.TestDoc}/download/table-schema?tableId=Table1`, kiwi);
Expand Down

0 comments on commit 7bc862f

Please sign in to comment.