Spaces:
Running
Running
| var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | |
| function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | |
| return new (P || (P = Promise))(function (resolve, reject) { | |
| function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | |
| function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | |
| function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | |
| step((generator = generator.apply(thisArg, _arguments || [])).next()); | |
| }); | |
| }; | |
| import { FunctionsClient } from '@supabase/functions-js'; | |
| import { PostgrestClient, } from '@supabase/postgrest-js'; | |
| import { RealtimeClient, } from '@supabase/realtime-js'; | |
| import { StorageClient as SupabaseStorageClient } from '@supabase/storage-js'; | |
| import { DEFAULT_HEADERS } from './lib/constants'; | |
| import { fetchWithAuth } from './lib/fetch'; | |
| import { stripTrailingSlash, applySettingDefaults } from './lib/helpers'; | |
| import { SupabaseAuthClient } from './lib/SupabaseAuthClient'; | |
| const DEFAULT_GLOBAL_OPTIONS = { | |
| headers: DEFAULT_HEADERS, | |
| }; | |
| const DEFAULT_DB_OPTIONS = { | |
| schema: 'public', | |
| }; | |
| const DEFAULT_AUTH_OPTIONS = { | |
| autoRefreshToken: true, | |
| persistSession: true, | |
| detectSessionInUrl: true, | |
| flowType: 'implicit', | |
| }; | |
| const DEFAULT_REALTIME_OPTIONS = {}; | |
| /** | |
| * Supabase Client. | |
| * | |
| * An isomorphic Javascript client for interacting with Postgres. | |
| */ | |
| export default class SupabaseClient { | |
| /** | |
| * Create a new client for use in the browser. | |
| * @param supabaseUrl The unique Supabase URL which is supplied when you create a new project in your project dashboard. | |
| * @param supabaseKey The unique Supabase Key which is supplied when you create a new project in your project dashboard. | |
| * @param options.db.schema You can switch in between schemas. The schema needs to be on the list of exposed schemas inside Supabase. | |
| * @param options.auth.autoRefreshToken Set to "true" if you want to automatically refresh the token before expiring. | |
| * @param options.auth.persistSession Set to "true" if you want to automatically save the user session into local storage. | |
| * @param options.auth.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user. | |
| * @param options.realtime Options passed along to realtime-js constructor. | |
| * @param options.global.fetch A custom fetch implementation. | |
| * @param options.global.headers Any additional headers to send with each network request. | |
| */ | |
| constructor(supabaseUrl, supabaseKey, options) { | |
| var _a, _b, _c, _d, _e, _f, _g, _h; | |
| this.supabaseUrl = supabaseUrl; | |
| this.supabaseKey = supabaseKey; | |
| if (!supabaseUrl) | |
| throw new Error('supabaseUrl is required.'); | |
| if (!supabaseKey) | |
| throw new Error('supabaseKey is required.'); | |
| const _supabaseUrl = stripTrailingSlash(supabaseUrl); | |
| this.realtimeUrl = `${_supabaseUrl}/realtime/v1`.replace(/^http/i, 'ws'); | |
| this.authUrl = `${_supabaseUrl}/auth/v1`; | |
| this.storageUrl = `${_supabaseUrl}/storage/v1`; | |
| this.functionsUrl = `${_supabaseUrl}/functions/v1`; | |
| // default storage key uses the supabase project ref as a namespace | |
| const defaultStorageKey = `sb-${new URL(this.authUrl).hostname.split('.')[0]}-auth-token`; | |
| const DEFAULTS = { | |
| db: DEFAULT_DB_OPTIONS, | |
| realtime: DEFAULT_REALTIME_OPTIONS, | |
| auth: Object.assign(Object.assign({}, DEFAULT_AUTH_OPTIONS), { storageKey: defaultStorageKey }), | |
| global: DEFAULT_GLOBAL_OPTIONS, | |
| }; | |
| const settings = applySettingDefaults(options !== null && options !== void 0 ? options : {}, DEFAULTS); | |
| this.storageKey = (_b = (_a = settings.auth) === null || _a === void 0 ? void 0 : _a.storageKey) !== null && _b !== void 0 ? _b : ''; | |
| this.headers = (_d = (_c = settings.global) === null || _c === void 0 ? void 0 : _c.headers) !== null && _d !== void 0 ? _d : {}; | |
| this.auth = this._initSupabaseAuthClient((_e = settings.auth) !== null && _e !== void 0 ? _e : {}, this.headers, (_f = settings.global) === null || _f === void 0 ? void 0 : _f.fetch); | |
| this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), (_g = settings.global) === null || _g === void 0 ? void 0 : _g.fetch); | |
| this.realtime = this._initRealtimeClient(Object.assign({ headers: this.headers }, settings.realtime)); | |
| this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, { | |
| headers: this.headers, | |
| schema: (_h = settings.db) === null || _h === void 0 ? void 0 : _h.schema, | |
| fetch: this.fetch, | |
| }); | |
| this._listenForAuthEvents(); | |
| } | |
| /** | |
| * Supabase Functions allows you to deploy and invoke edge functions. | |
| */ | |
| get functions() { | |
| return new FunctionsClient(this.functionsUrl, { | |
| headers: this.headers, | |
| customFetch: this.fetch, | |
| }); | |
| } | |
| /** | |
| * Supabase Storage allows you to manage user-generated content, such as photos or videos. | |
| */ | |
| get storage() { | |
| return new SupabaseStorageClient(this.storageUrl, this.headers, this.fetch); | |
| } | |
| /** | |
| * Perform a query on a table or a view. | |
| * | |
| * @param relation - The table or view name to query | |
| */ | |
| from(relation) { | |
| return this.rest.from(relation); | |
| } | |
| /** | |
| * Perform a query on a schema distinct from the default schema supplied via | |
| * the `options.db.schema` constructor parameter. | |
| * | |
| * The schema needs to be on the list of exposed schemas inside Supabase. | |
| * | |
| * @param schema - The name of the schema to query | |
| */ | |
| schema(schema) { | |
| return this.rest.schema(schema); | |
| } | |
| /** | |
| * Perform a function call. | |
| * | |
| * @param fn - The function name to call | |
| * @param args - The arguments to pass to the function call | |
| * @param options - Named parameters | |
| * @param options.head - When set to `true`, `data` will not be returned. | |
| * Useful if you only need the count. | |
| * @param options.count - Count algorithm to use to count rows returned by the | |
| * function. Only applicable for [set-returning | |
| * functions](https://www.postgresql.org/docs/current/functions-srf.html). | |
| * | |
| * `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the | |
| * hood. | |
| * | |
| * `"planned"`: Approximated but fast count algorithm. Uses the Postgres | |
| * statistics under the hood. | |
| * | |
| * `"estimated"`: Uses exact count for low numbers and planned count for high | |
| * numbers. | |
| */ | |
| rpc(fn, args = {}, options) { | |
| return this.rest.rpc(fn, args, options); | |
| } | |
| /** | |
| * Creates a Realtime channel with Broadcast, Presence, and Postgres Changes. | |
| * | |
| * @param {string} name - The name of the Realtime channel. | |
| * @param {Object} opts - The options to pass to the Realtime channel. | |
| * | |
| */ | |
| channel(name, opts = { config: {} }) { | |
| return this.realtime.channel(name, opts); | |
| } | |
| /** | |
| * Returns all Realtime channels. | |
| */ | |
| getChannels() { | |
| return this.realtime.getChannels(); | |
| } | |
| /** | |
| * Unsubscribes and removes Realtime channel from Realtime client. | |
| * | |
| * @param {RealtimeChannel} channel - The name of the Realtime channel. | |
| * | |
| */ | |
| removeChannel(channel) { | |
| return this.realtime.removeChannel(channel); | |
| } | |
| /** | |
| * Unsubscribes and removes all Realtime channels from Realtime client. | |
| */ | |
| removeAllChannels() { | |
| return this.realtime.removeAllChannels(); | |
| } | |
| _getAccessToken() { | |
| var _a, _b; | |
| return __awaiter(this, void 0, void 0, function* () { | |
| const { data } = yield this.auth.getSession(); | |
| return (_b = (_a = data.session) === null || _a === void 0 ? void 0 : _a.access_token) !== null && _b !== void 0 ? _b : null; | |
| }); | |
| } | |
| _initSupabaseAuthClient({ autoRefreshToken, persistSession, detectSessionInUrl, storage, storageKey, flowType, debug, }, headers, fetch) { | |
| const authHeaders = { | |
| Authorization: `Bearer ${this.supabaseKey}`, | |
| apikey: `${this.supabaseKey}`, | |
| }; | |
| return new SupabaseAuthClient({ | |
| url: this.authUrl, | |
| headers: Object.assign(Object.assign({}, authHeaders), headers), | |
| storageKey: storageKey, | |
| autoRefreshToken, | |
| persistSession, | |
| detectSessionInUrl, | |
| storage, | |
| flowType, | |
| debug, | |
| fetch, | |
| }); | |
| } | |
| _initRealtimeClient(options) { | |
| return new RealtimeClient(this.realtimeUrl, Object.assign(Object.assign({}, options), { params: Object.assign({ apikey: this.supabaseKey }, options === null || options === void 0 ? void 0 : options.params) })); | |
| } | |
| _listenForAuthEvents() { | |
| let data = this.auth.onAuthStateChange((event, session) => { | |
| this._handleTokenChanged(event, 'CLIENT', session === null || session === void 0 ? void 0 : session.access_token); | |
| }); | |
| return data; | |
| } | |
| _handleTokenChanged(event, source, token) { | |
| if ((event === 'TOKEN_REFRESHED' || event === 'SIGNED_IN') && | |
| this.changedAccessToken !== token) { | |
| // Token has changed | |
| this.realtime.setAuth(token !== null && token !== void 0 ? token : null); | |
| this.changedAccessToken = token; | |
| } | |
| else if (event === 'SIGNED_OUT') { | |
| // Token is removed | |
| this.realtime.setAuth(this.supabaseKey); | |
| if (source == 'STORAGE') | |
| this.auth.signOut(); | |
| this.changedAccessToken = undefined; | |
| } | |
| } | |
| } | |
| //# sourceMappingURL=SupabaseClient.js.map |