From 2dc7571276c79872aa01679dd50818ee9930e5f4 Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Tue, 7 Feb 2023 11:04:33 +0100 Subject: [PATCH 1/2] feat: add back `getUrl` & `getDataUrl` --- README.md | 57 --------------------------------------------- src/SanityClient.ts | 40 +++++++++++++++++++++++++++++++ test/client.test.ts | 21 +++++++++++++++++ 3 files changed, 61 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 157446cd..a269b7c8 100644 --- a/README.md +++ b/README.md @@ -1038,63 +1038,6 @@ const client = createClient() console.log(client.config().projectId) ``` -### `client.getUrl()` is removed - -Before: - -```ts -import createClient from '@sanity/client' -const client = createClient({projectId: 'abc123'}) - -console.log(client.getUrl('/foo/bar') === 'https://abc123.api.sanity.io/v1/foo/bar') -console.log(client.getUrl('/foo/bar', true) === 'https://abc123.apicdn.sanity.io/v1/foo/bar') -``` - -After: - -```ts -import {createClient} from '@sanity/client' -const client = createClient({projectId: 'abc123'}) - -const getUrl = (uri: string, useCdn = false) => { - const config = client.config() - const base = useCdn ? config.cdnUrl : config.url - return `${base}/${uri.replace(/^\//, '')}` -} - -console.log(getUrl('/foo/bar') === 'https://abc123.api.sanity.io/v1/foo/bar') -console.log(getUrl('/foo/bar', true) === 'https://abc123.apicdn.sanity.io/v1/foo/bar') -``` - -### `client.getDataUrl()` is removed - -Before: - -```ts -import createClient from '@sanity/client' -const client = createClient({dataset: 'bikeshop'}) - -console.log(client.getDataUrl('doc') === '/data/doc/bikeshop') -console.log(client.getDataUrl('doc', 'bike-123') === '/data/doc/bikeshop/bike-123') -``` - -After: - -```ts -import {createClient} from '@sanity/client' -const client = createClient({dataset: 'bikeshop'}) - -const getDataUrl = (operation: string, path?: string) => { - const {dataset} = client.config() - const baseUri = `/${operation}/${dataset}` - const uri = path ? `${baseUri}/${path}` : baseUri - return `/data${uri}`.replace(/\/($|\?)/, '$1') -} - -console.log(getDataUrl('doc') === '/data/doc/bikeshop') -console.log(getDataUrl('doc', 'bike-123') === '/data/doc/bikeshop/bike-123') -``` - ### `client.isPromiseAPI()` is removed, replace with an `instanceof` check Before: diff --git a/src/SanityClient.ts b/src/SanityClient.ts index ff05347e..f4cfbac1 100644 --- a/src/SanityClient.ts +++ b/src/SanityClient.ts @@ -613,6 +613,26 @@ export class ObservableSanityClient { request(options: RawRequestOptions): Observable { return dataMethods._request(this, this.#httpRequest, options) } + + /** + * Get a Sanity API URL for the URI provided + * + * @param uri URI/path to build URL for + * @param canUseCdn Whether or not to allow using the API CDN for this route + */ + getUrl(uri: string, canUseCdn?: boolean): string { + return dataMethods._getUrl(this, uri, canUseCdn) + } + + /** + * Get a Sanity API URL for the data operation and path provided + * + * @param operation Data operation + * @param path Path to append + */ + getDataUrl(operation: string, path?: string): string { + return dataMethods._getDataUrl(this, operation, path) + } } /** @public */ @@ -1210,4 +1230,24 @@ export class SanityClient { dataRequest(endpoint: string, body: unknown, options?: BaseMutationOptions): Promise { return lastValueFrom(dataMethods._dataRequest(this, this.#httpRequest, endpoint, body, options)) } + + /** + * Get a Sanity API URL for the URI provided + * + * @param uri URI/path to build URL for + * @param canUseCdn Whether or not to allow using the API CDN for this route + */ + getUrl(uri: string, canUseCdn?: boolean): string { + return dataMethods._getUrl(this, uri, canUseCdn) + } + + /** + * Get a Sanity API URL for the data operation and path provided + * + * @param operation Data operation + * @param path Path to append + */ + getDataUrl(operation: string, path?: string): string { + return dataMethods._getDataUrl(this, operation, path) + } } diff --git a/test/client.test.ts b/test/client.test.ts index d0f71b44..537d42ac 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -2312,4 +2312,25 @@ describe('client', async () => { } }) }) + + describe('getUrl', () => { + test('can use getUrl() to get API-relative paths', () => { + expect(getClient().getUrl('/bar/baz')).toEqual(`${projectHost()}/v1/bar/baz`) + }) + + test('can use getUrl() to get API-relative paths (custom api version)', () => { + expect(getClient({apiVersion: '2019-01-29'}).getUrl('/bar/baz')).toEqual( + `${projectHost()}/v2019-01-29/bar/baz` + ) + }) + }) + + describe('getDataUrl', () => { + test('can use getDataUrl() to get API paths to a dataset', () => { + expect(getClient({dataset: 'bikeshop'}).getDataUrl('doc')).toBe('/data/doc/bikeshop') + expect(getClient({dataset: 'bikeshop'}).getDataUrl('doc', 'bike-123')).toBe( + '/data/doc/bikeshop/bike-123' + ) + }) + }) }) From a657530374a1ba4a58263d812ccd52667fc0b821 Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Tue, 7 Feb 2023 12:50:21 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Espen Hovlandsdal Signed-off-by: Cody Olsen --- src/SanityClient.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/SanityClient.ts b/src/SanityClient.ts index f4cfbac1..fc03706f 100644 --- a/src/SanityClient.ts +++ b/src/SanityClient.ts @@ -617,8 +617,8 @@ export class ObservableSanityClient { /** * Get a Sanity API URL for the URI provided * - * @param uri URI/path to build URL for - * @param canUseCdn Whether or not to allow using the API CDN for this route + * @param uri - URI/path to build URL for + * @param canUseCdn - Whether or not to allow using the API CDN for this route */ getUrl(uri: string, canUseCdn?: boolean): string { return dataMethods._getUrl(this, uri, canUseCdn) @@ -627,8 +627,8 @@ export class ObservableSanityClient { /** * Get a Sanity API URL for the data operation and path provided * - * @param operation Data operation - * @param path Path to append + * @param operation - Data operation (eg `query`, `mutate`, `listen` or similar) + * @param path - Path to append after the operation */ getDataUrl(operation: string, path?: string): string { return dataMethods._getDataUrl(this, operation, path) @@ -1234,8 +1234,8 @@ export class SanityClient { /** * Get a Sanity API URL for the URI provided * - * @param uri URI/path to build URL for - * @param canUseCdn Whether or not to allow using the API CDN for this route + * @param uri - URI/path to build URL for + * @param canUseCdn - Whether or not to allow using the API CDN for this route */ getUrl(uri: string, canUseCdn?: boolean): string { return dataMethods._getUrl(this, uri, canUseCdn) @@ -1244,8 +1244,8 @@ export class SanityClient { /** * Get a Sanity API URL for the data operation and path provided * - * @param operation Data operation - * @param path Path to append + * @param operation - Data operation (eg `query`, `mutate`, `listen` or similar) + * @param path - Path to append after the operation */ getDataUrl(operation: string, path?: string): string { return dataMethods._getDataUrl(this, operation, path)