apiv3-client.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // eslint-disable-next-line no-restricted-imports
  2. import { AxiosResponse } from 'axios';
  3. import * as urljoin from 'url-join';
  4. // eslint-disable-next-line no-restricted-imports
  5. import { toArrayIfNot } from '~/utils/array-utils';
  6. import axios from '~/utils/axios';
  7. import loggerFactory from '~/utils/logger';
  8. const apiv3Root = '/_api/v3';
  9. const logger = loggerFactory('growi:apiv3');
  10. // get csrf token from body element
  11. const body = document.querySelector('body');
  12. const csrfToken = body?.dataset.csrftoken;
  13. type ParamWithCsrfKey = {
  14. _csrf: string,
  15. }
  16. const apiv3ErrorHandler = (_err) => {
  17. // extract api errors from general 400 err
  18. const err = _err.response ? _err.response.data.errors : _err;
  19. const errs = toArrayIfNot(err);
  20. for (const err of errs) {
  21. logger.error(err.message);
  22. }
  23. return errs;
  24. };
  25. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  26. export async function apiv3Request<T = any>(method: string, path: string, params: unknown): Promise<AxiosResponse<T>> {
  27. try {
  28. const res = await axios[method](urljoin(apiv3Root, path), params);
  29. return res;
  30. }
  31. catch (err) {
  32. const errors = apiv3ErrorHandler(err);
  33. throw errors;
  34. }
  35. }
  36. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  37. export async function apiv3Get<T = any>(path: string, params: unknown = {}): Promise<AxiosResponse<T>> {
  38. return apiv3Request('get', path, { params });
  39. }
  40. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  41. export async function apiv3Post<T = any>(path: string, params: any & ParamWithCsrfKey = {}): Promise<AxiosResponse<T>> {
  42. if (params._csrf == null) {
  43. params._csrf = csrfToken;
  44. }
  45. return apiv3Request('post', path, params);
  46. }
  47. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  48. export async function apiv3PostForm<T = any>(path: string, formData: FormData): Promise<AxiosResponse<T>> {
  49. if (formData.get('_csrf') == null && csrfToken != null) {
  50. formData.append('_csrf', csrfToken);
  51. }
  52. return apiv3Post<T>(path, formData);
  53. }
  54. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  55. export async function apiv3Put<T = any>(path: string, params: any & ParamWithCsrfKey = {}): Promise<AxiosResponse<T>> {
  56. if (params._csrf == null) {
  57. params._csrf = csrfToken;
  58. }
  59. return apiv3Request('put', path, params);
  60. }
  61. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  62. export async function apiv3Delete<T = any>(path: string, params: any & ParamWithCsrfKey = {}): Promise<AxiosResponse<T>> {
  63. if (params._csrf == null) {
  64. params._csrf = csrfToken;
  65. }
  66. return apiv3Request('delete', path, { params });
  67. }