Skip to content

Instantly share code, notes, and snippets.

View dsibinski's full-sized avatar

Dawid Sibiński dsibinski

View GitHub Profile
View getAllUsers_with_zod.ts
getAllUsers = async (): Promise<UserViewModel[]> => {
const url = `${this.apiEndpoint}/AllUsers`;
const response = await fetch(url);
const usersJson = await response.json();
const users = z.array(UserViewModelSchema).parse(usersJson);
return users;
};
View userViewModel_zod.ts
import { z } from "zod";
import { AddressViewModelSchema } from "./addressViewModel";
export const UserViewModelSchema = z.object({
id: z.string().uuid(),
name: z.string(),
lastName: z.string(),
login: z.string(),
isActive: z.boolean(),
loyaltyPoints: z.number(),
View UserViewModel_ZodSchema.ts
export const UserViewModelSchema = z.object({
id: z.string().uuid(),
name: z.string(),
lastName: z.string(),
login: z.string(),
isActive: z.boolean(),
loyaltyPoints: z.number(),
address: AddressViewModelSchema.nullable(),
});
View UserViewModel_renamed.cs
public record UserViewModel(Guid Id, string Name, string LastName,
string Login, bool IsActive, int FidelityPoints, AddressViewModel? Address = null);
View usersService.ts
export class UsersService {
private apiEndpoint: string;
constructor() {
this.apiEndpoint = "https://localhost:7131/Users";
}
getAllUsers = async (): Promise<UserViewModel[]> => {
const url = `${this.apiEndpoint}/AllUsers`;
const response = await fetch(url);
View userViewModel.ts
export interface UserViewModel {
id: Guid;
name: string;
lastName: string;
login: string;
isActive: boolean;
loyaltyPoints: number;
address: AddressViewModel | null;
}
View UserViewModel.cs
public record UserViewModel(Guid Id, string Name, string LastName,
string Login, bool IsActive, int LoyaltyPoints, AddressViewModel? Address = null);
View AspNetControllerAction.cs
public List<UserViewModel> AllUsers()
{
var usersViewModels = TestDataGenerator.GetTestUsers();
return usersViewModels.OrderBy(uvm => uvm.Name).ToList();
}
View ApiFetchSample.ts
getAllUsers = async (): Promise<UserViewModel[]> => {
const url = `${this.apiEndpoint}/AllUsers`;
const response = await fetch(url);
const users = (await response.json()) as UserViewModel[];
return users;
};
View AspNetControllerSample.cs
public record UserViewModel(Guid Id, string Name, string LastName, string Login,
bool IsActive, int LoyaltyPoints, AddressViewModel? Address = null);
// ...
public List<UserViewModel> AllUsers()
{
var usersViewModels = TestDataGenerator.GetTestUsers();
return usersViewModels.OrderBy(uvm => uvm.Name).ToList();
}