Skip to content

Commit

Permalink
[DEV-12120] Implement a new Stories API to fetch story by slug
Browse files Browse the repository at this point in the history
The difference with `search()` is that this one can find relocated
stories by its former slug values, even after slug has been changed.

The consumer code is expected to perform a redirect when a story slug doesn't
match the one coming from the request.
  • Loading branch information
e1himself committed Dec 7, 2023
1 parent 9398160 commit 86e1cb9
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/endpoints/Stories/Client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DeferredJobsApiClient } from '../../api';
import { ApiError, HttpCodes } from '../../http';
import { routing } from '../../routing';
import type { ExtendedStory, Story } from '../../types';
import type { ExtendedStory, Query, Story } from '../../types';
import { SortOrder } from '../../types';

import type {
Expand Down Expand Up @@ -174,6 +174,27 @@ export class Client {
return story;
}

async getBySlug<Options extends IncludeOptions & { formats?: Formats; query?: Query }>(
slug: Story['slug'],
options?: Exactly<Options, IncludeOptions & { formats?: Formats; query?: Query }>,
): Promise<ExtendedStory & InferExtraFields<Options>> {
if (slug.includes('/') || slug.includes('\\')) {
throw new Error('Story slugs cannot contain slashes.');
}

const { include, query, formats } = options ?? {};

const { story } = await this.apiClient.post<{
story: ExtendedStory & InferExtraFields<Options>;
}>(`${routing.storiesUrl}/by-slug/${slug}`, {
headers: acceptedFormatsHeader(formats),
query: { include },
payload: { query },
});

return story;
}

async create<Options extends IncludeOptions & { formats?: Formats }>(
payload: CreateRequest,
options?: Exactly<Options, IncludeOptions & { formats?: Formats }>,
Expand Down

0 comments on commit 86e1cb9

Please sign in to comment.