Skip to content

Commit

Permalink
feat: normalize docID to valid URI (#236)
Browse files Browse the repository at this point in the history
* chore(cd): ensure we update package-lock.json on release

* feat: normalize docID to valid URI
  • Loading branch information
louis-bompart authored Dec 14, 2022
1 parent 72ffd15 commit 985d209
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
11 changes: 10 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"axios": "^1.2.0",
"dayjs": "^1.10.4",
"exponential-backoff": "^3.1.0",
"isomorphic-fetch": "^3.0.0"
"isomorphic-fetch": "^3.0.0",
"zod": "^3.20.2"
},
"devDependencies": {
"@coveo/push-api-client": "file:.",
Expand Down
10 changes: 8 additions & 2 deletions src/documentBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ describe('DocumentBuilder', () => {
expect(docBuilder.marshal().title).toBe('title');
});

it('should marshal uri to documentId', () => {
expect(docBuilder.marshal().documentId).toBe('uri');
it('should uri marshal to documentID (path->uri)', () => {
expect(docBuilder.marshal().documentId).toMatch(/file:\/\/.*\/uri/);
});

it('should uri marshal to documentID (uri->uri)', () => {
expect(
new DocumentBuilder('foo://bar.biz', 'aaa').marshal().documentId
).toBe('foo://bar.biz');
});

it('should marshal data', () => {
Expand Down
12 changes: 11 additions & 1 deletion src/documentBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
BuiltInTransformers,
Transformer,
} from './validation/transformers/transformer';
import {z} from 'zod';
import {pathToFileURL} from 'url';
/**
* Utility class to build a {@link Document}.
*/
Expand Down Expand Up @@ -255,11 +257,19 @@ export class DocumentBuilder {
...this.marshalMetadata(),
...this.marshalCompressedBinaryData(),
...this.marshalPermissions(),
documentId: uri,
...this.marshalDocumentId(),
};
return out;
}

private marshalDocumentId() {
return {
documentId: z.string().url().safeParse(this.uri).success
? this.uri
: pathToFileURL(this.uri).toString(),
};
}

private marshalMetadata() {
if (!this.doc.metadata) {
return {};
Expand Down

0 comments on commit 985d209

Please sign in to comment.