Skip to content

Commit

Permalink
Migrate the API and Auth service from legacy to services
Browse files Browse the repository at this point in the history
  • Loading branch information
sondreb committed Oct 8, 2023
1 parent 69519c8 commit 2649605
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/src/app/people/people.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { DataService } from '../services/data';
import { CircleService } from '../services/circle';
import { OptionsService } from '../services/options';
import { MetricService } from '../services/metric-service';
import { ApiService } from '../legacy/services/api.service';
import { ApiService } from '../services/api.service';

@Component({
selector: 'app-people',
Expand Down
2 changes: 1 addition & 1 deletion app/src/app/projects/project/project.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { copyToClipboard } from '../../shared/utilities';
import { Subscription, tap } from 'rxjs';
import { DataService } from '../../services/data';
import { NavigationService } from '../../services/navigation';
import { ApiService } from '../../legacy/services/api.service';
import { ApiService } from '../../services/api.service';

@Component({
selector: 'app-project',
Expand Down
7 changes: 4 additions & 3 deletions app/src/app/projects/projects.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { copyToClipboard } from '../shared/utilities';
import { Subscription, tap } from 'rxjs';
import { DataService } from '../services/data';
import { NavigationService } from '../services/navigation';
import { ApiService } from '../legacy/services/api.service';
import { ApiService } from '../services/api.service';

@Component({
selector: 'app-projects',
Expand Down Expand Up @@ -75,7 +75,9 @@ export class ProjectsComponent {

getFollowingInCircle(id?: number) {
if (id == null) {
return this.profileService.following.filter((f) => f.circle == null || f.circle == 0);
return this.profileService.following.filter(
(f) => f.circle == null || f.circle == 0
);
} else {
return this.profileService.following.filter((f) => f.circle == id);
}
Expand Down Expand Up @@ -146,7 +148,6 @@ export class ProjectsComponent {

this.projects = await this.apiService.projects();


// this.subscriptions.push(this.profileService.items$.subscribe((profiles) => (this.following = profiles)) as Subscription);
}
}
183 changes: 183 additions & 0 deletions app/src/app/services/api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import { Injectable } from "@angular/core";
import { environment } from "../../environments/environment";

@Injectable({ providedIn: 'root' })
export class ApiService {
constructor() {}

baseUrl() {
return environment.apiUrl;
}

async categories() {
const response = await fetch(`${environment.apiUrl}/category/root`);

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result;
}

async collections() {
const response = await fetch(`${environment.apiUrl}/collection`);

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result;
}

async projects() {
const response = await fetch(`${environment.apiUrl}/project`);

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result;
}

async project(id: string) {
const response = await fetch(`${environment.apiUrl}/project/${id}`);

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result;
}

async users() {
const response = await fetch(`${environment.apiUrl}/user`);

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result;
}

// async deleteCategory() {
// const response = await fetch(`${environment.apiUrl}/collection`);

// if (response.status >= 400) {
// throw new Error(response.statusText);
// }

// const result = await response.json();
// return result;
// }

async insertCategory(item: any) {
const response = await fetch(`${environment.apiUrl}/category`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(item),
});

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result.item;
}

async updateCategory(id: string, item: any) {
const response = await fetch(`${environment.apiUrl}/category/${id}`, {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(item),
});

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result.item;
}

async deleteCategory(id: string) {
const response = await fetch(`${environment.apiUrl}/category/${id}`, {
method: 'DELETE',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result;
}

async insertUser(item: any) {
const response = await fetch(`${environment.apiUrl}/user`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(item),
});

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result.item;
}

async updateUser(id: string, item: any) {
const response = await fetch(`${environment.apiUrl}/user/${id}`, {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(item),
});

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result.item;
}

async deleteUser(id: string) {
const response = await fetch(`${environment.apiUrl}/user/${id}`, {
method: 'DELETE',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});

if (response.status >= 400) {
throw new Error(response.statusText);
}

const result = await response.json();
return result;
}
}
67 changes: 67 additions & 0 deletions app/src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@@ -1,66 +0,0 @@
import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';

@Injectable({ providedIn: 'root' })
export class AuthService {
constructor() {}

baseUrl() {
return environment.apiUrl;
}

async challenge() {
const response = await fetch(`${environment.apiUrl}/authenticate`);

if (response.status >= 400) {
throw new Error('Unable to receive authentication challenge.');
}

const result = await response.json();
return result;
}

async verify(challenge: string) {
const response = await fetch(`${environment.apiUrl}/authenticate`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(challenge),
});

if (response.status >= 400) {
throw new Error('Unable to verify authentication challenge.');
}

const result = await response.json();
return result;
}

async authenticated() {
const response = await fetch(
`${environment.apiUrl}/authenticate/protected`
);

if (response.status == 200) {
const result = await response.json();

return result;
} else {
return null;
}
}

async logout() {
const response = await fetch(`${environment.apiUrl}/authenticate/logout`);

// if (response.status >= 400) {
// throw new Error('Unable to receive authentication challenge.');
// }

const result = await response.json();
console.log('LOGOUT RESULT:', result);
return result;
}
}
2 changes: 1 addition & 1 deletion app/src/app/services/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { QueueService } from './queue.service';
import { UIService } from './ui';
import { CircleService } from './circle';
import { NostrService } from './nostr';
import { ApiService } from '../legacy/services/api.service';
import { ApiService } from '../services/api.service';

@Injectable({
providedIn: 'root',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blockcore-hub",
"version": "0.0.10",
"version": "0.0.11",
"scripts": {
"version": "node -p \"require('./package.json').version\""
},
Expand Down

0 comments on commit 2649605

Please sign in to comment.