@hywax/cms 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/.nuxt/cms/http-codes.ts +8 -0
  2. package/.nuxt/cms/index.ts +2 -1
  3. package/.nuxt/cms/uplora-image.ts +8 -0
  4. package/cli/commands/make/component.mjs +75 -0
  5. package/cli/commands/make/index.mjs +12 -0
  6. package/cli/index.mjs +15 -0
  7. package/cli/package.json +13 -0
  8. package/cli/templates.mjs +101 -0
  9. package/cli/utils.mjs +31 -0
  10. package/dist/module.d.mts +27 -3
  11. package/dist/module.json +1 -1
  12. package/dist/module.mjs +122 -31
  13. package/dist/runtime/components/ButtonClear.vue +1 -2
  14. package/dist/runtime/components/UploraImage.vue +108 -0
  15. package/dist/runtime/components/UploraImage.vue.d.ts +34 -0
  16. package/dist/runtime/components/prose/UploraImage.vue +1 -2
  17. package/dist/runtime/composables/useApi.d.ts +14 -0
  18. package/dist/runtime/composables/useApi.js +16 -0
  19. package/dist/runtime/composables/useSeoStats.d.ts +12 -0
  20. package/dist/runtime/composables/useSeoStats.js +44 -0
  21. package/dist/runtime/plugins/api.d.ts +6 -0
  22. package/dist/runtime/plugins/api.js +23 -0
  23. package/dist/runtime/server/errors/InternalHttpError.d.ts +4 -0
  24. package/dist/runtime/server/errors/InternalHttpError.js +6 -0
  25. package/dist/runtime/server/errors/TimeoutError.d.ts +2 -0
  26. package/dist/runtime/server/errors/TimeoutError.js +2 -0
  27. package/dist/runtime/server/errors/index.d.ts +2 -0
  28. package/dist/runtime/server/errors/index.js +2 -0
  29. package/dist/runtime/server/types/errors.d.ts +8 -0
  30. package/dist/runtime/server/types/errors.js +0 -0
  31. package/dist/runtime/server/types/index.d.ts +1 -0
  32. package/dist/runtime/server/types/index.js +1 -0
  33. package/dist/runtime/server/utils/errors.d.ts +8 -0
  34. package/dist/runtime/server/utils/errors.js +57 -0
  35. package/dist/runtime/server/utils/httpHandler.d.ts +10 -0
  36. package/dist/runtime/server/utils/httpHandler.js +15 -0
  37. package/dist/runtime/server/utils/pagination.d.ts +11 -0
  38. package/dist/runtime/server/utils/pagination.js +12 -0
  39. package/dist/runtime/server/utils/timeout.d.ts +7 -0
  40. package/dist/runtime/server/utils/timeout.js +9 -0
  41. package/dist/runtime/server/utils/validation.d.ts +3 -0
  42. package/dist/runtime/server/utils/validation.js +26 -0
  43. package/dist/runtime/types/image.d.ts +48 -0
  44. package/dist/runtime/types/image.js +0 -0
  45. package/dist/runtime/types/index.d.ts +4 -0
  46. package/dist/runtime/types/index.js +4 -0
  47. package/dist/runtime/types/query.d.ts +20 -0
  48. package/dist/runtime/types/query.js +0 -0
  49. package/dist/runtime/types/seo.d.ts +4 -0
  50. package/dist/runtime/types/seo.js +0 -0
  51. package/dist/runtime/types/utils.d.ts +4 -1
  52. package/dist/runtime/utils/avatar.d.ts +1 -0
  53. package/dist/runtime/utils/avatar.js +9 -0
  54. package/dist/runtime/utils/dictionaries.d.ts +4 -0
  55. package/dist/runtime/utils/dictionaries.js +6 -0
  56. package/dist/runtime/utils/image.d.ts +30 -0
  57. package/dist/runtime/utils/image.js +81 -0
  58. package/dist/runtime/utils/index.d.ts +4 -1
  59. package/dist/runtime/utils/index.js +4 -0
  60. package/dist/runtime/utils/slugify.d.ts +1 -0
  61. package/dist/runtime/utils/slugify.js +12 -0
  62. package/dist/types.d.mts +6 -2
  63. package/package.json +17 -3
