mirror of
https://github.com/lux-tel/websites.git
synced 2026-10-30 20:31:36 +00:00
36 lines
881 B
TypeScript
36 lines
881 B
TypeScript
import { groq } from 'next-sanity';
|
|
import type { SessionSanityClient } from '../lib/client';
|
|
import logger from '../lib/logger';
|
|
import type { AuthorSchemaType } from '../schemas/author';
|
|
|
|
type QUERY_GET_AUTHORS_RETURN_TYPE = Array<AuthorSchemaType>;
|
|
const QUERY_GET_AUTHORS_WITH_ID = groq`*[_type == 'author' && _id == $id]`;
|
|
|
|
export async function getAuthorById({ client, id }: { client: SessionSanityClient; id: string }) {
|
|
if (!id || id.length === 0) {
|
|
logger.warn('No id provided, returning null');
|
|
return null;
|
|
}
|
|
|
|
const [err, result] = await client.nextFetch<QUERY_GET_AUTHORS_RETURN_TYPE>({
|
|
query: QUERY_GET_AUTHORS_WITH_ID,
|
|
params: {
|
|
id: id,
|
|
},
|
|
});
|
|
|
|
if (err) {
|
|
logger.error(err);
|
|
return null;
|
|
}
|
|
|
|
const page = result[0];
|
|
|
|
if (!page) {
|
|
logger.info(`No author found for id ${id}`);
|
|
return null;
|
|
}
|
|
|
|
return page;
|
|
}
|