fix: [frontend] Update the dictionarySearch.remote.ts function in
sveltekit to use the new API endpoints
This commit is contained in:
parent
170f851344
commit
57fd98d882
1 changed files with 27 additions and 5 deletions
|
|
@ -1,17 +1,27 @@
|
||||||
import { query, getRequestEvent } from '$app/server';
|
import { query, getRequestEvent } from '$app/server';
|
||||||
import * as v from 'valibot';
|
import * as v from 'valibot';
|
||||||
import {
|
import {
|
||||||
searchWordformsApiDictionarySearchGet,
|
searchWordformsPrefixApiDictionarySearchGet,
|
||||||
searchWordformsApiDictionaryWordformsGet
|
searchWordformsApiDictionaryWordformsGet
|
||||||
} from '../../../../client';
|
} from '../../../../client';
|
||||||
import { COOKIE_NAME_AUTH_TOKEN } from '$lib/auth';
|
import { COOKIE_NAME_AUTH_TOKEN } from '$lib/auth';
|
||||||
|
|
||||||
|
export type DictionarySearchResult = {
|
||||||
|
lemma: {
|
||||||
|
text: string;
|
||||||
|
};
|
||||||
|
senses: {
|
||||||
|
text: string;
|
||||||
|
id: string;
|
||||||
|
}[];
|
||||||
|
};
|
||||||
|
|
||||||
export const dictionarySearch = query(
|
export const dictionarySearch = query(
|
||||||
v.object({
|
v.object({
|
||||||
text: v.string(),
|
text: v.string(),
|
||||||
langCode: v.string()
|
langCode: v.string()
|
||||||
}),
|
}),
|
||||||
async ({ langCode, text }) => {
|
async ({ langCode, text }): Promise<DictionarySearchResult[]> => {
|
||||||
const { cookies } = getRequestEvent();
|
const { cookies } = getRequestEvent();
|
||||||
const trimmed = text.trim();
|
const trimmed = text.trim();
|
||||||
|
|
||||||
|
|
@ -25,14 +35,26 @@ export const dictionarySearch = query(
|
||||||
query: { lang_code: langCode, text }
|
query: { lang_code: langCode, text }
|
||||||
});
|
});
|
||||||
|
|
||||||
return data;
|
return (data ?? []).map(({ lemma, senses }) => ({
|
||||||
|
lemma: { text: lemma.headword },
|
||||||
|
senses: senses.map(({ gloss, id }) => ({
|
||||||
|
text: gloss,
|
||||||
|
id
|
||||||
|
}))
|
||||||
|
}));
|
||||||
} else {
|
} else {
|
||||||
const { data } = await searchWordformsApiDictionarySearchGet({
|
const { data } = await searchWordformsPrefixApiDictionarySearchGet({
|
||||||
headers: { Authorization: `Bearer ${cookies.get(COOKIE_NAME_AUTH_TOKEN)}` },
|
headers: { Authorization: `Bearer ${cookies.get(COOKIE_NAME_AUTH_TOKEN)}` },
|
||||||
query: { lang_code: langCode, text }
|
query: { lang_code: langCode, text }
|
||||||
});
|
});
|
||||||
|
|
||||||
return data;
|
return (data ?? []).map(({ lemma, senses }) => ({
|
||||||
|
lemma: { text: lemma.headword },
|
||||||
|
senses: senses.map(({ gloss, id }) => ({
|
||||||
|
text: gloss,
|
||||||
|
id
|
||||||
|
}))
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue