teableio/teable

View on GitHub
packages/openapi/src/selection/temporary-paste.ts

Summary

Maintainability
A
0 mins
Test Coverage
import type { RouteConfig } from '@asteasolutions/zod-to-openapi';
import { fieldVoSchema, recordSchema } from '@teable/core';
import { axios } from '../axios';
import { registerRoute, urlBuilder } from '../utils';
import { z } from '../zod';
import { rangesRoSchema } from './range';

export const TEMPORARY_PASTE_URL = '/table/{tableId}/selection/temporaryPaste';

export const temporaryPasteRoSchema = rangesRoSchema
  .pick({
    viewId: true,
    ranges: true,
    excludeFieldIds: true,
  })
  .extend({
    content: z.string().openapi({
      description: 'Content to paste',
      example: 'John\tDoe\tjohn.doe@example.com',
    }),
    header: z.array(fieldVoSchema).optional().openapi({
      description: 'Table header for paste operation',
      example: [],
    }),
  });

export type ITemporaryPasteRo = z.infer<typeof temporaryPasteRoSchema>;

export const temporaryPasteVoSchema = z.array(
  recordSchema.pick({
    fields: true,
  })
);

export type ITemporaryPasteVo = z.infer<typeof temporaryPasteVoSchema>;

export const temporaryPasteRoute: RouteConfig = registerRoute({
  method: 'patch',
  path: TEMPORARY_PASTE_URL,
  description: 'Paste operation for pre-filled table rows',
  request: {
    params: z.object({
      tableId: z.string(),
    }),
    body: {
      content: {
        'application/json': {
          schema: temporaryPasteRoSchema,
        },
      },
    },
  },
  responses: {
    200: {
      description: 'Paste successfully',
      content: {
        'application/json': {
          schema: temporaryPasteVoSchema,
        },
      },
    },
  },
  tags: ['selection'],
});

export const temporaryPaste = async (tableId: string, pasteRo: ITemporaryPasteRo) => {
  return axios.patch<ITemporaryPasteVo>(
    urlBuilder(TEMPORARY_PASTE_URL, {
      tableId,
    }),
    pasteRo
  );
};