@@ -0,0 +1,34 @@
1
+ import type { AppConfig } from '@nuxt/schema';
2
+ import type { ImgHTMLAttributes } from 'vue';
3
+ import type { ComponentConfig, ImageFormat, ImageSize } from '../types';
4
+ import theme from '#build/cms/uplora-image';
5
+ export type UploraImage = ComponentConfig<typeof theme, AppConfig, 'uploraImage'>;
6
+ export interface UploraImageProps {
7
+ id: string;
8
+ alt: string;
9
+ formats?: ImageFormat[];
10
+ sizes?: ImageSize[];
11
+ lqip?: string;
12
+ loading?: 'lazy' | 'eager';
13
+ preload?: boolean | {
14
+ fetchPriority: 'auto' | 'high' | 'low';
15
+ };
16
+ nonce?: string;
17
+ imgAttrs?: ImgHTMLAttributes;
18
+ class?: any;
19
+ ui?: UploraImage['slots'];
20
+ }
21
+ export interface UploraImageEmits {
22
+ load: [Event];
23
+ error: [string | Event];
24
+ }
25
+ declare const _default: import("vue").DefineComponent<UploraImageProps, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
26
+ error: (args_0: string | Event) => any;
27
+ load: (args_0: Event) => any;
28
+ }, string, import("vue").PublicProps, Readonly<UploraImageProps> & Readonly<{
29
+ onError?: ((args_0: string | Event) => any) | undefined;
30
+ onLoad?: ((args_0: Event) => any) | undefined;
31
+ }>, {
32
+ loading: "lazy" | "eager";
33
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
34
+ export default _default;
@@ -6,8 +6,7 @@
6
6
 
7
7
  <script>
8
8
  import theme from "#build/cms/prose/uplora-image";
9
- import { useAppConfig } from "#imports";
10
- import { computed } from "vue";
9
+ import { computed, useAppConfig } from "#imports";
11
10
  import { tv } from "../../utils/tv";
12
11
  </script>
13
12
 
@@ -0,0 +1,14 @@
1
+ import type { AvailableRouterMethod, NitroFetchRequest } from 'nitropack';
2
+ import type { AsyncData, FetchResult, UseFetchOptions } from 'nuxt/app';
3
+ import type { DefaultAsyncDataValue } from 'nuxt/app/defaults';
4
+ import type { FetchError } from 'ofetch';
5
+ import type { Ref } from 'vue';
6
+ type PickFrom<T, K extends Array<string>> = T extends Array<any> ? T : T extends Record<string, any> ? keyof T extends K[number] ? T : K[number] extends never ? T : Pick<T, K[number]> : T;
7
+ type KeysOf<T> = Array<T extends T ? (keyof T extends string ? keyof T : never) : never>;
8
+ /**
9
+ * На данный момент нет возможности использовать `useFetch` со своим $fetch,
10
+ * поэтому приходится использовать костыль.
11
+ * https://github.com/nuxt/nuxt/issues/14736
12
+ */
13
+ export declare function useApi<ResT = void, ErrorT = FetchError, ReqT extends NitroFetchRequest = NitroFetchRequest, Method extends AvailableRouterMethod<ReqT> = ResT extends void ? 'get' extends AvailableRouterMethod<ReqT> ? 'get' : AvailableRouterMethod<ReqT> : AvailableRouterMethod<ReqT>, _ResT = ResT extends void ? FetchResult<ReqT, Method> : ResT, DataT = _ResT, PickKeys extends KeysOf<DataT> = KeysOf<DataT>, DefaultT = DefaultAsyncDataValue>(request: Ref<ReqT> | ReqT | (() => ReqT), opts?: UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | DefaultAsyncDataValue>;
14
+ export {};
@@ -0,0 +1,16 @@
1
+ import { useFetch, useNuxtApp } from "#imports";
2
+ import { defu } from "defu";
3
+ export function useApi(request, opts) {
4
+ const $api = useNuxtApp().$api;
5
+ return useFetch(
6
+ request,
7
+ defu(
8
+ {
9
+ $fetch: $api
10
+ },
11
+ {
12
+ ...opts
13
+ }
14
+ )
15
+ );
16
+ }
@@ -0,0 +1,12 @@
1
+ import type { ComputedRef, MaybeRefOrGetter } from 'vue';
2
+ import type { SEO } from '../types';
3
+ interface SEOStats {
4
+ progress: number;
5
+ color: 'error' | 'warning' | 'success';
6
+ }
7
+ interface UseSEOStatsReturn {
8
+ title: ComputedRef<SEOStats>;
9
+ description: ComputedRef<SEOStats>;
10
+ }
11
+ export declare function useSEOStats(options: MaybeRefOrGetter<SEO>): UseSEOStatsReturn;
12
+ export {};
@@ -0,0 +1,44 @@
1
+ import { computed, toValue } from "#imports";
2
+ function computeScore(length, recommendedLength, maxLength) {
3
+ if (length === 0) {
4
+ return 0;
5
+ }
6
+ if (length < recommendedLength) {
7
+ return length / recommendedLength * 100;
8
+ }
9
+ if (length > maxLength) {
10
+ return Math.max(0, 100 - (length - maxLength) / maxLength * 100);
11
+ }
12
+ return 100;
13
+ }
14
+ function computeColor(score) {
15
+ if (score < 30) {
16
+ return "error";
17
+ }
18
+ if (score < 70) {
19
+ return "warning";
20
+ }
21
+ return "success";
22
+ }
23
+ export function useSEOStats(options) {
24
+ const title = computed(() => {
25
+ const value = toValue(options);
26
+ const score = computeScore(value.title.length, 45, 60);
27
+ return {
28
+ progress: Math.round(score),
29
+ color: computeColor(score)
30
+ };
31
+ });
32
+ const description = computed(() => {
33
+ const value = toValue(options);
34
+ const score = computeScore(value.description.length, 130, 160);
35
+ return {
36
+ progress: Math.round(score),
37
+ color: computeColor(score)
38
+ };
39
+ });
40
+ return {
41
+ title,
42
+ description
43
+ };
44
+ }
@@ -0,0 +1,6 @@
1
+ declare const _default: import("#app").Plugin<{
2
+ api: import("nitropack/types").$Fetch<unknown, import("nitropack/types").NitroFetchRequest>;
3
+ }> & import("#app").ObjectPlugin<{
4
+ api: import("nitropack/types").$Fetch<unknown, import("nitropack/types").NitroFetchRequest>;
5
+ }>;
6
+ export default _default;
@@ -0,0 +1,23 @@
1
+ import { defineNuxtPlugin, useAppConfig, useToast } from "#imports";
2
+ export default defineNuxtPlugin({
3
+ name: "cms-api",
4
+ async setup() {
5
+ const toast = useToast();
6
+ const appConfig = useAppConfig();
7
+ const api = $fetch.create({
8
+ async onResponseError({ response }) {
9
+ toast.add({
10
+ title: "\u041E\u0448\u0438\u0431\u043A\u0430",
11
+ description: response._data.message ?? "\u0427\u0442\u043E-\u0442\u043E \u043F\u043E\u0448\u043B\u043E \u043D\u0435 \u0442\u0430\u043A",
12
+ color: "error",
13
+ icon: appConfig.ui.icons.close
14
+ });
15
+ }
16
+ });
17
+ return {
18
+ provide: {
19
+ api
20
+ }
21
+ };
22
+ }
23
+ });
@@ -0,0 +1,4 @@
1
+ export declare class InternalHttpError extends Error {
2
+ httpCodeString: string;
3
+ constructor(httpCodeString: string);
4
+ }
@@ -0,0 +1,6 @@
1
+ export class InternalHttpError extends Error {
2
+ constructor(httpCodeString) {
3
+ super("Internal Error");
4
+ this.httpCodeString = httpCodeString;
5
+ }
6
+ }
@@ -0,0 +1,2 @@
1
+ export declare class TimeoutError extends Error {
2
+ }
@@ -0,0 +1,2 @@
1
+ export class TimeoutError extends Error {
2
+ }
@@ -0,0 +1,2 @@
1
+ export * from './InternalHttpError';
2
+ export * from './TimeoutError';
@@ -0,0 +1,2 @@
1
+ export * from "./InternalHttpError.js";
2
+ export * from "./TimeoutError.js";
@@ -0,0 +1,8 @@
1
+ export type ErrorMapCodes = Record<string, string>;
2
+ export interface CustomError {
3
+ statusCode: number;
4
+ message: string;
5
+ data?: {
6
+ [key: string]: unknown;
7
+ };
8
+ }
File without changes
@@ -0,0 +1 @@
1
+ export * from './errors';
@@ -0,0 +1 @@
1
+ export * from "./errors.js";
@@ -0,0 +1,8 @@
1
+ import type { CustomError, ErrorMapCodes } from '../types';
2
+ import { H3Error } from 'h3';
3
+ import { InternalHttpError } from '../errors';
4
+ export declare function parseErrorString(errorString: string): CustomError;
5
+ export declare function errorServerResolver(error: unknown, errorMap?: ErrorMapCodes): H3Error<{
6
+ [key: string]: unknown;
7
+ }>;
8
+ export declare function createServerError(errorString: string): InternalHttpError;
@@ -0,0 +1,57 @@
1
+ import { HTTP_CODE_BAD_REQUEST, HTTP_CODE_INTERNAL_SERVER_ERROR, HTTP_CODE_REQUEST_TIMEOUT, HTTP_CODE_UNAUTHORIZED } from "#cms/http-codes";
2
+ import { consola } from "consola";
3
+ import { defu } from "defu";
4
+ import { createError, H3Error } from "h3";
5
+ import { z } from "zod";
6
+ import { InternalHttpError, TimeoutError } from "../errors/index.js";
7
+ const defaultHttpCodes = {
8
+ DEFAULT: HTTP_CODE_INTERNAL_SERVER_ERROR,
9
+ ZOD: HTTP_CODE_BAD_REQUEST,
10
+ DB: HTTP_CODE_INTERNAL_SERVER_ERROR,
11
+ ERROR_UNAUTHORIZED: HTTP_CODE_UNAUTHORIZED,
12
+ TIMEOUT: HTTP_CODE_REQUEST_TIMEOUT
13
+ };
14
+ function extractHttpCodeString(error, errorMap = {}) {
15
+ errorMap = defu(errorMap, defaultHttpCodes);
16
+ if (error instanceof InternalHttpError) {
17
+ return error.httpCodeString;
18
+ } else if (error instanceof z.ZodError) {
19
+ return errorMap.ZOD;
20
+ } else if (error instanceof TimeoutError) {
21
+ return errorMap.TIMEOUT;
22
+ } else if (error instanceof H3Error && error.cause instanceof InternalHttpError) {
23
+ return error.cause.httpCodeString;
24
+ } else if (error instanceof Error) {
25
+ if ("query" in error && "params" in error && Array.isArray(error.params)) {
26
+ const errorMaybeCode2 = `DB_${error.cause?.code}`;
27
+ return errorMap[errorMaybeCode2] ?? errorMap.DB;
28
+ }
29
+ const normalizedMessage = error.message.trim().replaceAll(" ", "_").toUpperCase();
30
+ const errorMaybeCode = `ERROR_${normalizedMessage}`;
31
+ return errorMap[errorMaybeCode] ?? errorMap.DEFAULT;
32
+ }
33
+ return errorMap.DEFAULT;
34
+ }
35
+ export function parseErrorString(errorString) {
36
+ const [code, message] = errorString.split(":");
37
+ if (!code || !message) {
38
+ throw new Error("Invalid error string");
39
+ }
40
+ return {
41
+ statusCode: Number.parseInt(code, 10),
42
+ message: message.trim()
43
+ };
44
+ }
45
+ export function errorServerResolver(error, errorMap) {
46
+ const httpCodeString = extractHttpCodeString(error, errorMap);
47
+ const parsedHttpCode = parseErrorString(httpCodeString);
48
+ consola.error({
49
+ code: parsedHttpCode.statusCode,
50
+ message: parsedHttpCode.message,
51
+ error
52
+ });
53
+ return createError(parsedHttpCode);
54
+ }
55
+ export function createServerError(errorString) {
56
+ return new InternalHttpError(errorString);
57
+ }
@@ -0,0 +1,10 @@
1
+ import type { EventHandler, EventHandlerRequest, EventHandlerResponse } from 'h3';
2
+ import type { ErrorMapCodes } from '../types';
3
+ export interface EventHandlerOptions {
4
+ errorMap?: ErrorMapCodes;
5
+ }
6
+ /**
7
+ * Функция для определения обработчика HTTP-запросов.
8
+ * Обрабатывает, H3-обработчик и ловит, обрабатывает ошибки.
9
+ */
10
+ export declare function defineHttpHandler<Request extends EventHandlerRequest, Response extends EventHandlerResponse>(handler: EventHandler<Request, Response | Promise<Response>>, options?: EventHandlerOptions): EventHandler<Request, Promise<Response>>;
@@ -0,0 +1,15 @@
1
+ import { eventHandler } from "h3";
2
+ import { errorServerResolver } from "./errors.js";
3
+ export function defineHttpHandler(handler, options) {
4
+ return eventHandler(async (event) => {
5
+ try {
6
+ return await handler(event);
7
+ } catch (e) {
8
+ let error = e;
9
+ if (error.cause?.data instanceof Error) {
10
+ error = error.cause?.data;
11
+ }
12
+ throw errorServerResolver(error, options?.errorMap);
13
+ }
14
+ });
15
+ }
@@ -0,0 +1,11 @@
1
+ import type { KeysMatching, Pagination } from '../../types';
2
+ export declare function extractPaginationData<T extends {
3
+ total: number;
4
+ }>(data: T[]): {
5
+ items: Omit<T, 'total'>[];
6
+ pagination: Pagination;
7
+ };
8
+ export declare function extractPaginationData<T extends object, K extends KeysMatching<T, number>>(data: T[], key: K): {
9
+ items: Omit<T, K>[];
10
+ pagination: Pagination;
11
+ };
@@ -0,0 +1,12 @@
1
+ export function extractPaginationData(data, key) {
2
+ if (!data.length) {
3
+ return { items: [], pagination: { total: 0 } };
4
+ }
5
+ const totalKey = key ?? "total";
6
+ const total = Number(data[0]?.[totalKey]) || 0;
7
+ const items = data.map(({ [totalKey]: _, ...item }) => item);
8
+ return {
9
+ items,
10
+ pagination: { total }
11
+ };
12
+ }
@@ -0,0 +1,7 @@
1
+ import { TimeoutError } from '../errors/TimeoutError';
2
+ interface TryWithTimeoutOptions {
3
+ timeout?: number;
4
+ timeoutMessage?: string;
5
+ }
6
+ export declare function tryWithTimeout<T>(promise: Promise<T>, options: TryWithTimeoutOptions): Promise<T | TimeoutError>;
7
+ export {};
@@ -0,0 +1,9 @@
1
+ import { TimeoutError } from "../errors/TimeoutError.js";
2
+ export function tryWithTimeout(promise, options) {
3
+ return Promise.race([
4
+ promise,
5
+ new Promise(
6
+ (_, reject) => setTimeout(() => reject(new TimeoutError(options?.timeoutMessage)), options?.timeout ?? 1e3)
7
+ )
8
+ ]);
9
+ }
@@ -0,0 +1,3 @@
1
+ import type { PaginationQuery, PaginationQueryRaw, SortQuery, SortQueryRaw } from '../../types';
2
+ export declare function getValidatedSort<T extends string[]>(query: SortQueryRaw, availableColumns: readonly [...T]): SortQuery<T[number]>;
3
+ export declare function getValidatedPagination(query: PaginationQueryRaw): PaginationQuery;
@@ -0,0 +1,26 @@
1
+ import { HTTP_CODE_BAD_REQUEST } from "#cms/http-codes";
2
+ import { InternalHttpError } from "../errors/InternalHttpError.js";
3
+ export function getValidatedSort(query, availableColumns) {
4
+ if (!query.sortColumn || !availableColumns.includes(query.sortColumn)) {
5
+ throw new InternalHttpError(HTTP_CODE_BAD_REQUEST);
6
+ }
7
+ const sortType = query.sortType === "asc" ? "asc" : "desc";
8
+ const sortColumn = query.sortColumn;
9
+ return {
10
+ sortType,
11
+ sortColumn
12
+ };
13
+ }
14
+ export function getValidatedPagination(query) {
15
+ const perPage = Number(query.perPage);
16
+ if (!Number.isInteger(perPage) || perPage < 1 || perPage > 100) {
17
+ throw new InternalHttpError(HTTP_CODE_BAD_REQUEST);
18
+ }
19
+ const page = Number(query.page) || 1;
20
+ const pageOffset = (page - 1) * perPage;
21
+ return {
22
+ page,
23
+ perPage,
24
+ pageOffset
25
+ };
26
+ }
@@ -0,0 +1,48 @@
1
+ import type { ImageExtension, ImageMimeType } from '@uplora/formats';
2
+ export type ImageFormat = ImageExtension;
3
+ /**
4
+ * Размер изображения
5
+ */
6
+ export interface ImageSize {
7
+ /**
8
+ * Ширина
9
+ *
10
+ * @example 1024
11
+ */
12
+ width?: number;
13
+ /**
14
+ * Высота
15
+ *
16
+ * @example 1024
17
+ */
18
+ height?: number;
19
+ /**
20
+ * Описание размера
21
+ *
22
+ * @example 2x, 512px
23
+ */
24
+ descriptor: string;
25
+ }
26
+ /**
27
+ * Источник изображения
28
+ */
29
+ export interface ImageSource {
30
+ /**
31
+ * Image URL
32
+ *
33
+ * @example https://uplora.ru/fgu1yd2ncj05iogcooz31u55
34
+ */
35
+ img: string;
36
+ /**
37
+ * Image srcset
38
+ *
39
+ * @example https://uplora.ru/fgu1yd2ncj05iogcooz31u55/-/format/jpg 1x, https://uplora.ru/fgu1yd2ncj05iogcooz31u55/-/format/jpg 2x
40
+ */
41
+ srcset?: string;
42
+ /**
43
+ * Image mime type
44
+ *
45
+ * @example image/jpeg
46
+ */
47
+ type?: ImageMimeType;
48
+ }
File without changes
@@ -1,2 +1,6 @@
1
1
  export * from '../components/ButtonClear.vue';
2
+ export * from '../components/UploraImage.vue';
3
+ export * from './image';
4
+ export * from './query';
5
+ export * from './seo';
2
6
  export * from './utils';
@@ -1,2 +1,6 @@
1
1
  export * from "../components/ButtonClear.vue";
2
+ export * from "../components/UploraImage.vue";
3
+ export * from "./image.js";
4
+ export * from "./query.js";
5
+ export * from "./seo.js";
2
6
  export * from "./utils.js";
@@ -0,0 +1,20 @@
1
+ export interface SortQueryRaw {
2
+ sortColumn?: string;
3
+ sortType?: string;
4
+ }
5
+ export interface SortQuery<K = string> {
6
+ sortColumn: K;
7
+ sortType: 'asc' | 'desc';
8
+ }
9
+ export interface Pagination {
10
+ total: number;
11
+ }
12
+ export interface PaginationQueryRaw {
13
+ page?: string;
14
+ perPage?: string;
15
+ }
16
+ export interface PaginationQuery {
17
+ page: number;
18
+ perPage: number;
19
+ pageOffset: number;
20
+ }
File without changes
@@ -0,0 +1,4 @@
1
+ export interface SEO {
2
+ title: string;
3
+ description: string;
4
+ }
File without changes
@@ -1,3 +1,6 @@
1
1
  import type { AcceptableValue as _AcceptableValue } from 'reka-ui';
2
- export type AcceptableValue = Exclude<_AcceptableValue, Record<string, any>>;
3
2
  export * from './tv';
3
+ export type AcceptableValue = Exclude<_AcceptableValue, Record<string, any>>;
4
+ export type KeysMatching<T, V> = {
5
+ [K in keyof T]: T[K] extends V ? K : never;
6
+ }[keyof T];
@@ -0,0 +1 @@
1
+ export declare function createAvatarByName(name?: string): string;
@@ -0,0 +1,9 @@
1
+ import { initials } from "@dicebear/collection";
2
+ import { createAvatar } from "@dicebear/core";
3
+ export function createAvatarByName(name = "Anonymous") {
4
+ const avatar = createAvatar(initials, {
5
+ seed: name,
6
+ backgroundType: ["gradientLinear"]
7
+ });
8
+ return avatar.toDataUri();
9
+ }
@@ -0,0 +1,4 @@
1
+ export declare function labelsToDictionary<T extends Record<any, string>>(labels: T): Array<{
2
+ value: T[keyof T];
3
+ label: string;
4
+ }>;
@@ -0,0 +1,6 @@
1
+ export function labelsToDictionary(labels) {
2
+ return Object.entries(labels).map(([value, label]) => ({
3
+ value,
4
+ label
5
+ }));
6
+ }
@@ -0,0 +1,30 @@
1
+ import type { Filters } from '@uplora/serializer';
2
+ import type { ImageFormat, ImageSize, ImageSource } from '../types';
3
+ /**
4
+ * Создает функцию для получения URL изображения из Uplora
5
+ */
6
+ export declare function createUploraImageResolver(): (id: string, filters?: Filters) => string;
7
+ export interface BuildUploraImageOptions {
8
+ id: string;
9
+ formats?: ImageFormat[];
10
+ sizes?: ImageSize[];
11
+ }
12
+ export interface BuildUploraImageReturn {
13
+ img: string;
14
+ original: string;
15
+ sources: ImageSource[];
16
+ srcset?: string;
17
+ }
18
+ /**
19
+ * Создает URL изображения из Uplora
20
+ */
21
+ export declare function buildUploraImage(options: BuildUploraImageOptions): BuildUploraImageReturn;
22
+ export interface GenerateImageSizesOptions {
23
+ width: number;
24
+ minWidth?: number;
25
+ step?: number;
26
+ }
27
+ /**
28
+ * Генерирует размеры изображения
29
+ */
30
+ export declare function generateImageSizes(options: GenerateImageSizesOptions): ImageSize[];
@@ -0,0 +1,81 @@
1
+ import { useRuntimeConfig } from "#imports";
2
+ import { imagesExtensionsToMimeTypes } from "@uplora/formats";
3
+ import { serialize } from "@uplora/serializer";
4
+ export function createUploraImageResolver() {
5
+ const runtimeConfig = useRuntimeConfig();
6
+ const { fluxorUrl } = runtimeConfig.public;
7
+ return (id, filters) => {
8
+ const serializeFilters = filters ? serialize(filters) : "";
9
+ return `${fluxorUrl}/${id}${serializeFilters ? `/${serializeFilters}` : ""}`;
10
+ };
11
+ }
12
+ export function buildUploraImage(options) {
13
+ const resolver = createUploraImageResolver();
14
+ const original = resolver(options.id);
15
+ const sizes = options.sizes ?? [{ descriptor: "1x" }];
16
+ const sources = [];
17
+ function makeSrcset(format) {
18
+ if (sizes && sizes.length > 1) {
19
+ return sizes.map((size) => `${resolver(options.id, { resize: { width: size.width, height: size.height }, format })} ${size.descriptor}`).join(", ");
20
+ }
21
+ return null;
22
+ }
23
+ if (options.formats) {
24
+ for (const format of options.formats) {
25
+ const srcset = makeSrcset(format);
26
+ sources.push({
27
+ img: resolver(options.id, {
28
+ resize: {
29
+ width: sizes[0]?.width,
30
+ height: sizes[0]?.height
31
+ },
32
+ format
33
+ }),
34
+ ...srcset ? { srcset } : {},
35
+ type: imagesExtensionsToMimeTypes[format]
36
+ });
37
+ }
38
+ } else {
39
+ const srcset = makeSrcset();
40
+ sources.push({
41
+ img: resolver(options.id, {
42
+ resize: {
43
+ width: sizes[0]?.width,
44
+ height: sizes[0]?.height
45
+ }
46
+ }),
47
+ ...srcset ? { srcset } : {}
48
+ });
49
+ }
50
+ return {
51
+ img: original,
52
+ original,
53
+ ...sources.length > 0 ? {
54
+ img: sources[0]?.img,
55
+ srcset: sources[0]?.srcset
56
+ } : {},
57
+ sources
58
+ };
59
+ }
60
+ export function generateImageSizes(options) {
61
+ const { width, minWidth = 640, step = 0.7 } = options;
62
+ const sizes = [];
63
+ let currentWidth = width;
64
+ while (true) {
65
+ const normalizedWidth = Math.round(currentWidth);
66
+ sizes.push({
67
+ width: normalizedWidth,
68
+ descriptor: `${Math.round(normalizedWidth * step)}w`
69
+ });
70
+ currentWidth *= step;
71
+ if (currentWidth < minWidth) {
72
+ const normalizedWidth2 = Math.round(currentWidth);
73
+ sizes.push({
74
+ width: normalizedWidth2,
75
+ descriptor: `${Math.round(normalizedWidth2 * step)}w`
76
+ });
77
+ break;
78
+ }
79
+ }
80
+ return sizes;
81
+ }
@@ -1 +1,4 @@
1
- export {};
1
+ export * from './avatar';
2
+ export * from './dictionaries';
3
+ export * from './image';
4
+ export * from './slugify';
@@ -0,0 +1,4 @@
1
+ export * from "./avatar.js";
2
+ export * from "./dictionaries.js";
3
+ export * from "./image.js";
4
+ export * from "./slugify.js";