18 Aralık 2019 Çarşamba

package.json Dosyası - Node.js Projelerinde Kullanılır

Giriş
Açıklaması şöyle
The package.json is a very important file in Node.js. It is called the heart of this technology. It holds the metadata for the Node.js project.
The Node.js developer can find the package.json in the root directory of the Node.js module or app.
npm init is the command used to create a project using the package.json file.
Her ayarı bu dosyaya eklemek doğru olmayabilir. Bir çok kütüphane kendi dosyası ile de çalışabiliyor. Açıklaması şöyle.
For example: babel in babel.config.js, jest in jest.config.js, eslint in an .eslintrc.json, etc.
dependencies Alanı
Proje tarafından kullanılan kütüphane listesidir. npm install komutu ile kütüphaneler indirilir
Örnek
"dependencies": {
  "axios": "0.18.0",
  "lodash": "4.17.10",
  "react": "16.12.0"
"react-dng": "3.0.2"
"react-dnd-html5-backend": "7.4.4"
"react-dom": "16.12.0"
"react-redux": "7.1.3"
"react-router": "4.3.1"
"react-router-dom": "4.3.1"
"react-scripts-ts": "2.16.0"
"react-vis": "1.11.4"
}
scripts Alanı
start, build, test, eject komutları için çalışacak script listesidir
Örnek
"scripts": {
  "start": "node Main"  
}
Örnek
"scripts": {
  "start": "react-scripts-ts start"  
  "build": "react-scripts-ts build"  
  "test": "react-scripts-ts test --env=jsdom"  
  "eject": "react-scripts-ts eject"  
}
eslintConfig Alanı
Örnek
"eslintConfig ": {
  "extends": "react-app"
}

16 Aralık 2019 Pazartesi

TypeScript Map

Giriş
TypeScript artık Map'i destekliyor.

Örnek - Kullanmayın
Eski kodlarda şöyle yaparız.
type FooMap<E extends Foo<any>> = {[key: string]: E };

axios

Giriş
Şu satırı dahil ederiz.
import axios from "axios";
axios iki şekilde kullanılabilir.

1. axios Promise döndürdüğü için asenkron olarak
2. try ve catch içinde veya

all metodu
Tüm işlemlerin bitmesini bekler. Şöyle yaparız.
axios.all([getObjectZero(), getObjectOne()])
create metodu
Örnek
Şöyle yaparız.
 // creating axios 
 const api = axios.create({
 baseURL: https://speech.googleapis.com/v1/operations,
 crossDomain: true,
 responseType: 'json',
 headers: {
 'Accept': 'application/json',
 'Content-Type': 'application/json',
 'Authorization':'your_key
 },
});

// calling api

api.get('/speech_name')
.then(res=>{
console.log(res);
}).
catch('error');
get metodu
Örnek
Açıklaması şöyle.
You'll have to access the Axios response's data to get the actual response payload.
Cevabın içindeki data alanına erişmek için şöyle yaparız.
export const getNews = async () => {
  const response = await axios.get("https://...");
  return response.data;
};
Örnek
Şöyle yaparız.
const objectZeroURL = 'http://example.com/objects[0].a';
const objectOneURL = 'http://example.com/objects[1].a';

function getObjectZero() {
  return axios.get(objectZeroURL);
}

function getObjectOne() {
  return axios.get(objectOneURL);
}
get metodu - url parametreleri
Şöyle yaparız.
const access_key ='my_key'
axios.get('http://data.fixer.io/api/2013-12-24', {
  params: {
    access_key: access_key,
    base: LKR,
    symbols: ETH
  }
})
.then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.log(error);
})
post metodu
Şöyle yaparız.
axios.post('/somewhere', {id: 123})
.catch(function (error){
  console.log(err)
})

TypeScript Array

Array Tanımlama
Örnek
Şöyle yaparız
let arrayOfStrings:string[] = [ 'some', 'strings' ];
Örnek
Şöyle yaparız. Bu aslında Tuple gibi düşünülebilir.
let myArray:[ string, number ] = [ "some", 15 ]
Örnek - Mixed Array
Açıklaması şöyle
Sometimes, an array may be made of multiple types, but have an unknown length. In this situation, we can use a union type. For instance, an array of unknown length which only consists of strings and numbers, looks could be defined as this
Şöyle yaparız. 
let myArray:(string|number)[] = [ "some", 15 ]

Type Inference
Bazen derleyici type inference (tip çıkarımı) yapamıyor. Yardımcı olmak için metodun isminden sonra şöyle yaparız.
let pillarsCount = blockSides
  .map((blockSide) => {
    ...
    return 0
  })
  .reduce<number>((a, b) => a + b, 0)
find metodu
Şöyle yaparız.
const hostel = hostels.find(x => x.identifier === '433456');