Skip to content

Commit

Permalink
feat(logging): add logging interceptor optimized for browser
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorsalgado committed Dec 6, 2021
1 parent 0639342 commit c745d73
Show file tree
Hide file tree
Showing 13 changed files with 54,652 additions and 54,236 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint/eslint-plugin', 'eslint-plugin-tsdoc'],
extends: ['plugin:@typescript-eslint/recommended', 'standard', 'prettier'],
root: true,
env: {
jest: true,
node: true
node: true,
browser: true
},
rules: {
'tsdoc/syntax': 'error',
Expand Down
13 changes: 9 additions & 4 deletions examples/react/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

import { DrizzleBuilder, GET, Query } from '@drizzle-http/core'
import { noop } from '@drizzle-http/core'
import { Streaming, StreamTo, StreamToHttpError, UndiciCallFactory } from '@drizzle-http/undici'
import { StreamTo, StreamToHttpError, UndiciCallFactory } from '@drizzle-http/undici'
import { StreamToResult } from '@drizzle-http/undici'
import { Streaming } from '@drizzle-http/undici'
import { createServer } from 'http'
import { Writable } from 'stream'
import url from 'url'
Expand All @@ -27,8 +28,6 @@ const partiesAPI = DrizzleBuilder.newBuilder()
const port = parseInt(String(process.env.PORT || 3001))

createServer((req, res) => {
console.log('REQUEST RECEIVED')

const cors = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET, PUT, DELETE, PATCH',
Expand All @@ -46,6 +45,12 @@ createServer((req, res) => {
const header = { 'Content-Type': 'application/json;charset=utf-8' }
const charset = 'utf-8'

if (req.method === 'POST') {
res.writeHead(405, 'Method Not Allowed', { ...header, ...cors })
res.write(JSON.stringify({ error: 'Method Not Allowed' }), charset)
res.end()
}

res.setHeader('Content-Type', 'application/json;charset=utf-8')
res.writeHead(200, { ...header, ...cors })

Expand All @@ -58,7 +63,7 @@ createServer((req, res) => {
})
.catch((err: StreamToHttpError) => {
res.writeHead(500, 'Internal Server Error', { ...header, ...cors })
res.write(JSON.stringify({ error: err.stack }), charset)
res.write(JSON.stringify({ error: err.message }), charset)
res.end()
})
})
Expand Down
7 changes: 3 additions & 4 deletions examples/react/src/api.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

import { AsJSON, ContentType, DrizzleBuilder, FullResponse, GET, MediaTypes, noop, Query } from '@drizzle-http/core'
import { AsJSON, DrizzleBuilder, GET, noop, Query } from '@drizzle-http/core'
import { CORS, FetchCallFactory, KeepAlive } from '@drizzle-http/fetch'
import { Level, LoggingInterceptor } from '@drizzle-http/logging-interceptor'
import { BrowserLoggingInterceptor, Level } from '@drizzle-http/logging-interceptor'

const PORT = process.env.PORT || 3001

class PartiesClientAPI {
@GET('/')
@ContentType(MediaTypes.APPLICATION_JSON_UTF8)
@CORS()
@KeepAlive(true)
@AsJSON()
Expand All @@ -20,6 +19,6 @@ class PartiesClientAPI {
export const deputiesApi = DrizzleBuilder.newBuilder()
.baseUrl(`http://localhost:${PORT}`)
.callFactory(FetchCallFactory.DEFAULT)
.addInterceptor(new LoggingInterceptor(Level.BODY))
.addInterceptor(new BrowserLoggingInterceptor(Level.BODY))
.build()
.create(PartiesClientAPI)
3 changes: 2 additions & 1 deletion examples/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"references": [
{ "path": "../../pkgs/drizzle-http-core", "prepend": false },
{ "path": "../../pkgs/drizzle-http-undici", "prepend": false }
{ "path": "../../pkgs/drizzle-http-undici", "prepend": false },
{ "path": "../../pkgs/drizzle-http-logging-interceptor", "prepend": false }
],
"include": ["http/"]
}
Loading

0 comments on commit c745d73

Please sign in to comment.