Skip to content

Commit

Permalink
Feat/#66 JS SDK api 명세에 맞게 수정 (#70)
Browse files Browse the repository at this point in the history
* fix: app-ip -> appKey in SDK

* fix: created_at -> timestamp

* fix: Nginx CORS 허용 헤더 변경

- app-id -> appKey
  • Loading branch information
LuizyHub authored Aug 17, 2024
1 parent 8ef2537 commit ff1965a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions nginx/sites-available/default
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ server {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,app-id' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,appKey' always;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
Expand All @@ -53,7 +53,7 @@ server {
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,app-id' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,appKey' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
}
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ CDN을 통해 직접 스크립트를 포함할 수 있습니다:
LogBat SDK를 사용하기 전에 먼저 초기화해야 합니다:

```html

<script src="https://sdk.logbat.info/sdk.js"></script>
<script>
LogBat.init({ appId: 'YOUR_APP_ID' });
LogBat.init({appKey: 'YOUR_APP_KEY'});
</script>
```

Expand Down
13 changes: 7 additions & 6 deletions sdk/js/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe('LogBat SDK', () => {
jest.clearAllMocks();
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
LogBat.init({ appId: 'test-app' });
LogBat['isInitialized'] = false;
LogBat.init({ appKey: 'test-app' });
});

afterEach(() => {
Expand All @@ -25,7 +26,7 @@ describe('LogBat SDK', () => {
});

test('should initialize with appId', () => {
expect(LogBat['appId']).toBe('test-app');
expect(LogBat['appKey']).toBe('test-app');
});

test('should send log message', async () => {
Expand All @@ -34,12 +35,12 @@ describe('LogBat SDK', () => {
expect(consoleLogSpy).toHaveBeenCalledWith('Test log message');
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(
'https://api.logbat.info/log',
'https://api.logbat.info/logs',
expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({
'Content-Type': 'application/json',
'app_id': 'test-app'
'appKey': 'test-app'
}),
body: expect.stringContaining('Test log message')
})
Expand All @@ -52,12 +53,12 @@ describe('LogBat SDK', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith('Test error message');
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(
'https://api.logbat.info/log',
'https://api.logbat.info/logs',
expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({
'Content-Type': 'application/json',
'app_id': 'test-app'
'appKey': 'test-app'
}),
body: expect.stringContaining('Test error message')
})
Expand Down
12 changes: 6 additions & 6 deletions sdk/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class LogBat {
private static appId: string = '';
private static appKey: string = '';
private static apiEndpoint: string = 'https://api.logbat.info/logs';
private static originalConsole: { log: typeof console.log; error: typeof console.error };
private static isInitialized: boolean = false;

public static init(config: { appId: string }): void {
public static init(config: { appKey: string }): void {
if (this.isInitialized) {
console.warn('LogBat SDK is already initialized. Ignoring repeated initialization.');
return;
}

this.appId = config.appId;
this.appKey = config.appKey;
this.overrideConsoleMethods();
this.isInitialized = true;
}
Expand All @@ -30,18 +30,18 @@ class LogBat {
private static async sendLog(level: string, args: any[]): Promise<void> {
const logData = {
level: level,
created_at: new Date().toISOString(),
data: args.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
).join(' ')
).join(' '),
timestamp: new Date().toISOString()
};

try {
const response = await fetch(this.apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'app-id': this.appId
'appKey': this.appKey
},
body: JSON.stringify(logData),
});
Expand Down

0 comments on commit ff1965a

Please sign in to comment.