Skip to content

Commit 7f30d1f

Browse files
authored
Merge pull request #17 from Strift/develop
Enable Nuxt Bridge integration and add composables
2 parents a07424e + e6e2ce1 commit 7f30d1f

File tree

5 files changed

+78
-44
lines changed

5 files changed

+78
-44
lines changed

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v14.17.3

package.json

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
{
22
"name": "vue-supabase",
3-
"version": "2.2.3",
3+
"version": "2.2.4",
44
"description": "A small wrapper for integrating supabase to Vuejs",
5-
"main": "dist/cjs/index.js",
6-
"module": "dist/esm/index.js",
7-
"types": "dist/types/index.d.ts",
8-
"engine": {
9-
"node": ">= 1"
10-
},
11-
"scripts": {
12-
"prepare": "npm run build",
13-
"test": "echo 'no tests to run'",
14-
"lint": "eslint . --ext .ts",
15-
"build": "tsc -p config/tsconfig.cjs.json && tsc -p config/tsconfig.esm.json && tsc -p config/tsconfig.types.json"
16-
},
17-
"repository": {
18-
"type": "git",
19-
"url": "git+https://github.com/scottrobertson/vue-supabase.git"
20-
},
215
"keywords": [
226
"vue",
237
"custom",
248
"supabase",
259
"vue-supabase"
2610
],
27-
"author": "Scott Robertson",
11+
"author": "Supabase Community",
2812
"license": "MIT",
13+
"homepage": "https://github.com/supabase-community/vue-supabase#readme",
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/supabase-community/vue-supabase.git"
17+
},
2918
"bugs": {
30-
"url": "https://github.com/scottrobertson/vue-supabase/issues"
19+
"url": "https://github.com/supabase-community/vue-supabase/issues"
20+
},
21+
"engine": {
22+
"node": ">= 1"
3123
},
32-
"homepage": "https://github.com/scottrobertson/vue-supabase#readme",
24+
"scripts": {
25+
"prepare": "npm run build",
26+
"test": "echo 'no tests to run'",
27+
"lint": "eslint . --ext .ts",
28+
"build": "tsc -p config/tsconfig.cjs.json && tsc -p config/tsconfig.esm.json && tsc -p config/tsconfig.types.json"
29+
},
30+
"main": "dist/cjs/index.js",
31+
"module": "dist/esm/index.js",
32+
"types": "dist/types/index.d.ts",
3333
"dependencies": {
3434
"@supabase/supabase-js": "^1.25.2",
3535
"vue-demi": "^0.11.2"
@@ -47,4 +47,4 @@
4747
"prettier": "^2.4.1",
4848
"typescript": "^4.4.4"
4949
}
50-
}
50+
}

src/VueSupabaseClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export class VueSupabaseClient extends SupabaseClient {
2020
configurable: true,
2121
});
2222
} else {
23+
app.mixin({
24+
provide: () => ({
25+
[supabaseSymbol]: self,
26+
}),
27+
});
2328
Object.defineProperty(app.prototype, "$supabase", {
2429
get: () => self,
2530
configurable: true,

src/composables.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
// @ts-ignore
2-
import { inject } from "vue-demi";
3-
import { SupabaseClient } from "@supabase/supabase-js";
2+
import { inject, onMounted, onUnmounted } from "vue-demi";
3+
import {
4+
SupabaseClient,
5+
AuthChangeEvent,
6+
Session,
7+
} from "@supabase/supabase-js";
48
import supabaseSymbol from "./symbol";
59

610
export function useSupabase(): SupabaseClient {
7-
return inject(supabaseSymbol);
11+
const supabase = inject<SupabaseClient>(supabaseSymbol);
12+
if (!supabase) {
13+
throw new Error("Supabase provider not found");
14+
}
15+
return supabase;
816
}
917

1018
export function useSupabaseAuth(): SupabaseClient["auth"] {
@@ -16,3 +24,33 @@ export function useSupabaseStorage(): SupabaseClient["storage"] {
1624
const supabase = useSupabase();
1725
return supabase.storage;
1826
}
27+
28+
export function useSupabaseFrom(): SupabaseClient["from"] {
29+
const supabase = useSupabase();
30+
return supabase.from;
31+
}
32+
33+
type AuthChangeHandler = (
34+
event: AuthChangeEvent,
35+
session: Session | null
36+
) => void;
37+
38+
export function useOnAuthStateChange(callback: AuthChangeHandler): void {
39+
const client = useSupabase();
40+
41+
onMounted(() => {
42+
if (client.auth.session()) {
43+
callback("SIGNED_IN", client.auth.session());
44+
}
45+
});
46+
47+
const { data: authListener } = client.auth.onAuthStateChange(
48+
(event, session) => {
49+
callback(event, session);
50+
}
51+
);
52+
53+
onUnmounted(() => {
54+
authListener?.unsubscribe();
55+
});
56+
}

src/index.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
/** @ts-ignore , vue-demi seems to be not strongly typed so typescript freaks out. */
22
import { App, Vue2, Plugin, PluginObject } from "vue-demi";
3-
import {
4-
SupabaseClient,
5-
SupabaseClientOptions,
6-
SupabaseRealtimePayload,
7-
AuthUser,
8-
AuthSession,
9-
Subscription,
10-
} from "@supabase/supabase-js";
11-
import { VueSupabaseClient, createVueSupabase } from "./VueSupabaseClient";
12-
import {
13-
useSupabase,
14-
useSupabaseAuth,
15-
useSupabaseStorage,
16-
} from "./composables";
3+
import { SupabaseClient, SupabaseClientOptions } from "@supabase/supabase-js";
4+
import { VueSupabaseClient } from "./VueSupabaseClient";
175

186
// @ts-expect-error: Module vue/types/vue cannot be found.
197
declare module "vue/types/vue" {
@@ -40,7 +28,9 @@ export function install(
4028
supabase.install(app);
4129
}
4230

43-
export { useSupabase, useSupabaseAuth, useSupabaseStorage, createVueSupabase };
31+
const VueSupabasePlugin: PluginObject<SupabasePluginOptions> | Plugin = {
32+
install,
33+
};
4434

4535
export {
4636
SupabaseClient,
@@ -51,10 +41,10 @@ export {
5141
AuthSession,
5242
AuthSession as Session,
5343
Subscription,
54-
};
44+
} from "@supabase/supabase-js";
5545

56-
const VueSupabase: PluginObject<SupabasePluginOptions> | Plugin = {
57-
install,
58-
};
46+
export * from "./composables";
47+
48+
export { createVueSupabase } from "./VueSupabaseClient";
5949

60-
export default VueSupabase;
50+
export default VueSupabasePlugin;

0 commit comments

Comments
 (0)