Skip to content

Commit

Permalink
Merge pull request #11 from internxt/feature/improve-photos-types
Browse files Browse the repository at this point in the history
Added Json parse pipe to photos.devices, photos.photos
  • Loading branch information
carlossalasamper authored Dec 30, 2021
2 parents 6580584 + 77231c1 commit 110ff11
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@internxt/sdk",
"version": "0.1.8",
"version": "0.1.9",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
33 changes: 19 additions & 14 deletions src/photos/devices/index.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,59 @@
import axios from 'axios';

import { extractAxiosErrorMessage } from '../../utils';
import { Device, DeviceId, PhotosModel } from '..';
import { CreateDeviceData, Device, DeviceId, DeviceJSON, PhotosSdkModel } from '..';

export default class DevicesSubmodule {
private model: PhotosModel;
private model: PhotosSdkModel;

constructor(model: PhotosModel) {
constructor(model: PhotosSdkModel) {
this.model = model;
}

getDeviceById(deviceId: DeviceId): Promise<Device> {
public getDeviceById(deviceId: DeviceId): Promise<Device> {
return axios
.get<Device>(`${this.model.baseUrl}/photos/${deviceId}`, {
.get<DeviceJSON>(`${this.model.baseUrl}/devices/${deviceId}`, {
headers: {
Authorization: `Bearer ${this.model.accessToken}`,
},
})
.then((res) => {
return res.data;
})
.then((res) => this.parse(res.data))
.catch((err) => {
throw new Error(extractAxiosErrorMessage(err));
});
}

createDevice(device: Omit<Device, 'id'>): Promise<DeviceId> {
public createDevice(data: CreateDeviceData): Promise<Device> {
return axios
.post<DeviceId>(`${this.model.baseUrl}/photos`, device, {
.post<DeviceJSON>(`${this.model.baseUrl}/devices`, data, {
headers: {
Authorization: `Bearer ${this.model.accessToken}`,
},
})
.then((res) => {
return res.data;
})
.then((res) => this.parse(res.data))
.catch((err) => {
throw new Error(extractAxiosErrorMessage(err));
});
}

deleteDevice(deviceId: DeviceId): Promise<unknown> {
public deleteDevice(deviceId: DeviceId): Promise<void> {
return axios
.delete(`${this.model.baseUrl}/devices/${deviceId}`, {
headers: {
Authorization: `Bearer ${this.model.accessToken}`,
},
})
.then(() => undefined)
.catch((err) => {
throw new Error(extractAxiosErrorMessage(err));
});
}

private parse(json: DeviceJSON): Device {
return {
...json,
createdAt: new Date(json.createdAt),
updatedAt: new Date(json.updatedAt),
};
}
}
4 changes: 2 additions & 2 deletions src/photos/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { PhotosModel } from './types';
import { PhotosSdkModel } from './types';
import DevicesSubmodule from './devices';
import PhotosSubmodule from './photos';
import SharesSubmodule from './shares';

export class Photos {
private readonly model: PhotosModel;
private readonly model: PhotosSdkModel;

public readonly photos: PhotosSubmodule;
public readonly devices: DevicesSubmodule;
Expand Down
47 changes: 26 additions & 21 deletions src/photos/photos/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import axios from 'axios';

import { extractAxiosErrorMessage } from '../../utils';
import { Photo, PhotoId, PhotosModel } from '..';
import { CreatePhotoData, Photo, PhotoId, PhotoJSON, PhotosSdkModel } from '..';

export default class PhotosSubmodule {
private model: PhotosModel;
private model: PhotosSdkModel;

constructor(model: PhotosModel) {
constructor(model: PhotosSdkModel) {
this.model = model;
}

getPhotoById(photoId: PhotoId): Promise<Photo> {
public getPhotoById(photoId: PhotoId): Promise<Photo> {
return axios
.get<Photo>(`${this.model.baseUrl}/photos/${photoId}`, {
.get<PhotoJSON>(`${this.model.baseUrl}/photos/${photoId}`, {
headers: {
Authorization: `Bearer ${this.model.accessToken}`,
},
})
.then((res) => {
return res.data;
})
.then((res) => this.parse(res.data))
.catch((err) => {
throw new Error(extractAxiosErrorMessage(err));
});
}

getPhotos(offset: number, limit: number): Promise<Photo[]> {
public getPhotos(offset: number, limit: number): Promise<Photo[]> {
if (limit > 200 || limit < 1) {
throw new Error('Invalid limit. Limit should be positive and lower than 201. Provided limit was: ' + limit);
}
Expand All @@ -35,20 +33,18 @@ export default class PhotosSubmodule {
}

return axios
.get<Photo[]>(`${this.model.baseUrl}/photos/?offset=${offset}&limit=${limit}`, {
.get<PhotoJSON[]>(`${this.model.baseUrl}/photos/?offset=${offset}&limit=${limit}`, {
headers: {
Authorization: `Bearer ${this.model.accessToken}`,
},
})
.then((res) => {
return res.data;
})
.then((res) => res.data.map((photoJson) => this.parse(photoJson)))
.catch((err) => {
throw new Error(extractAxiosErrorMessage(err));
});
}

getPhotosCountByMonth(month: number, year: number): Promise<number> {
public getPhotosCountByMonth(month: number, year: number): Promise<number> {
return axios
.get<number>(`${this.model.baseUrl}/photos/?month=${month}&year=${year}`, {
headers: {
Expand All @@ -63,7 +59,7 @@ export default class PhotosSubmodule {
});
}

getPhotosCountByYear(year: number): Promise<number> {
public getPhotosCountByYear(year: number): Promise<number> {
return axios
.get<number>(`${this.model.baseUrl}/photos/?year=${year}`, {
headers: {
Expand All @@ -78,30 +74,39 @@ export default class PhotosSubmodule {
});
}

createPhoto(photo: Omit<Photo, 'id'>): Promise<PhotoId> {
public createPhoto(data: CreatePhotoData): Promise<Photo> {
return axios
.post<PhotoId>(`${this.model.baseUrl}/photos`, photo, {
.post<PhotoJSON>(`${this.model.baseUrl}/photos`, data, {
headers: {
Authorization: `Bearer ${this.model.accessToken}`,
},
})
.then((res) => {
return res.data;
})
.then((res) => this.parse(res.data))
.catch((err) => {
throw new Error(extractAxiosErrorMessage(err));
});
}

deletePhotoById(photoId: PhotoId): Promise<unknown> {
public deletePhotoById(photoId: PhotoId): Promise<void> {
return axios
.delete(`${this.model.baseUrl}/photos/${photoId}`, {
headers: {
Authorization: `Bearer ${this.model.accessToken}`,
},
})
.then(() => undefined)
.catch((err) => {
throw new Error(extractAxiosErrorMessage(err));
});
}

private parse(json: PhotoJSON): Photo {
return {
...json,
creationDate: new Date(json.creationDate),
lastStatusChangeAt: new Date(json.lastStatusChangeAt),
createdAt: new Date(json.createdAt),
updatedAt: new Date(json.updatedAt),
};
}
}
6 changes: 3 additions & 3 deletions src/photos/shares/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import axios from 'axios';
import { extractAxiosErrorMessage } from '../../utils';

import { CreatePhotoShareBody, PhotosModel, Share } from '..';
import { CreatePhotoShareBody, PhotosSdkModel, Share } from '..';

export default class SharesSubmodule {
private model: PhotosModel;
private model: PhotosSdkModel;

constructor(model: PhotosModel) {
constructor(model: PhotosSdkModel) {
this.model = model;
}

Expand Down
35 changes: 26 additions & 9 deletions src/photos/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface PhotosModel {
export interface PhotosSdkModel {
baseUrl: string;
accessToken?: string;
}
Expand All @@ -9,24 +9,30 @@ export interface Device {
mac: string;
name: string;
userId: string;
createdAt: Date;
updatedAt: Date;
}
export interface DeviceJSON extends Omit<Device, 'createdAt' | 'updatedAt'> {
createdAt: string;
updatedAt: string;
}

export type PhotoType = string;
export type FileId = string;
export type PhotoId = string;
export interface CreateDeviceData {
mac: string;
name: string;
userId: string;
}

export enum PhotoStatus {
Exists = 'EXISTS',
Trashed = 'TRASHED',
Deleted = 'DELETED',
}

export type FileId = string;
export type PhotoId = string;
export interface Photo {
id: PhotoId;
name: string;
type: PhotoType;
type: string;
size: number;
width: number;
height: number;
Expand All @@ -35,23 +41,34 @@ export interface Photo {
deviceId: DeviceId;
userId: string;
status: PhotoStatus;
lastStatusChangeAt: Date;
creationDate: Date;
createdAt: Date;
updatedAt: Date;
}
export interface PhotoJSON extends Omit<Photo, 'lastStatusChangeAt' | 'creationDate' | 'createdAt' | 'updatedAt'> {
lastStatusChangeAt: string;
creationDate: string;
createdAt: string;
updatedAt: string;
}
export type CreatePhotoData = Omit<Photo, 'id' | 'status' | 'lastStatusChangeAt' | 'createdAt' | 'updatedAt'>;

export type ShareId = string;
export interface Share {
id: string;
id: ShareId;
bucket: string;
encryptionKey: string;
photoId: string;
token: string;
views: number;
createdAt: Date;
updatedAt: Date;
}
export interface ShareJSON extends Omit<Share, 'createdAt' | 'updatedAt'> {
createdAt: string;
updatedAt: string;
}

export interface CreatePhotoShareBody {
encryptionKey: string;
views: number;
Expand Down

0 comments on commit 110ff11

Please sign in to comment.