-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #490 from gitroomhq/feat/public-api
Public API
- Loading branch information
Showing
18 changed files
with
550 additions
and
525 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; | ||
import { AuthService } from '@gitroom/backend/services/auth/auth.service'; | ||
import { StripeService } from '@gitroom/nestjs-libraries/services/stripe.service'; | ||
import { PoliciesGuard } from '@gitroom/backend/services/auth/permissions/permissions.guard'; | ||
import { PermissionsService } from '@gitroom/backend/services/auth/permissions/permissions.service'; | ||
import { IntegrationManager } from '@gitroom/nestjs-libraries/integrations/integration.manager'; | ||
import { UploadModule } from '@gitroom/nestjs-libraries/upload/upload.module'; | ||
import { OpenaiService } from '@gitroom/nestjs-libraries/openai/openai.service'; | ||
import { ExtractContentService } from '@gitroom/nestjs-libraries/openai/extract.content.service'; | ||
import { CodesService } from '@gitroom/nestjs-libraries/services/codes.service'; | ||
import { PublicIntegrationsController } from '@gitroom/backend/public-api/routes/v1/public.integrations.controller'; | ||
import { PublicAuthMiddleware } from '@gitroom/backend/services/auth/public.auth.middleware'; | ||
|
||
const authenticatedController = [ | ||
PublicIntegrationsController | ||
]; | ||
@Module({ | ||
imports: [ | ||
UploadModule, | ||
], | ||
controllers: [ | ||
...authenticatedController, | ||
], | ||
providers: [ | ||
AuthService, | ||
StripeService, | ||
OpenaiService, | ||
ExtractContentService, | ||
PoliciesGuard, | ||
PermissionsService, | ||
CodesService, | ||
IntegrationManager, | ||
], | ||
get exports() { | ||
return [...this.imports, ...this.providers]; | ||
}, | ||
}) | ||
export class PublicApiModule implements NestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer.apply(PublicAuthMiddleware).forRoutes(...authenticatedController); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
apps/backend/src/public-api/routes/v1/public.integrations.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
HttpException, | ||
Post, | ||
Query, | ||
UploadedFile, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request'; | ||
import { Organization } from '@prisma/client'; | ||
import { IntegrationService } from '@gitroom/nestjs-libraries/database/prisma/integrations/integration.service'; | ||
import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability'; | ||
import { | ||
AuthorizationActions, | ||
Sections, | ||
} from '@gitroom/backend/services/auth/permissions/permissions.service'; | ||
import { CreatePostDto } from '@gitroom/nestjs-libraries/dtos/posts/create.post.dto'; | ||
import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/posts.service'; | ||
import { FileInterceptor } from '@nestjs/platform-express'; | ||
import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory'; | ||
import { MediaService } from '@gitroom/nestjs-libraries/database/prisma/media/media.service'; | ||
import { GetPostsDto } from '@gitroom/nestjs-libraries/dtos/posts/get.posts.dto'; | ||
|
||
@ApiTags('Public API') | ||
@Controller('/public/v1') | ||
export class PublicIntegrationsController { | ||
private storage = UploadFactory.createStorage(); | ||
|
||
constructor( | ||
private _integrationService: IntegrationService, | ||
private _postsService: PostsService, | ||
private _mediaService: MediaService | ||
) {} | ||
|
||
@Post('/upload') | ||
@UseInterceptors(FileInterceptor('file')) | ||
async uploadSimple( | ||
@GetOrgFromRequest() org: Organization, | ||
@UploadedFile('file') file: Express.Multer.File | ||
) { | ||
if (!file) { | ||
throw new HttpException({ msg: 'No file provided' }, 400); | ||
} | ||
|
||
const getFile = await this.storage.uploadFile(file); | ||
return this._mediaService.saveFile( | ||
org.id, | ||
getFile.originalname, | ||
getFile.path | ||
); | ||
} | ||
|
||
@Get('/posts') | ||
async getPosts( | ||
@GetOrgFromRequest() org: Organization, | ||
@Query() query: GetPostsDto | ||
) { | ||
const posts = await this._postsService.getPosts(org.id, query); | ||
|
||
return { | ||
posts, | ||
// comments, | ||
}; | ||
} | ||
|
||
@Post('/posts') | ||
@CheckPolicies([AuthorizationActions.Create, Sections.POSTS_PER_MONTH]) | ||
createPost( | ||
@GetOrgFromRequest() org: Organization, | ||
@Body() body: CreatePostDto | ||
) { | ||
console.log(JSON.stringify(body, null, 2)); | ||
return this._postsService.createPost(org.id, body); | ||
} | ||
|
||
@Get('/integrations') | ||
async listIntegration(@GetOrgFromRequest() org: Organization) { | ||
return (await this._integrationService.getIntegrationsList(org.id)).map( | ||
(org) => ({ | ||
id: org.id, | ||
name: org.name, | ||
identifier: org.providerIdentifier, | ||
picture: org.picture, | ||
disabled: org.disabled, | ||
profile: org.profile, | ||
customer: org.customer | ||
? { | ||
id: org.customer.id, | ||
name: org.customer.name, | ||
} | ||
: undefined, | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { HttpStatus, Injectable, NestMiddleware } from '@nestjs/common'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { OrganizationService } from '@gitroom/nestjs-libraries/database/prisma/organizations/organization.service'; | ||
import { HttpForbiddenException } from '@gitroom/nestjs-libraries/services/exception.filter'; | ||
|
||
@Injectable() | ||
export class PublicAuthMiddleware implements NestMiddleware { | ||
constructor(private _organizationService: OrganizationService) {} | ||
async use(req: Request, res: Response, next: NextFunction) { | ||
const auth = (req.headers.authorization || | ||
req.headers.Authorization) as string; | ||
if (!auth) { | ||
res.status(HttpStatus.UNAUTHORIZED).json({ msg: 'No API Key found' }); | ||
return; | ||
} | ||
try { | ||
const org = await this._organizationService.getOrgByApiKey(auth); | ||
if (!org) { | ||
res.status(HttpStatus.UNAUTHORIZED).json({ msg: 'Invalid API key' }); | ||
return ; | ||
} | ||
|
||
if (!!process.env.STRIPE_SECRET_KEY && !org.subscription) { | ||
res.status(HttpStatus.UNAUTHORIZED).json({ msg: 'No subscription found' }); | ||
return ; | ||
} | ||
|
||
// @ts-ignore | ||
req.org = {...org, users: [{users: {role: 'SUPERADMIN'}}]}; | ||
} catch (err) { | ||
throw new HttpForbiddenException(); | ||
} | ||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
apps/frontend/src/components/public-api/public.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use client'; | ||
|
||
import { useState, useCallback } from 'react'; | ||
import { useUser } from '../layout/user.context'; | ||
import { Button } from '@gitroom/react/form/button'; | ||
import copy from 'copy-to-clipboard'; | ||
import { useToaster } from '@gitroom/react/toaster/toaster'; | ||
|
||
export const PublicComponent = () => { | ||
const user = useUser(); | ||
const toaster = useToaster(); | ||
const [reveal, setReveal] = useState(false); | ||
|
||
const copyToClipboard = useCallback(() => { | ||
toaster.show('API Key copied to clipboard', 'success'); | ||
copy(user?.publicApi!); | ||
}, [user]); | ||
|
||
if (!user || !user.publicApi) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col"> | ||
<h2 className="text-[24px]">Public API</h2> | ||
<div className="text-customColor18 mt-[4px]"> | ||
Use Postiz API to integrate with your tools. | ||
</div> | ||
<div className="my-[16px] mt-[16px] bg-sixth border-fifth items-center border rounded-[4px] p-[24px] flex gap-[24px]"> | ||
<div className="flex items-center"> | ||
{reveal ? ( | ||
user.publicApi | ||
) : ( | ||
<> | ||
<div className="blur-sm">{user.publicApi.slice(0, -5)}</div> | ||
<div>{user.publicApi.slice(-5)}</div> | ||
</> | ||
)} | ||
</div> | ||
<div> | ||
{!reveal ? ( | ||
<Button onClick={() => setReveal(true)}>Reveal</Button> | ||
) : ( | ||
<Button onClick={copyToClipboard}>Copy Key</Button> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.