Skip to content

Commit

Permalink
modules for shared types and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Resul Avan authored and Resul Avan committed Jul 21, 2020
1 parent 553a0cc commit a57a86a
Show file tree
Hide file tree
Showing 37 changed files with 216 additions and 593 deletions.
17 changes: 4 additions & 13 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ jobs:
steps:
- checkout
- run:
name: Install commons-types dependencies
command: npm --prefix functions commons-types
- run:
name: Build commons-types
command: npm --prefix functions run build
name: Install dependencies
command: npm run install
- run:
name: Add .env
command: |
Expand Down Expand Up @@ -45,21 +42,15 @@ jobs:
echo " console.log('firebase-messaging-sw.js setBackgroundMessageHandler', payload)" >> src/static/firebase-messaging-sw.js
echo "})" >> src/static/firebase-messaging-sw.js
echo "" >> src/static/firebase-messaging-sw.js
echo ""
cat src/static/firebase-messaging-sw.js
- run:
name: Install functions dependencies
command: npm --prefix functions install
- run:
name: Install src dependencies
command: npm --prefix src install
- run:
name: deploy to Firebase Hosting
command: ./src/node_modules/.bin/firebase deploy --project=$FIREBASE_PROJECT_ID --token=$FIREBASE_CI_TOKEN
- run:
name: Call to warm up
command: |
curl "$WEBSITE_URL"
curl "$WEBSITE_URL/api/healthy"
curl "$WEBSITE_URL/sitemap.xml"
workflows:
version: 2
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ node_modules

# modules
lib
**/*.tar.gz
**/*.tgz

# firebase
functions/nuxt.config.ts
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ See the [Features](#features) for more functionalities
|<img width="1604" alt="Profile" src="https://firebasestorage.googleapis.com/v0/b/nuxt-ts-firebase-auth-ssr.appspot.com/o/github%2Fprofile.png?alt=media&token=b14fd223-1248-44bc-8f9f-92f45273c716"> | <img width="1604" alt="Ligthbox" src="https://firebasestorage.googleapis.com/v0/b/nuxt-ts-firebase-auth-ssr.appspot.com/o/github%2Flightbox.png?alt=media&token=347fe927-f425-49dd-8d2b-05c626de48cf">|<img width="1604" alt="Ligthbox view" src="https://firebasestorage.googleapis.com/v0/b/nuxt-ts-firebase-auth-ssr.appspot.com/o/github%2Flightbox-view.png?alt=media&token=d6e31892-c0cb-4708-9b9f-d53d52644e9e">|
|<img width="1604" alt="Crop" src="https://firebasestorage.googleapis.com/v0/b/nuxt-ts-firebase-auth-ssr.appspot.com/o/github%2Fcrop.png?alt=media&token=d547fe2e-fe51-4904-8e22-f3c4a6d6e886"> | <img width="1604" alt="login" src="https://firebasestorage.googleapis.com/v0/b/nuxt-ts-firebase-auth-ssr.appspot.com/o/github%2Flogin.png?alt=media&token=08d3333c-d54f-495a-937b-fa178d22b21d">|<img width="1604" alt="register" src="https://firebasestorage.googleapis.com/v0/b/nuxt-ts-firebase-auth-ssr.appspot.com/o/github%2Fregister.png?alt=media&token=2cd96b4d-3f34-4a4c-a4a5-0de7b72c3d6a">|

## Folder Structure

- [functions](./functions): source folder for firebase-functions
- [modules](./functions/modules): Shared types and functions between front-end and back-end (firebase-functions and nuxt serverMiddleware)
- [src](./src): nuxt application


## DETAILED DOCUMENTATION
> Coming soon
Expand Down Expand Up @@ -174,10 +181,9 @@ FIREBASE_MEASUREMENT_ID= ***

the root `package.json` has been created to manage build and deployment easily.

### install dependencies
### install dependencies (not `npm install` or `npm i` )
```bash
$ npm --prefix functions install
$ npm --prefix src install
$ npm run install
```

### deploy
Expand Down
3 changes: 3 additions & 0 deletions functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@

### deploy sitemapApp
firebase deploy --only functions:sitemapApp

## install modules
go to [modules](./modules)
28 changes: 9 additions & 19 deletions functions/modules/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
# modules
The code has nuxt frontend application and firebase functions as backend. Additionally, nuxt application has `serverMiddleware`. That `serverMiddleware` has same functionality as `firebase-functions`. That means there are some shared types and functions.

## build
npm i
npm run build

## install local modules
npm i <local-path-of-the-module>

or
npm i <tar.gz-path-of-the-module>


sample
The `modules` has been created to keep consistency, the code clean and reduce the maintenance duration.

npm i ../modules/types-module
npm i ../modules/types-module/types-module-1.0.0.tgz
The modules:
- [types-module](./types-module): has all shared types (enums, interfaces, constants)
- [handlers-module](./handlers-module): has all express handlers. Also, has all backend services like firebase-admin and firestore

## remove local modules
npm remove <package-name-of-the-module>
## npm install on all modules
npm run install

sample

npm remove types-module
## build all modules
npm run build
14 changes: 11 additions & 3 deletions functions/modules/handlers-module/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# types module
# handlers-module

## build
npm i
npm run build

## install
npm i ../modules/types-module
npm i <relative-path-from-target-package>

`on function`

npm i ../modules/handlers-module

`on src (nuxt)`

npm i ../functions/modules/handlers-module

## remove local modules
npm remove types-module
npm remove handlers-module
25 changes: 0 additions & 25 deletions functions/modules/handlers-module/src/config.ts

This file was deleted.

35 changes: 35 additions & 0 deletions functions/modules/handlers-module/src/handler-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import admin from 'firebase-admin'

export class HandlerConfig {

private static credentials = ''

private static readonly defaultInitializer = () => {
admin.initializeApp({
credential: admin.credential.applicationDefault()
})
console.log('firebase admin is initialized by default credentials')
return admin
}

private static readonly credentialsInitializer = () => {
const serviceAccount = HandlerConfig.credentials
// const serviceAccount = require(HandlerConfig.credentials)
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
})
console.log('firebase admin is initialized by custom credentials')
}

