Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add back getUrl & getDataUrl #110

Merged
merged 2 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 40 additions & 0 deletions src/SanityClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,26 @@ export class ObservableSanityClient {
request<R = FIXME>(options: RawRequestOptions): Observable<R> {
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 (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)
}
}

/** @public */
Expand Down Expand Up @@ -1210,4 +1230,24 @@ export class SanityClient {
dataRequest(endpoint: string, body: unknown, options?: BaseMutationOptions): Promise<FIXME> {
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 (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)
}
}
21 changes: 21 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
})
})
})