Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
feat: cdn client (#49)
Browse files Browse the repository at this point in the history
* feat: cdn client

* update README
  • Loading branch information
ricokahler authored Jan 2, 2021
1 parent c383174 commit cc18bd1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export default createClient<Documents>({
token: '...',
// by default sanity-codegen caches responses in memory. this can be disabled if desired
// disabledCache: true,
//
// (optional) enables the usage of `apicdn.sanity.io`. this is recommended
// if you plan on using this in browsers. don't use this with preview mode
// see here: https://www.sanity.io/docs/api-cdn
// useCdn: true,
});
```

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sanity-codegen",
"version": "0.6.4",
"version": "0.7.0",
"private": true,
"description": "",
"repository": {
Expand Down
54 changes: 54 additions & 0 deletions src/client/create-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,60 @@ describe('query', () => {
]
`);
});

it('conditionally applies tokens and CDN endpoints', async () => {
const postOne = {
_createdAt: '2020-10-24T03:00:29Z',
_id: 'post-one',
_rev: 'rev',
_type: 'post',
_updatedAt: '2020-10-24T03:04:54Z',
};
const postTwo = {
_createdAt: '2020-10-24T03:00:29Z',
_id: 'post-two',
_rev: 'rev',
_type: 'post',
_updatedAt: '2020-10-24T03:04:54Z',
};
const postTwoDraft = {
_createdAt: '2020-10-24T03:00:29Z',
_id: 'drafts.post-two',
_rev: 'rev',
_type: 'post',
_updatedAt: '2020-10-24T03:04:54Z',
};

const mockFetch: any = jest.fn(() => ({
ok: true,
json: () => Promise.resolve({ result: [postOne, postTwo, postTwoDraft] }),
}));

const sanity = createClient({
projectId: 'test-project-id',
dataset: 'test-dataset',
fetch: mockFetch,
previewMode: false,
token: 'test-token',
useCdn: true,
});

const docs = await sanity.query('*');

expect(docs).toEqual([postOne, postTwo]);

expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://test-project-id.apicdn.sanity.io/v1/data/query/test-dataset?query=*",
Object {
"headers": Object {
"Accept": "application/json",
},
},
]
`);
});
});

describe('get', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/client/create-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface CreateClientOptions {
token?: string;
previewMode?: boolean;
disabledCache?: boolean;
useCdn?: boolean;
}

interface SanityResult<T> {
Expand All @@ -22,6 +23,7 @@ function createClient<Documents extends { _type: string; _id: string }>({
previewMode = false,
fetch,
disabledCache,
useCdn,
}: CreateClientOptions) {
const cache: { [key: string]: any } = {};

Expand Down Expand Up @@ -129,10 +131,12 @@ function createClient<Documents extends { _type: string; _id: string }>({

searchParams.set('query', query);
const response = await jsonFetch<SanityResult<T>>(
`https://${projectId}.api.sanity.io/v1/data/query/${dataset}?${searchParams.toString()}`,
`https://${projectId}.${
useCdn ? 'apicdn' : 'api'
}.sanity.io/v1/data/query/${dataset}?${searchParams.toString()}`,
{
// conditionally add the authorization header if the token is present
...(token && { headers: { Authorization: `Bearer ${token}` } }),
...(preview && { headers: { Authorization: `Bearer ${token}` } }),
}
);

Expand Down

0 comments on commit cc18bd1

Please sign in to comment.