@prefabs.tech/fastify-firebase 0.90.0 → 0.90.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prefabs.tech/fastify-firebase",
|
|
3
|
-
"version": "0.90.
|
|
3
|
+
"version": "0.90.2",
|
|
4
4
|
"description": "Fastify firebase plugin",
|
|
5
5
|
"homepage": "https://github.com/prefabs-tech/fastify/tree/main/packages/firebase#readme",
|
|
6
6
|
"repository": {
|
|
@@ -13,10 +13,10 @@
|
|
|
13
13
|
"exports": {
|
|
14
14
|
".": {
|
|
15
15
|
"import": "./dist/prefabs-tech-fastify-firebase.js",
|
|
16
|
-
"require": "./dist/prefabs-tech-fastify-firebase.
|
|
16
|
+
"require": "./dist/prefabs-tech-fastify-firebase.cjs"
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
|
-
"main": "./dist/prefabs-tech-fastify-firebase.
|
|
19
|
+
"main": "./dist/prefabs-tech-fastify-firebase.cjs",
|
|
20
20
|
"module": "./dist/prefabs-tech-fastify-firebase.js",
|
|
21
21
|
"types": "./dist/types/index.d.ts",
|
|
22
22
|
"files": [
|
|
@@ -24,18 +24,18 @@
|
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"firebase-admin": "12.7.0",
|
|
27
|
-
"zod": "3.25.
|
|
27
|
+
"zod": "3.25.76"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@prefabs.tech/eslint-config": "0.2.0",
|
|
31
|
-
"@prefabs.tech/fastify-config": "0.90.
|
|
32
|
-
"@prefabs.tech/fastify-error-handler": "0.90.
|
|
33
|
-
"@prefabs.tech/fastify-graphql": "0.90.
|
|
34
|
-
"@prefabs.tech/fastify-slonik": "0.90.
|
|
31
|
+
"@prefabs.tech/fastify-config": "0.90.2",
|
|
32
|
+
"@prefabs.tech/fastify-error-handler": "0.90.2",
|
|
33
|
+
"@prefabs.tech/fastify-graphql": "0.90.2",
|
|
34
|
+
"@prefabs.tech/fastify-slonik": "0.90.2",
|
|
35
35
|
"@prefabs.tech/tsconfig": "0.2.0",
|
|
36
|
-
"@types/node": "20.19.
|
|
37
|
-
"@typescript-eslint/eslint-plugin": "8.
|
|
38
|
-
"@typescript-eslint/parser": "8.
|
|
36
|
+
"@types/node": "20.19.13",
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "8.42.0",
|
|
38
|
+
"@typescript-eslint/parser": "8.42.0",
|
|
39
39
|
"@vitest/coverage-istanbul": "3.2.4",
|
|
40
40
|
"eslint": "8.57.1",
|
|
41
41
|
"eslint-config-prettier": "9.1.2",
|
|
@@ -43,25 +43,25 @@
|
|
|
43
43
|
"eslint-import-resolver-typescript": "3.10.1",
|
|
44
44
|
"eslint-plugin-import": "2.32.0",
|
|
45
45
|
"eslint-plugin-n": "14.0.0",
|
|
46
|
-
"eslint-plugin-prettier": "5.5.
|
|
46
|
+
"eslint-plugin-prettier": "5.5.4",
|
|
47
47
|
"eslint-plugin-promise": "7.2.1",
|
|
48
48
|
"eslint-plugin-unicorn": "56.0.1",
|
|
49
|
-
"fastify": "5.
|
|
49
|
+
"fastify": "5.5.0",
|
|
50
50
|
"fastify-plugin": "5.0.1",
|
|
51
|
-
"graphql": "16.
|
|
52
|
-
"mercurius": "16.
|
|
51
|
+
"graphql": "16.11.0",
|
|
52
|
+
"mercurius": "16.2.0",
|
|
53
53
|
"prettier": "3.6.2",
|
|
54
54
|
"slonik": "46.8.0",
|
|
55
55
|
"supertokens-node": "14.1.4",
|
|
56
|
-
"typescript": "5.
|
|
56
|
+
"typescript": "5.9.2",
|
|
57
57
|
"vite": "6.3.5",
|
|
58
58
|
"vitest": "3.2.4"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
|
61
|
-
"@prefabs.tech/fastify-config": "0.90.
|
|
62
|
-
"@prefabs.tech/fastify-error-handler": "0.90.
|
|
63
|
-
"@prefabs.tech/fastify-graphql": "0.90.
|
|
64
|
-
"@prefabs.tech/fastify-slonik": "0.90.
|
|
61
|
+
"@prefabs.tech/fastify-config": "0.90.2",
|
|
62
|
+
"@prefabs.tech/fastify-error-handler": "0.90.2",
|
|
63
|
+
"@prefabs.tech/fastify-graphql": "0.90.2",
|
|
64
|
+
"@prefabs.tech/fastify-slonik": "0.90.2",
|
|
65
65
|
"fastify": ">=5.2.1",
|
|
66
66
|
"fastify-plugin": ">=5.0.1",
|
|
67
67
|
"mercurius": ">=16.1.0",
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
(function(i,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("fastify-plugin"),require("firebase-admin"),require("slonik"),require("@prefabs.tech/fastify-slonik"),require("mercurius"),require("@prefabs.tech/fastify-graphql")):typeof define=="function"&&define.amd?define(["exports","fastify-plugin","firebase-admin","slonik","@prefabs.tech/fastify-slonik","mercurius","@prefabs.tech/fastify-graphql"],h):(i=typeof globalThis<"u"?globalThis:i||self,h(i.PrefabsTechFastifyFirebase={},i.FastifyPlugin,i.FirebaseAdmin,i.Slonik,i.PrefabsTechFastifySlonik,i.mercurius,i.PrefabsTechFastifyGraphql))})(this,function(i,h,g,l,S,c,E){"use strict";const U=(e,r)=>{if(!(g.apps.length>0)){if(e.firebase?.enabled!==!1&&!e.firebase.credentials){r.log.error("Firebase credentials are missing");return}try{g.initializeApp({credential:g.credential.cert({projectId:e.firebase.credentials?.projectId,privateKey:e.firebase.credentials?.privateKey.replaceAll(String.raw`\n`,`
|
|
2
|
-
`),clientEmail:e.firebase.credentials?.clientEmail})})}catch(t){r.log.error("Failed to initialize firebase"),r.log.error(t)}}},y=async e=>{await g.messaging().sendEachForMulticast(e)},I="/send-notification",w="/user-device",R="/user-device",D="user_devices",N=e=>{const r=e.firebase.table?.userDevices?.name||D;return l.sql.unsafe`
|
|
3
|
-
DO $$
|
|
4
|
-
BEGIN
|
|
5
|
-
CREATE TABLE IF NOT EXISTS ${l.sql.identifier([r])} (
|
|
6
|
-
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
|
7
|
-
user_id VARCHAR(255) NOT NULL,
|
|
8
|
-
device_token VARCHAR(255) NOT NULL,
|
|
9
|
-
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
10
|
-
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
11
|
-
);
|
|
12
|
-
|
|
13
|
-
CREATE INDEX IF NOT EXISTS idx_user_id_device_token ON ${l.sql.identifier([r])} (user_id, device_token);
|
|
14
|
-
END $$;
|
|
15
|
-
`},$=async(e,r)=>{await e.connect(async t=>{await t.query(N(r))})};class O extends S.DefaultSqlFactory{static TABLE=D;getDeleteExistingTokenSql(r){return l.sql.type(this.validationSchema)`
|
|
16
|
-
DELETE
|
|
17
|
-
FROM ${this.tableFragment}
|
|
18
|
-
${this.getWhereFragment({filterFragment:l.sql.fragment`device_token = ${r}`})}
|
|
19
|
-
RETURNING *;
|
|
20
|
-
`}getFindByUserIdSql(r){return l.sql.type(this.validationSchema)`
|
|
21
|
-
SELECT *
|
|
22
|
-
FROM ${this.tableFragment}
|
|
23
|
-
${this.getWhereFragment({filterFragment:l.sql.fragment`user_id = ${r}`})};
|
|
24
|
-
`}get table(){return this.config.firebase.table?.userDevices?.name||super.table}}class v extends S.BaseService{async getByUserId(r){const t=this.factory.getFindByUserIdSql(r);return await this.database.connect(n=>n.any(t))}async removeByDeviceToken(r){const t=this.factory.getDeleteExistingTokenSql(r);return await this.database.connect(n=>n.maybeOne(t))}get factory(){return super.factory}get sqlFactoryClass(){return O}async preCreate(r){const{deviceToken:t}=r;return await this.removeByDeviceToken(t),r}}const M={sendNotification:async(e,r)=>{if(!e.user)throw e.server.httpErrors.unauthorized("Unauthorised");const{body:o,title:n,data:a,userId:u}=e.body,s=await new v(e.config,e.slonik,e.dbSchema).getByUserId(u);if(!s||s.length===0)throw e.server.httpErrors.unprocessableEntity("No devices found for the receiver");const p=s.map(b=>b.deviceToken),f={android:{priority:"high",notification:{sound:"default"}},apns:{payload:{aps:{sound:"default"}}},tokens:p,notification:{title:n,body:o},data:{...a,title:n,body:o}};await y(f),r.send({message:"Notification sent successfully"})}},C={description:"Send a notification to a specific user",operationId:"sendNotification",body:{type:"object",properties:{title:{type:"string"},message:{type:"string"},userId:{type:"string"}},required:["title","message","userId"]},response:{200:{type:"object",properties:{success:{type:"boolean"},message:{type:"string"}}},401:{description:"Unauthorized",$ref:"ErrorResponse#"},500:{$ref:"ErrorResponse#"}},tags:["notifications"]},T=e=>async()=>{if(e.config.firebase.enabled===!1)throw e.httpErrors.notFound("Firebase is disabled")},F=async e=>{const r=e.config.firebase.handlers?.userDevice,t=e.config.firebase.notification;t?.test?.enabled&&e.post(t.test.path||I,{preHandler:[e.verifySession(),T(e)],schema:C},r?.addUserDevice||M.sendNotification)},_={addUserDevice:async(e,r)=>{const{body:t,config:o,dbSchema:n,slonik:a,user:u}=e;if(!u)throw e.server.httpErrors.unauthorized("Unauthorised");const{deviceToken:d}=t,s=new v(o,a,n);r.send(await s.create({userId:u.id,deviceToken:d}))},removeUserDevice:async(e,r)=>{const t=e.user;if(!t)throw e.server.httpErrors.unauthorized("Unauthorised");const{deviceToken:o}=e.body,n=new v(e.config,e.slonik,e.dbSchema),a=await n.getByUserId(t.id);if(!a||a.length===0)throw e.server.httpErrors.notFound("No devices found for the user");if(!a.find(d=>d.deviceToken===o))throw e.server.httpErrors.unprocessableEntity("Device requested to delete not owned by user");r.send(await n.removeByDeviceToken(o))}},k={type:"object",properties:{userId:{type:"string"},deviceToken:{type:"string"},createdAt:{type:"number"},updatedAt:{type:"number"}},required:["userId","deviceToken","createdAt","updatedAt"]},q={description:"Delete a user device by device token",operationId:"deleteUserDevice",body:{type:"object",properties:{deviceToken:{type:"string"}},required:["deviceToken"]},response:{200:{...k,nullable:!0},401:{description:"Unauthorized",$ref:"ErrorResponse#"},500:{$ref:"ErrorResponse#"}},tags:["user-devices"]},B={description:"Register a new user device",operationId:"postUserDevice",body:{type:"object",properties:{deviceToken:{type:"string"}},required:["deviceToken"]},response:{200:{...k,nullable:!0},401:{description:"Unauthorized",$ref:"ErrorResponse#"},500:{$ref:"ErrorResponse#"}},tags:["user-devices"]},A=async e=>{const r=e.config.firebase.handlers?.userDevice;e.post(w,{preHandler:[e.verifySession(),T(e)],schema:B},r?.addUserDevice||_.addUserDevice),e.delete(R,{preHandler:[e.verifySession(),T(e)],schema:q},r?.removeUserDevice||_.removeUserDevice)},L=h(async e=>{const{config:r,slonik:t,log:o}=e;r.firebase.enabled===!1?o.info("fastify-firebase plugin is not enabled"):(o.info("Registering fastify-firebase plugin"),await $(t,r),U(r,e));const{routePrefix:n,routes:a}=r.firebase;a?.notifications?.disabled||await e.register(F,{prefix:n}),a?.userDevices?.disabled||await e.register(A,{prefix:n})}),W={Mutation:{sendNotification:async(e,r,t)=>{const{app:o,config:n,dbSchema:a,database:u,user:d}=t;if(!d)return new c.mercurius.ErrorWithProps("unauthorized",{},401);if(n.firebase.enabled===!1)return new c.mercurius.ErrorWithProps("Firebase is not enabled",{},404);try{const{userId:s,title:p,body:f,data:b}=r.data;if(!s)return new c.mercurius.ErrorWithProps("Receiver id is required",{},400);const m=await new v(n,u,a).getByUserId(s);if(!m||m.length===0)return new c.mercurius.ErrorWithProps("Receiver device not found",{},404);const H={tokens:m.map(Y=>Y.deviceToken),notification:{title:p,body:f},data:b};return await y(H),{message:"Notification sent successfully"}}catch(s){return o.log.error(s),new c.mercurius.ErrorWithProps("Oops, Something went wrong",{},500)}}},Query:{}},z={Mutation:{addUserDevice:async(e,r,t)=>{const{app:o,config:n,dbSchema:a,database:u,user:d}=t;if(n.firebase.enabled===!1)return new c.mercurius.ErrorWithProps("Firebase is not enabled",{},404);if(!d)return new c.mercurius.ErrorWithProps("unauthorized",{},401);try{const{deviceToken:s}=r.data;return await new v(n,u,a).create({userId:d.id,deviceToken:s})}catch(s){return o.log.error(s),new c.mercurius.ErrorWithProps("Oops, Something went wrong",{},500)}},removeUserDevice:async(e,r,t)=>{const{app:o,config:n,dbSchema:a,database:u,user:d}=t;if(n.firebase.enabled===!1)return new c.mercurius.ErrorWithProps("Firebase is not enabled",{},404);if(!d)return new c.mercurius.ErrorWithProps("unauthorized",{},401);try{const{deviceToken:s}=r.data,p=new v(n,u,a),f=await p.getByUserId(d.id);return!f||f.length===0?new c.mercurius.ErrorWithProps("No devices found for user",{},403):f.find(P=>P.deviceToken===s)?await p.removeByDeviceToken(s):new c.mercurius.ErrorWithProps("Device requested to delete not owned by user",{},403)}catch(s){return o.log.error(s),new c.mercurius.ErrorWithProps("Oops, Something went wrong",{},500)}}},Query:{}},V=E.gql`
|
|
25
|
-
type SendNotificationResponse {
|
|
26
|
-
message: String!
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
input SendNotificationInput {
|
|
30
|
-
userId: String!
|
|
31
|
-
title: String!
|
|
32
|
-
body: String!
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
type Mutation {
|
|
36
|
-
sendNotification(data: SendNotificationInput): SendNotificationResponse
|
|
37
|
-
@auth
|
|
38
|
-
}
|
|
39
|
-
`,j=E.gql`
|
|
40
|
-
type UserDevice {
|
|
41
|
-
id: Int!
|
|
42
|
-
userId: String!
|
|
43
|
-
deviceToken: String!
|
|
44
|
-
createdAt: Float!
|
|
45
|
-
updatedAt: Float!
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
type UserDevices {
|
|
49
|
-
data: [UserDevice]!
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
input UserDeviceCreateInput {
|
|
53
|
-
deviceToken: String!
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
input UserDeviceUpdateInput {
|
|
57
|
-
deviceToken: String!
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
input UserDeviceRemoveInput {
|
|
61
|
-
deviceToken: String!
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
type Mutation {
|
|
65
|
-
addUserDevice(data: UserDeviceCreateInput): UserDevice @auth
|
|
66
|
-
removeUserDevice(data: UserDeviceRemoveInput): UserDevice @auth
|
|
67
|
-
}
|
|
68
|
-
`,Q=E.mergeTypeDefs([E.baseSchema,V,j]);i.ROUTE_SEND_NOTIFICATION=I,i.ROUTE_USER_DEVICE_ADD=w,i.ROUTE_USER_DEVICE_REMOVE=R,i.TABLE_USER_DEVICES=D,i.UserDeviceService=v,i.createUserDevicesTableQuery=N,i.default=L,i.firebaseSchema=Q,i.initializeFirebase=U,i.notificationResolver=W,i.notificationRoutes=F,i.sendPushNotification=y,i.userDeviceResolver=z,i.userDeviceRoutes=A,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|