From 985d209b790f98b08141e6439d3689c9fc65f3de Mon Sep 17 00:00:00 2001 From: Louis Bompart Date: Wed, 14 Dec 2022 14:49:15 -0500 Subject: [PATCH] feat: normalize docID to valid URI (#236) * chore(cd): ensure we update package-lock.json on release * feat: normalize docID to valid URI --- package-lock.json | 11 ++++++++++- package.json | 3 ++- src/documentBuilder.spec.ts | 10 ++++++++-- src/documentBuilder.ts | 12 +++++++++++- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index ae1ce842..0e86153a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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:.", @@ -11345,6 +11346,14 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } + }, + "node_modules/zod": { + "version": "3.20.2", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.20.2.tgz", + "integrity": "sha512-1MzNQdAvO+54H+EaK5YpyEy0T+Ejo/7YLHS93G3RnYWh5gaotGHwGeN/ZO687qEDU2y4CdStQYXVHIgrUl5UVQ==", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } } } } \ No newline at end of file diff --git a/package.json b/package.json index 91b0dbaf..ac50edb5 100644 --- a/package.json +++ b/package.json @@ -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:.", diff --git a/src/documentBuilder.spec.ts b/src/documentBuilder.spec.ts index 8f21080f..ebc146d4 100644 --- a/src/documentBuilder.spec.ts +++ b/src/documentBuilder.spec.ts @@ -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', () => { diff --git a/src/documentBuilder.ts b/src/documentBuilder.ts index 23202f7f..8c19044a 100644 --- a/src/documentBuilder.ts +++ b/src/documentBuilder.ts @@ -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}. */ @@ -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 {};