static setCredentials = (credentials: string) => {
HandlerConfig.credentials = credentials
}

static getAdmin () {
if (admin.apps.length === 0) {
this.credentials ? this.credentialsInitializer() : this.defaultInitializer()
}
return admin;
}

}
3 changes: 3 additions & 0 deletions functions/modules/handlers-module/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './handler-config'
export * from './api'
export * from './sitemap-handler'
export * from './service/request-handler-service'
export * from './service/global-service'
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import admin from 'firebase-admin'
import {
AuthUser,
collection,
Expand All @@ -13,19 +12,18 @@ import {
WhereClause
} from 'types-module'
import { deleteModel, getModelById, getModelsByWhereClauses } from './firestore-admin-collection-service'
import { HandlerConfig } from '../config'
import { HandlerConfig } from '../handler-config'
import admin from 'firebase-admin'
import DecodedIdToken = admin.auth.DecodedIdToken

HandlerConfig.initialize()

export const getDecodedIdToken = (idToken: string): Promise<DecodedIdToken> => {
return admin.auth()
return HandlerConfig.getAdmin().auth()
.verifyIdToken(idToken)
.then((decodedIdToken: DecodedIdToken) => decodedIdToken)
}

export const setCustomClaims = async (uid: string, firebaseClaims: FirebaseClaims): Promise<void> => {
await admin.auth().setCustomUserClaims(uid, firebaseClaims);
await HandlerConfig.getAdmin().auth().setCustomUserClaims(uid, firebaseClaims);
}

export const validateClaimsAndGet = async (decodedIdToken: DecodedIdToken) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseModel, WhereClause } from 'types-module'
import admin from 'firebase-admin'
import { HandlerConfig } from '../handler-config'

const toBaseModelArray = <T extends BaseModel> (querySnapshot: admin.firestore.QuerySnapshot): T[] => {
const docs: T[] = []
Expand All @@ -14,7 +15,7 @@ const getQueryByWhereClauses = (
collection: string,
whereClause: WhereClause,
...whereClauses: WhereClause[]) => {
const collectionReference = admin.firestore().collection(collection)
const collectionReference = HandlerConfig.getAdmin().firestore().collection(collection)

let query = collectionReference.where(whereClause.field, whereClause.operator, whereClause.value)
whereClauses.forEach((wc) => {
Expand All @@ -25,7 +26,7 @@ const getQueryByWhereClauses = (
}

export async function getModelById<T extends BaseModel> (collection: string, modelId: string) {
return await admin.firestore()
return await HandlerConfig.getAdmin().firestore()
.collection(collection)
.doc(modelId)
.get()
Expand All @@ -47,5 +48,5 @@ export async function getModelsByWhereClauses<T extends BaseModel> (
}

export const deleteModel = <T extends BaseModel> (collection: string, model: T) => {
return admin.firestore().collection(collection).doc(model.id as string).delete()
return HandlerConfig.getAdmin().firestore().collection(collection).doc(model.id as string).delete()
}
1 change: 1 addition & 0 deletions functions/modules/handlers-module/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"moduleResolution": "Node",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"types": [
"./src/types"
]
Expand Down
9 changes: 9 additions & 0 deletions functions/modules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"scripts": {
"build": "npm --prefix types-module run build && npm --prefix handlers-module run build",
"install": "npm --prefix types-module install && npm --prefix handlers-module install",
"re-install": "npm run clear-all && npm run install",
"clear": "rm -rf */lib",
"clear-all": "rm -rf */node_modules */lib */package-lock.json"
}
}
10 changes: 9 additions & 1 deletion functions/modules/types-module/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# types module
# types-module

## build
npm i
npm run build

## install
npm i <relative-path-from-target-package>

`on function`

npm i ../modules/types-module

`on src (nuxt)`

npm i ../functions/modules/types-module

## remove local modules
npm remove types-module
Loading

0 comments on commit a57a86a

Please sign in to comment.