Skip to content

Commit

Permalink
Refactors protolib/base to not include external files, just before ex…
Browse files Browse the repository at this point in the history
…tracting it into its own package
  • Loading branch information
jcarlosn committed Jul 6, 2024
1 parent d0c60bd commit cc8d650
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/app/features/auth/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useSearchParams, useRouter } from 'solito/navigation';
import { getErrorMessage } from "@my/ui";
import { SiteConfig } from '../../conf'
import { useSession, useSessionContext } from 'protolib/lib/Session'
import { createSession } from 'protolib/api/lib/session'
import { createSession } from 'protolib/base/Session'
import { Auth } from 'protolib/lib/Auth'
import { Center } from 'protolib/components/Center'
import { HorizontalBox } from 'protolib/components/HorizontalBox'
Expand Down
2 changes: 1 addition & 1 deletion packages/app/features/auth/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ProtofyLogoSVG, Separator, XStack, getValidation } from '@my/ui'
import { useSearchParams, useRouter } from 'solito/navigation';
import { getErrorMessage } from "@my/ui";
import { useSession, useSessionContext } from 'protolib/lib/Session'
import { createSession } from 'protolib/api/lib/session'
import { createSession } from 'protolib/base/Session'
import { Auth } from 'protolib/lib/Auth'
import { Center } from 'protolib/components/Center'
import { HorizontalBox } from 'protolib/components/HorizontalBox'
Expand Down
3 changes: 1 addition & 2 deletions packages/protobase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"clean": "rm -rf dist"
"build": "tsc"
},
"files": [
"dist"
Expand Down
7 changes: 4 additions & 3 deletions packages/protolib/src/api/lib/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import type {Request, RequestHandler, Response} from 'express';
import {ZodError} from "../../base";
import {createSession, SessionDataType} from './session';
import type { Request, RequestHandler, Response } from 'express';
import { ZodError } from "../../base";
import { SessionDataType } from './session';
import { createSession } from '../../base/Session';
import {verifyToken} from './crypt';
import { getLogger } from '../../base';

Expand Down
12 changes: 0 additions & 12 deletions packages/protolib/src/api/lib/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ export const getSessionContext = async (type) => {
return { state: 'resolved', group: type ? (await API.get('/adminapi/v1/groups/'+type)).data : {} }
}

export const createSession = (data?:userData, token?:string):SessionDataType => {
return {
user: {
admin: data?.admin ? data?.admin : false,
id: data?.id ? data.id : 'guest',
type: data?.id ? (data?.type ? data.type : 'user') : 'guest',
permissions: data?.permissions ? data?.permissions : []
},
token: token ? token : '',
loggedIn: data?.id ? true : false
}
}
export const validateSession = async (session:SessionDataType):Promise<validatedUserData> => jwt.verify(session.token ?? '', process.env.TOKEN_SECRET ?? '') as validatedUserData
//DO NOT DELETE
//This is the code to validate sessions in api instead of nextjs
Expand Down
2 changes: 1 addition & 1 deletion packages/protolib/src/base/Api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PendingResult, getPendingResult } from "../base/PendingResult";
import { PendingResult, getPendingResult } from "./PendingResult";
import {devMode} from './env'
import { isElectron } from "./lib/isElectron";
import { getLogger } from "./logger"
Expand Down
1 change: 0 additions & 1 deletion packages/protolib/src/base/BaseSchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Zod from 'zod'
import { v4 as uuidv4 } from 'uuid';
import moment from 'moment';
import { ProtoModel } from './ProtoModel';

initSchemaSystem()

Expand Down
2 changes: 1 addition & 1 deletion packages/protolib/src/base/ProtoCollection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SessionDataType } from "../api/lib/session";
import { SessionDataType } from "./lib/perms";
import { ProtoModel } from "./ProtoModel";

export class ProtoCollection<T extends ProtoModel<any>> {
Expand Down
5 changes: 3 additions & 2 deletions packages/protolib/src/base/ProtoModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createSession, SessionDataType } from 'protolib/api/lib/session'
import { ZodObject } from 'protolib/base'
import { createSession } from './Session'
import { SessionDataType } from './lib/perms'
import { ZodObject } from './BaseSchema'
import { ProtoSchema } from './ProtoSchema';
import { getLogger } from "./logger"
import { API } from './Api';
Expand Down
14 changes: 14 additions & 0 deletions packages/protolib/src/base/Session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SessionDataType } from "./lib/perms"

export const createSession = (data?, token?:string):SessionDataType => {
return {
user: {
admin: data?.admin ? data?.admin : false,
id: data?.id ? data.id : 'guest',
type: data?.id ? (data?.type ? data.type : 'user') : 'guest',
permissions: data?.permissions ? data?.permissions : []
},
token: token ? token : '',
loggedIn: data?.id ? true : false
}
}
3 changes: 2 additions & 1 deletion packages/protolib/src/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from './lib/isElectron'
export * from './lib/perms'
export * from './lib/getEnv'
export * from './logger'
export * from './Protofy'
export * from './Protofy'
export * from './Session'
5 changes: 3 additions & 2 deletions packages/protolib/src/lib/Session.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { atom, useAtom } from 'jotai';
import { atomWithStorage, useHydrateAtoms } from 'jotai/utils';
import * as cookie from 'cookie'
import { createSession, validateSession, SessionDataType, getSessionContext } from '../api/lib/session';
import { validateSession, SessionDataType, getSessionContext } from '../api/lib/session';
import { NextPageContext } from 'next'
import { parse } from 'cookie';
import { getLogger } from "protolib/base/logger"
import { getLogger } from "../base/logger"
import { createSession } from '../base/Session'

const logger = getLogger()

Expand Down
3 changes: 1 addition & 2 deletions packages/protonode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"clean": "rm -rf dist"
"build": "tsc"
},
"files": [
"dist"
Expand Down

0 comments on commit cc8d650

Please sign in to comment.