Skip to content

Commit

Permalink
✨ call getEvents endpoints for events and aggregateIds
Browse files Browse the repository at this point in the history
  • Loading branch information
anaisberg committed Aug 18, 2023
1 parent 1b95d32 commit b965a10
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 49 deletions.
3 changes: 2 additions & 1 deletion packages/http-event-storage-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"watch": "rm -rf dist && concurrently 'yarn:package-* --watch'"
},
"dependencies": {
"swagger-client": "^3.20.0"
"swagger-client": "^3.20.0",
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/cli": "^7.17.6",
Expand Down
45 changes: 27 additions & 18 deletions packages/http-event-storage-adapter/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import SwaggerClient from 'swagger-client';
import uniqBy from 'lodash/uniqBy';
import SwaggerClient, { Api } from 'swagger-client';

import { EventDetail, GroupedEvent, StorageAdapter } from '@castore/core';

import { getEventTag } from './utils/getEventTag';
import { Swagger } from './utils/types';

export class HttpEventStorageAdapter implements StorageAdapter {
getEvents: StorageAdapter['getEvents'];
listAggregateIds: StorageAdapter['listAggregateIds'];
Expand All @@ -11,32 +15,37 @@ export class HttpEventStorageAdapter implements StorageAdapter {
putSnapshot: StorageAdapter['putSnapshot'];
getLastSnapshot: StorageAdapter['getLastSnapshot'];
listSnapshots: StorageAdapter['listSnapshots'];
swagger: string;
swagger: Swagger;
baseUrl: string;
apiClient: SwaggerClient;
eventApi: Api | undefined;

constructor({ swagger, baseUrl }: { swagger: string; baseUrl: string }) {
constructor({ swagger, baseUrl }: { swagger: Swagger; baseUrl: string }) {
this.baseUrl = baseUrl;
this.swagger = swagger;

this.apiClient = new SwaggerClient(this.swagger);
const apiClient = new SwaggerClient({ spec: this.swagger });
const apiEventTag = getEventTag(apiClient);
this.eventApi = apiClient.apis[apiEventTag];

this.getEvents = async (aggregateId: string) => {
const events: EventDetail[] = [];

// const response = await this.apiClient.apis.event.getEvents({
// aggregateId,
// });
// await response.json();
// const events: EventDetail[] = await response.json();
// ...
await Promise.resolve();
console.log('aggregateId', aggregateId);

return { events };
if (this.eventApi?.getEvents === undefined) return { events: [] };

const { body } = await this.eventApi.getEvents({
aggregateId,
});

return { events: body.events };
};

this.listAggregateIds = async () => {
return Promise.resolve({ aggregateIds: [] });
if (this.eventApi?.getEvents === undefined) return { aggregateIds: [] };

const { body } = await this.eventApi.getEvents({});
const aggregateIds = uniqBy(body.events, 'aggregateId').map(
event => event.aggregateId,
);

return { aggregateIds };
};

// We do not implement snapshots in this adapter
Expand Down
36 changes: 13 additions & 23 deletions packages/http-event-storage-adapter/src/script.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import SwaggerClient from 'swagger-client';

// import swagger from './pokeapi.json';
import swagger from './pokeapi.json';

const main = async () => {
// console.log(swagger);
await Promise.resolve();
const apiClient = new SwaggerClient(
'https://petstore.swagger.io/v2/swagger.json',
);
// eslint-disable-next-line @typescript-eslint/await-thenable
const client = await apiClient;
const apis = client.apis;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const petApi = apis.pet;
console.log('getPokemon', petApi);
if (petApi === undefined) {
console.log('petApi is not defined');
const apiClient = new SwaggerClient({ spec: swagger });

const apis = apiClient.apis;

return;
}
const findPetsByTags = petApi.findPetsByTags;
if (findPetsByTags === undefined) {
console.log('findPetsByTags is not defined');
const pokemonApi = apis.default;
console.log('getPokemon', pokemonApi);
const getPokemon = pokemonApi.getEvents;

return;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const response = await findPetsByTags({ tags: ['dogs'] });
console.log(response);
const { body } = await getPokemon({
idOrName: ['pikachu'],
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
// const body = response.body;
console.log(body);
};
void main();
25 changes: 19 additions & 6 deletions packages/http-event-storage-adapter/src/swaggerClient.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
declare module 'swagger-client' {
import type { Swagger, Tag } from './utils/types.d.ts';
import { EventDetail } from '@castore/core';

type Api = {
getEvents: (
arg: Record<unknown, unknown>,
) => Promise<{ body: { events: EventDetail[] } }>;
};

export default class SwaggerClient {
http(request: unknown): unknown;
apis: Record<
string,
Record<string, (arg: Record<unknown, unknown>) => unknown>
>;
apis: {
default: Api;
event?: Api;
};
tags: Tag[];

constructor(url: unknown, options?: Record<string, unknown>);
constructor(
url: string | { spec: Swagger },
options?: Record<string, unknown>,
);
}
export type SwaggerClientConstructor = new (
url: unknown,
url: string | { spec: Swagger },
options?: Record<string, unknown>,
) => SwaggerClient;
}
14 changes: 14 additions & 0 deletions packages/http-event-storage-adapter/src/utils/getEventTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Tag } from './types';

export const getEventTag = (swagger: { tags?: Tag[] }): 'default' | 'event' => {
const { tags } = swagger;
if (tags === undefined || tags.length === 0) {
return 'default';
}
const hasEventTag = tags.findIndex(tag => tag.name === 'event');
if (hasEventTag > -1) {
return 'event';
}

return 'default';
};
50 changes: 50 additions & 0 deletions packages/http-event-storage-adapter/src/utils/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
type Contact = Record<string, unknown>;
type License = Record<string, unknown>;

type Info = {
title: string; // The title of the API.
version: string; // The version of the OpenAPI document (which is distinct from the OpenAPI Specification version or the API implementation version).
summary?: string;
description?: string; // A short description of the API.
termsOfService?: string;
contact?: Contact;
license: License;
};

type Server = {
url: string;
description?: string;
variables?: Record<string, unknown>;
};

type ExternalDocumentation = {
url: string;
description?: string;
};

type PathItem = {
$ref?: string; // Allows for a referenced definition of this path item. The referenced structure MUST be in the form of a Path Item Object. In case a Path Item Object field appears both in the defined object and the referenced object, the behavior is undefined. See the rules for resolving Relative References.
summary?: string; // An optional, string summary, intended to apply to all operations in this path.
description?: string; // An optional, string description, intended to apply to all operations in this path. CommonMark syntax MAY be used for rich text representation.
};

type Path = Record<string, PathItem>;

export type Tag = {
name: string; // The name of the tag.
description?: string; // A description for the tag. CommonMark syntax MAY be used for rich text representation.
externalDocs: ExternalDocumentation;
};

export type Swagger = {
openapi: string; // This string MUST be the version number of the OpenAPI Specification that the OpenAPI document uses.
infoObject: Info; // Provides metadata about the API. The metadata MAY be used by tooling as required.
jsonSchemaDialect?: string; // The default value for the $schema keyword within Schema Objects contained within this OAS document. This MUST be in the form of a URI.
servers?: Server[]; // An array of Server Objects, which provide connectivity information to a target server. If the servers property is not provided, or is an empty array, the default value would be a Server Object with a url value of /.
paths?: Path[];
webhooks?: Record<string, unknown>;
components?: unknown;
security?: Record<string, unknown>[];
tags?: Tag[];
externalDocs?: ExternalDocumentation;
};
2 changes: 1 addition & 1 deletion packages/http-event-storage-adapter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"ts-node": {
"files": true
},
"files": ["src/script.ts", "src/swaggerClient.d.ts"],
"files": ["src/script.ts", "src/swaggerClient.d.ts", "src/utils/types.d.ts"],
"exclude": ["./dist"],
"include": ["./**/*.ts", "./**/pokeapi.json"]
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5694,6 +5694,7 @@ __metadata:
concurrently: ^7.1.0
dependency-cruiser: ^11.7.0
eslint: ^8.14.0
lodash: ^4.17.21
lodash.omit: ^4.5.0
mockdate: ^3.0.5
prettier: ^2.6.2
Expand Down

0 comments on commit b965a10

Please sign in to comment.