gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

base.js (1937B)


      1 /*
      2 	GoToSocial
      3 	Copyright (C) GoToSocial Authors admin@gotosocial.org
      4 	SPDX-License-Identifier: AGPL-3.0-or-later
      5 
      6 	This program is free software: you can redistribute it and/or modify
      7 	it under the terms of the GNU Affero General Public License as published by
      8 	the Free Software Foundation, either version 3 of the License, or
      9 	(at your option) any later version.
     10 
     11 	This program is distributed in the hope that it will be useful,
     12 	but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 	GNU Affero General Public License for more details.
     15 
     16 	You should have received a copy of the GNU Affero General Public License
     17 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
     18 */
     19 
     20 "use strict";
     21 
     22 const { createApi, fetchBaseQuery } = require("@reduxjs/toolkit/query/react");
     23 const { serialize: serializeForm } = require("object-to-formdata");
     24 
     25 function instanceBasedQuery(args, api, extraOptions) {
     26 	const state = api.getState();
     27 	const { instance, token } = state.oauth;
     28 
     29 	if (args.baseUrl == undefined) {
     30 		args.baseUrl = instance;
     31 	}
     32 
     33 	if (args.discardEmpty) {
     34 		if (args.body == undefined || Object.keys(args.body).length == 0) {
     35 			return { data: null };
     36 		}
     37 		delete args.discardEmpty;
     38 	}
     39 
     40 	if (args.asForm) {
     41 		delete args.asForm;
     42 		args.body = serializeForm(args.body, {
     43 			indices: true, // Array indices, for profile fields
     44 		});
     45 	}
     46 
     47 	return fetchBaseQuery({
     48 		baseUrl: args.baseUrl,
     49 		prepareHeaders: (headers) => {
     50 			if (token != undefined) {
     51 				headers.set('Authorization', token);
     52 			}
     53 			headers.set("Accept", "application/json");
     54 			return headers;
     55 		},
     56 	})(args, api, extraOptions);
     57 }
     58 
     59 module.exports = createApi({
     60 	reducerPath: "api",
     61 	baseQuery: instanceBasedQuery,
     62 	tagTypes: ["Auth", "Emoji", "Reports", "Account"],
     63 	endpoints: (build) => ({
     64 		instance: build.query({
     65 			query: () => ({
     66 				url: `/api/v1/instance`
     67 			})
     68 		})
     69 	})
     70 });